package com.tcl.factory.ir;

import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;

import com.tcl.factory.bsp.TCL;
import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.commands.FactoryCommandEvent;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;

public class IRManagerListener {

	private static String TAG = "IRManagerListener";
	private static int CMD_PANELKEY = 0x7f;

	public static void parserEvent(int cmd_data,int keyCode) {
		// if(keyCode == KeyMap.KEYCODE_P_ON)
		// {
		// TV.writeS(TVDefine.PMode, DEFS.Enable);
		// return;
		// }
		// if(keyCode==KeyMap.KEYCODE_P_OFF)
		// {
		// TV.writeS(TVDefine.PMode, DEFS.Disable);
		// TV.writeS(TVDefine.DMode, DEFS.Disable);
		// SerialManager.mUiSLsner.onChange(DEFS.EXIT_APP);
		// return;
		// }
		if (true) {
			// keyCode=TV.readV(TVDefine.SidePanelKeyCode)&0xffff;
			// int cmd_data=(keyCode&0xff00)>>8;
			// keyCode=keyCode&0x00ff;
			Log.d("lyf", " cmd_data =" + cmd_data);
			Log.d("lyf", " keyCode =" + keyCode);
			if (0 == cmd_data) { // panelKey
				switch (keyCode) {
				case KeyEvent.KEYCODE_DPAD_UP:
					break;
				case KeyEvent.KEYCODE_DPAD_DOWN:
					break;
				case KeyEvent.KEYCODE_DPAD_RIGHT:
					break;
				case KeyEvent.KEYCODE_DPAD_LEFT:
					break;
				case KeyEvent.KEYCODE_DPAD_CENTER:
					break;
				case KeyEvent.KEYCODE_POWER:
					break;
				case KeyEvent.KEYCODE_HOME:
					break;
				}
			}

			switch (cmd_data) {
			
			case FactoryCommandEvent.FACTORY_SET_TARGET_INFO:
				ThirdParty.setTargetInfoForTV(0,0,0,1);
				break;
			
			// case 0x05:
			case FactoryCommandEvent.FACTORY_HOTKEY_CTRL_CODE:
				switch (keyCode) {
				case 1:
					TV.writeS(TVDefine.PMode, DEFS.Enable);
					TCL.setInfoDisServiceEnable(DEFS.Enable);
					break;
				case 0:
					TV.writeS(TVDefine.PMode, DEFS.Disable);
					TV.writeS(TVDefine.DMode, DEFS.Disable);
					TCL.setFacInfoShowEnable(DEFS.Disable);
					TCL.setInfoDisServiceEnable(DEFS.Disable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_WB_ENV_INIT_CODE:
				TV.writeV(TVDefine.WhiteBalance);
				break;
			case FactoryCommandEvent.FACTORY_SET_SCREEN_MODE_CODE:
				int mode = TV.readV(TVDefine.PictureFormat);
				mode--;
				if (mode < 1)
					mode = 2;
				TV.writeV(TVDefine.PictureFormat, mode);
				break;
			case FactoryCommandEvent.FACTORY_GAMMA_TABLE_CHECK:
				TCL.ToastGammaTableCheckResult(keyCode);
				break;
			// case 0x06:
			case FactoryCommandEvent.FACTORY_WARM_UP_CTRL_CODE:
				switch (keyCode) {
				case 1:
					TV.writeS(TVDefine.KeyCodeTest, DEFS.Disable);
					TV.writeS(TVDefine.WarmUP, DEFS.Enable);
					break;
				case 0:
					TV.writeS(TVDefine.WarmUP, DEFS.Disable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_KEY_TEST_CTRL_CODE:
				switch (keyCode) {
				case 1:
					TV.writeS(TVDefine.KeyCodeTest, DEFS.Enable);
					break;
				case 0:
					TV.writeS(TVDefine.KeyCodeTest, DEFS.Disable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_DEVICE_ID_TEST_CODE:
				TV.readS(TVDefine.DeviceID);
				break;
			case FactoryCommandEvent.FACTORY_PC_LINK_TEST_CODE:
				if (TV.readS(TVDefine.NetworkConnectivity)) {
					TCL.getPcLinkStatus(false);
				} else {
					TCL.setWifiLink();
				}
				break;
			case FactoryCommandEvent.FACTORY_RESET_SHOP_CODE:
				TV.writeV(TVDefine.ShopInit);
				break;
			case FactoryCommandEvent.FACTORY_RESET_ALL_CODE:
				// Context mContext = null;
				// Toast.makeText(mContext, "NVM and ShutDown", 3000).show();
				TV.writeV(TVDefine.NVMReSet);
				break;
			case FactoryCommandEvent.FACTORY_SOUNT_TEST_CTRL_CODE:
				if(DEFS.RATTLE_ON_OFF ==0 ){
					TV.setSource(DEFS.SOURCE_ATV, 0);
					TV.writeV(TVDefine.AudioMode, 1);
					TV.writeV(TVDefine.AudioBalance, 0);
					TV.writeV(TVDefine.ChannelSet, 0);
				} else {
					TV.writeV(TVDefine.ChannelSet, 1);
				}
				break;
			case FactoryCommandEvent.FACTORY_SET_AUDIO_BALANCE_CODE:
				//TV.writeV(TVDefine.AudioBalance, keyCode);//工厂取消该功能
				break;
			// case 0x08:
			case FactoryCommandEvent.FACTORY_SELECT_SRC_AV_CODE:
				switch (keyCode) {
				case 0:
					TV.setSource(DEFS.SOURCE_AV, 1);
					break;
				case 1:
					TV.setSource(DEFS.SOURCE_AV, 2);
					break;
				// case 3:
				// break;
				// case 4:
				// break;
				}
				break;
			case FactoryCommandEvent.FACTORY_SELECT_SRC_YPBPR_CODE:
				switch (keyCode) {
				case 0:
					TV.setSource(DEFS.SOURCE_YPBPR, 1);
					break;
				case 1:
					TV.setSource(DEFS.SOURCE_YPBPR, 2);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_SELECT_SRC_VGA_CODE:
				TV.setSource(DEFS.SOURCE_VGA, 1);
				break;
			case FactoryCommandEvent.FACTORY_SELECT_SRC_HDMI_CODE:
				switch (keyCode) {
				case 0:
					TV.setSource(DEFS.SOURCE_HDMI, 1);
					break;
				case 1:
					TV.setSource(DEFS.SOURCE_HDMI, 2);
					break;
				case 2:
					TV.setSource(DEFS.SOURCE_HDMI, 3);
					break;
				case 3:
					TV.setSource(DEFS.SOURCE_HDMI, 4);
					break;

				default:
					break;
				}

				break;
			case FactoryCommandEvent.FACTORY_SELECT_SRC_USB_CODE:
				ThirdParty.selectSrcUSB();
				break;
			case FactoryCommandEvent.FACTORY_SRVC_MENU_CTRL_CODE:
//				Toast.makeText(ThirdParty.mContext, "FACTORY_MENU_CTRL_CODE="+keyCode + "::" +MainMenu._Instance, 3000).show();
				switch (keyCode) {
				case 0:
					TCL.setSerInfoShowEnable(DEFS.Disable);
//					TCL.setFacInfoShowEnable(DEFS.Disable);
					break;
				case 1:
					TCL.setSerInfoShowEnable(DEFS.Enable);
//					TCL.setFacInfoShowEnable(DEFS.Enable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_MUTE_CTRL_CODE:
				switch (keyCode) {
				case 0:
					ThirdParty.setAudioMuteStatusVisible(DEFS.Disable);
					break;
				case 1:
					ThirdParty.setAudioMuteStatusVisible(DEFS.Enable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_MICROPHONE_CTRL_CODE:
				switch (keyCode) {
				case 0:
					TCL.AsyncEnableKaraoke(DEFS.COMMON_OFF);
					break;
				case 1:
					TCL.AsyncEnableKaraoke(DEFS.COMMON_ON);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_SET_COLOR_TMP_CODE:
				int typeValue = TV.readV(TVDefine.ColorTemperature);
				typeValue--;
				if (typeValue < 1)
					typeValue = 3;
				TV.writeV(TVDefine.ColorTemperature, typeValue);
				break;
			case FactoryCommandEvent.FACTORY_3D_MODE_CTRL_CODE:
				switch (keyCode) {
				case 0:
					TV.writeS(TVDefine.ThreeDMode, DEFS.Disable);
					break;
				case 1:
					TV.writeS(TVDefine.ThreeDMode, DEFS.Enable);
					break;
				}
				break;

			case FactoryCommandEvent.FACTORY_PANEL_CHECK_CTRL_CODE:
				switch (keyCode) {
				case 0:
					TCL.setTestPatternStatus(false);
					break;
				case 1:
					TCL.setTestPatternStatus(true);
					break;
				}
				break;
			// case 0x09:
			case FactoryCommandEvent.FACTORY_SET_VOLUME_CODE:
				if(keyCode == 0) {
					TV.writeV(TVDefine.AudioVolume, keyCode);
					exec("input keyevent KEYCODE_VOLUME_DOWN");
				} else if(keyCode == 1) { 
					TV.writeV(TVDefine.AudioVolume, keyCode + 1);
					exec("input keyevent KEYCODE_VOLUME_DOWN");
				} else if(keyCode == 100) {
					TV.writeV(TVDefine.AudioVolume, keyCode);
					exec("input keyevent KEYCODE_VOLUME_UP");
				} else {
					TV.writeV(TVDefine.AudioVolume, keyCode - 1);
					exec("input keyevent KEYCODE_VOLUME_UP");
				}
				break;
			// 0x11
			case FactoryCommandEvent.FACTORY_SELECT_ANTENNA_PRGRM_CODE:
				if (!TV.getSourceType().equalsIgnoreCase(DEFS.ATV))
					TV.setSource(DEFS.SOURCE_ATV, 0);
				TV.setATVChannel(keyCode);
				break;
			// 0x12
			case FactoryCommandEvent.FACTORY_SELECT_CABLE_PRGRM_CODE:
				if (!TV.getSourceType().equalsIgnoreCase(DEFS.ATV))
					TV.setSource(DEFS.SOURCE_ATV, 0);
				TV.setATVChannel(keyCode);
				break;
			case FactoryCommandEvent.FAC_DATA_PICMODE_STANDARD:
				ThirdParty.setPicMode(ThirdParty.PM_NORMAL);
				break;
			case FactoryCommandEvent.FAC_DATA_PICMODE_SOFT:
				ThirdParty.setPicMode(ThirdParty.PM_MILD);
				break;
			case FactoryCommandEvent.FAC_DATA_PICMODE_BRIGHT:
				ThirdParty.setPicMode(ThirdParty.PM_VIVID);
				break;
			case FactoryCommandEvent.FAC_DATA_PICMODE_PERSONAL:
				ThirdParty.setPicMode(ThirdParty.PM_USER);
				break;
			case FactoryCommandEvent.FACTORY_SELECT_SRC_ATV_CODE:
				TV.setSource(DEFS.SOURCE_ATV, 0);
				break;
			case FactoryCommandEvent.FACTORY_MENU_CTRL_CODE:
//				Toast.makeText(ThirdParty.mContext, "FACTORY_MENU_CTRL_CODE="+keyCode + "::" +MainMenu._Instance, 3000).show();
				switch (keyCode) {
				case 0:
					TCL.setFacInfoShowEnable(DEFS.Disable);
					break;
				case 1:
					TCL.setFacInfoShowEnable(DEFS.Enable);
					break;
				}
				break;
			case FactoryCommandEvent.FACTORY_USB_CLONE_CODE:
					TV.writeV(TVDefine.USB2NVM,0);
				break;
			case 292:
			case 306:
				break;
			default:
				break;
			}
		}
	}
	
	private static void exec(final String cmd) {
		new Thread(new Runnable() {

			@Override
			public void run() {
				ThirdParty.setRootSeek(cmd);
			}
		}).start();
	}

	public interface GetIRCodeListener {
		void onChange(int data);
	}

	public static GetIRCodeListener mGetIRCodeListener;

	public static void addListener(GetIRCodeListener getInsListener) {
		mGetIRCodeListener = getInsListener;

	}

}
