/**
 * 
 */
package com.wetongji_android;

import java.util.List;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.j256.ormlite.android.apptools.OpenHelperManager;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.wetongji_android.R;
import com.wetongji_android.data.Course;
import com.wetongji_android.data.Exam;
import com.wetongji_android.data.WTDbHelper;
import com.wetongji_android.util.WTDateParser;

/**
 * @author hezibo
 *
 */
public class WTCourseDetailActivity extends SherlockActivity 
{
	private ImageView img_course_detail_avatar;
	private TextView tv_course_detail_no;
	private TextView tv_course_detail_name;
	private TextView tv_course_teacher;
	private TextView tv_course_time_location;
	private TextView tv_course_point;
	private TextView tv_course_required;
	private TextView tv_course_hours;
	
	private WTDbHelper dbHelper;
	
	private String courseId;
	private String examId;
	private boolean from;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.app_course_details);
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setIcon(R.drawable.ic_action_back);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        
		img_course_detail_avatar=(ImageView) findViewById(
				R.id.img_course_detail_avatar);
        tv_course_detail_no=(TextView) findViewById(R.id.tv_course_detail_no);
        tv_course_detail_name=(TextView) findViewById(R.id.tv_course_detail_name);
        tv_course_teacher=(TextView) findViewById(R.id.tv_course_detail_teacher);
        tv_course_time_location=(TextView) findViewById(R.id.tv_course_detail_time_location);
        tv_course_point=(TextView) findViewById(R.id.tv_course_detail_point);
        tv_course_required=(TextView) findViewById(R.id.tv_course_detail_required);
        tv_course_hours=(TextView) findViewById(R.id.tv_course_detail_hours);
        
        courseId=getIntent().getStringExtra("course_id");
        examId=getIntent().getStringExtra("exam_id");
        from = getIntent().getBooleanExtra("from_reminder", false);
        dbHelper=OpenHelperManager.getHelper(this, WTDbHelper.class);
        
        if(courseId!=null)
        	setTitle(R.string.course_detail_view_title);
        else if(examId!=null)
        	setTitle(R.string.exam_detail_view_title);
        readCourseContext();
        
	}
	
	@Override
	protected void onDestroy() 
	{
		super.onDestroy();
		if(dbHelper!=null){
			OpenHelperManager.releaseHelper();
			dbHelper=null;
		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) 
	{
		if(keyCode == KeyEvent.KEYCODE_BACK)
		{
			finish();
			if(from)
			{
				overridePendingTransition(R.anim.still_when_down, R.anim.roll_down);
			}else
			{
				overridePendingTransition(R.anim.left_to_inter, R.anim.inter_to_right);
			}
			return true;
		}
		
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) 
	{
		switch(item.getItemId())
		{
		case android.R.id.home:
		{
			finish();
			if(from)
			{
				overridePendingTransition(R.anim.still_when_down, R.anim.roll_down);
			}else
			{
				overridePendingTransition(R.anim.left_to_inter, R.anim.inter_to_right);
			}
			return true;
		}
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void readCourseContext(){
		if(courseId!=null){
			RuntimeExceptionDao<Course, String> courseDao=dbHelper.getCourseDao();
			List<Course> courseList=courseDao.queryForEq("NO", courseId);
			Course firstCourse=courseList.get(0);
			if(firstCourse.getRequired().equals("±ØÐÞ"))
				img_course_detail_avatar.setImageResource(R.drawable.blue_dot);
			else
				img_course_detail_avatar.setImageResource(R.drawable.green_dot);
			tv_course_detail_no.setText(firstCourse.getNO());
			tv_course_detail_name.setText(firstCourse.getName());
			tv_course_teacher.setText(firstCourse.getTeacher());
			String courseTimeLocation="";
			for(int i=0;i!=courseList.size();i++){
				Course course=courseList.get(i);
				if(!course.getWeekType().equals("È«"))
					courseTimeLocation+=course.getWeekType()+"ÖÜ ";
				courseTimeLocation+=course.getWeekDay()+" ";
				courseTimeLocation+=course.getSectionStart()+"-"+course.getSectionEnd()+"½Ú ";
				courseTimeLocation+=" "+course.getLocation();
				if(i!=courseList.size()-1)
					courseTimeLocation+="\n";
			}
			tv_course_time_location.setText(courseTimeLocation);
			tv_course_point.setText(String.valueOf(firstCourse.getPoint()));
			tv_course_required.setText(firstCourse.getRequired());
			tv_course_hours.setText(String.valueOf(firstCourse.getHours()));
		}
		else if(examId!=null){
			RuntimeExceptionDao<Exam, String> examDao=dbHelper.getExamDao();
			List<Exam> ExamList=examDao.queryForEq("NO", examId);
			Exam firstExam=ExamList.get(0);
			img_course_detail_avatar.setImageResource(R.drawable.red_dot);
			tv_course_detail_no.setText(firstExam.getNO());
			tv_course_detail_name.setText(firstExam.getName());
			tv_course_teacher.setText(firstExam.getTeacher());
			String examTimeLocation=WTDateParser.parseBeginAndEndTime(
					firstExam.getBegin(), firstExam.getEnd());
			examTimeLocation+=" "+firstExam.getLocation();
			tv_course_time_location.setText(examTimeLocation);
			tv_course_point.setText(String.valueOf(firstExam.getPoint()));
			tv_course_required.setText(firstExam.getRequired());
			tv_course_hours.setText(String.valueOf(firstExam.getHours()));
		}
	}
}
