package com.tcl.factory.view;
//import com.mediatek.tv.common.ConfigType;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PowerManager;
import android.os.PowerManager.WakeLock;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tcl.customerapi.R;
import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;


public class WarmUp extends Activity {
	private String TAG = this.getClass().getName();
	MyThread myHandler = new MyThread();
	TextView warpUpTime=null;
	private final int DELAY_TIME=1000;
	Thread thread=null;
	int timeSecond=0;
	int colorLable=0;
	RelativeLayout backGroundColor=null;
	WakeLock mWakeLock=null;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.fac_warmup);		
		warpUpTime=(TextView)findViewById(R.id.warm_up_time);
		backGroundColor=(RelativeLayout)findViewById(R.id.backgroud_layout);
		
		PowerManager pm = (PowerManager)getSystemService(Context.POWER_SERVICE);
		mWakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "warm_up");
		//backGroundColor.setBackgroundResource(R.color.red_color);
		
		backGroundColor.setBackgroundDrawable(null);
		thread=	new Thread(myHandler);
		thread.start();
		displayWarmUpInfo(0);
		if(MainMenu.mainArea != null) {
			MainMenu.mainArea.setVisibility(View.INVISIBLE);
		}
		//CommandExecute.setSoundMuteType(ConfigType.AUDIO_SPEAKER_OFF);
	}
	@Override
	 public void onAttachedToWindow(){
//		TV.APPKeyGuard(DEFS.Enable);
		super.onAttachedToWindow();
	 } 
	
	@Override
	protected void onStart() {
		Debug.v(TAG, "onStart");
		mWakeLock.acquire();//Acquire wakeLock before warmup
		TV.writeS(TVDefine.PowerKeyPassToApp, DEFS.Enable);// forbid power key and power key pass to app
		Intent intent = new Intent();
		intent.setClass(this, InfoService.class);//stop service  
		intent.putExtra("mode", 0);
		stopService(intent);
		super.onStart();
	}
	@Override
	protected void onDestroy() {
		Debug.v(TAG, "onDestroy-1102");
		mWakeLock.release();//Release wakeLock before warmup
//		backGroundColor.setBackgroundResource(R.color.transparent);
		TV.writeS(TVDefine.PowerKeyPassToApp, DEFS.Disable);// power key deal in framework
		TV.writeS(TVDefine.AudioMute, DEFS.Disable);
 		TV.writeS(TVDefine.WarmUP, DEFS.Disable);
		
		// add by wangst,20150526;MT5655������������������,�ϻ�����ʾ�����˵�;�����Ͳ���ok
		if(ThirdParty.isWarmupQuitApp()){
			Debug.v(TAG, "onDestroy-isWarmupQuitApp");
			if(MainMenu.mainArea != null) {
//				MainMenu.mainArea.setVisibility(View.VISIBLE);
				MainMenu._Instance.finish();
			}
		} else {
			if(MainMenu._Instance != null) {
				MainMenu._Instance.updateListView(100);
				MainMenu._Instance.updateView();
			}
		}
		
		Intent intent = new Intent();
		intent.setClass(this, InfoService.class);//solve it 
		intent.putExtra("mode", 0);
		startService(intent);
		super.onDestroy();
	}
	//�ϻ��˳�������ң�ء��������Menu??
	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		Debug.v(TAG , "dispatchKeyEvent : event.getAction() = " + event.getAction() + ", event.getKeyCode() = " + event.getKeyCode());
		int keyCode = event.getKeyCode();
		
		int cmd_data=(keyCode&0xff00)>>8;
		keyCode=keyCode&0x00ff;
		
		if(event.getAction() == KeyEvent.ACTION_DOWN){
			if(cmd_data!=0) {
				Debug.v(TAG , "FactoryIR cmd_data="+cmd_data+",keyCode=" + keyCode);
			//����ң��PW/Menu
				if (((cmd_data == 6) && (keyCode == 21))
						|| ((cmd_data == 0x7f) && (keyCode == KeyEvent.KEYCODE_HOME))
						|| ((cmd_data == 0x7f) && (keyCode == KeyEvent.KEYCODE_MENU)) // �ϻ���Ӧmeue��
						|| ((cmd_data == 5) && (keyCode == 63)))//this factory key value is warm off
				{
					ThirdParty.setWModeStatus(DEFS.Disable);
				}
				return true;
			}
//			if(keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//				ThirdParty.setWModeStatus(DEFS.Disable);
//				return true;
//			}
		}
		return true;
	}

	public void displayWarmUpInfo(int seconds){
		int hour=0, min=0, second=0, tmp=0;
		StringBuffer str=new StringBuffer("");

		hour=seconds/3600;	
		DEFS.WarmUPTime[1]=(short) hour;
		tmp= seconds%3600;
		min=tmp/60;
		DEFS.WarmUPTime[0]=(short) min;
		second=tmp%60;
		str=str.append("PW");	
		str=str.append("\n");
		str=str.append(intToString(hour));
		str=str.append(":");
		str=str.append(intToString(min));
		str=str.append(":");
		str=str.append(intToString(second));
//		str=str.append("-----------------------------------------").append("PW");	
//		str=str.append("-----------------------------------------").append("\n");
//		str=str.append("-----------------------------------------").append(intToString(hour));
//		str=str.append("-----------------------------------------").append(":");
//		str=str.append("-----------------------------------------").append(intToString(min));
//		str=str.append("-----------------------------------------").append(":");
//		str=str.append("-----------------------------------------").append(intToString(second));
		warpUpTime.setText(str);

	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			timeSecond++;
			colorLable++;
			if(colorLable==8)
				colorLable=0;
			displayWarmUpInfo(timeSecond);
			if(colorLable%2==  0)
			{	
				Debug.v("WarmUp", "TIME ="+timeSecond);
				int colorId;
				warpUpTime.setVisibility(View.VISIBLE);
				switch(colorLable)
				{
				case 0: colorId=R.color.blue_color;break;
				case 2: colorId=R.color.green_color;break;
				case 4: colorId=R.color.white_color;break;
				case 6: colorId=R.color.red_color;break;
				default:colorId=R.color.blue_color;
				}
//				backGroundColor.setBackgroundResource(colorId);
			}
			else warpUpTime.setVisibility(View.INVISIBLE);
			super.handleMessage(msg);
		}
	};
	public class MyThread implements Runnable{
		@Override
		public void run() {
			while (ThirdParty.getWModeStatus() && ThirdParty.getPModeStatus()) {
				try {
					Thread.currentThread().sleep(DELAY_TIME);//���̕�ͣ10�룬��λ���� CommondExecute.getWarmUpStatus()==ConfigType.COMMON_ON
					Message message=Message.obtain();
					message.what=1;
					handler.sendMessage(message);//�l����??
				    } 
				catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			ThirdParty.setWModeStatus(DEFS.Disable);
			//MainMenu.mainArea.setVisibility(View.VISIBLE);
			finish();
		}
	}

	public String intToString(int num)
	{
		if(num==0)
			return "00";
		else if(num<10)
			return "0"+num;
		else 
			return ""+num;
	}
	
	



}
