package com.tcl.factory.view;

 
 
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;

import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.def.ProjectInfo;
import com.tcl.factory.thirdparty.ThirdParty;
 
public class DModeServerMenu extends SubMenu {
 
	private static final int SKD_PROJEC_ID_START=60000;
	private static final int SKD_PROJECT_ID_END=60100;
	private int areaIndex=0;
	private Context mDModeServerMenuContext;
	private int counter=0;
	
	public static  String MenuInfos_DModeServerMenu[][]={
		{"0-Project SN",""},
		{"1-EPolisy Number",""},
		{"2-Hardware",""},
		{"3-Software",""},
		
		{"4-projectName",""},
		{"5-frcVersion",""},
		{"6-stbcversion",""},
		
		{"7-Bootloader",""},
		{"8-Project ID",""},
		{"9-Panel ID",""},
		{"10-USB Update",""},
		{"11-Nonstandard",""},
		{"12-Non-standard_DTV",""},
		{"13-Suspend To Ram",""},
		{"14-Shop Mode",""},
		{"15-SKD Mode",""},
	};
	public 	DModeServerMenu(Context context)
	{
		mDModeServerMenuContext = context;
		super.menuInfos=MenuInfos_DModeServerMenu;
		super.menuLabel = "Main menu\\Server";
		super.setTAG();
		updateMenuInfos();		
	}


	public void KeyEvent(int keyCode,int pos)
	{
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_LEFT:
		case KeyEvent.KEYCODE_DPAD_RIGHT:
		{
			switch(pos)
			{
			case 8:
				setProjectID(keyCode);
				break;
			case 9:
				setPanelID(keyCode);
				break;
			case 13:
				setSTRstatus(keyCode);
				break;
			case 14:
				setShopModeStatus(keyCode);
				break;
			case 15:
				setSKDModeStatus(keyCode);
				break;
			}
		}
		break;
		default:
			break;
		}
	}
	
	public  boolean numKeyEvent(int  keyCode,int pos) {
		switch(pos)
		{
		case 5+3: 
			eventkey_str = eventkey_str + (keyCode - KeyEvent.KEYCODE_0);
			Log.i("jt", "numKeyEvent eventkey_str = " + eventkey_str);
			handler.postDelayed(runnable, DELAY_TIME_3S);
			return true;
		}
		return false;
	}

	private void PKGDownloadHandler(int keyCode) 
	{
		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT)
		{
			TV.writeS(TVDefine.PMode, DEFS.Disable);
			TV.writeS(TVDefine.DMode, DEFS.Disable);
//			Utils.intentHandler(DEFS.Package_MTK_PKGDownload);		 
		}
	}
	String eventkey_str = "";//eventkey_str + (keyCode - KeyEvent.KEYCODE_0);
	
	
	private void setProjectID() 
	{
		int projectID = 0;
		try {
			projectID = Integer.parseInt(eventkey_str);
		} catch(Exception e) {
			e.printStackTrace();
			return;
		}
		List projectIDList = ThirdParty.getProjectIDList();
		int size = projectIDList.size();
		for(int i=0; i<size; i++)
		{
			Log.i("jt","["+projectIDList.size()+"]["+projectIDList.get(i)+"]");
			if((Integer) projectIDList.get(i) == projectID) {
				TV.writeV(TVDefine.ProjectID,projectID);
				return;
			}
		}
		
	}
	private final int   DELAY_TIME_3S=1500;  
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			Log.i("jt", "handler eventkey_str = " + eventkey_str);
			setProjectID();
			MainMenu._Instance.updateListView(100);
			eventkey_str = "";
			handler.removeCallbacks(runnable);
			super.handleMessage(msg);
		}
	};
	
	private Runnable runnable = new Runnable()
	{
		public void run() 
		{
			Message message = Message.obtain();
			message.what = 1;
			handler.sendMessage(message);
		}
	};
	
	private void setSTRstatus(int keyCode)
	{
		if(ThirdParty.getSTRstatus())
		{
			ThirdParty.setSTRstatus(false);
		}
		else
		{
			ThirdParty.setSTRstatus(true);
		}	
	}
	
	private void setShopModeStatus(int keyCode)
	{
		Log.v(TAG , " wujy setShopModeStatus  ");
		if(ThirdParty.getShopModeStatus())
		{
			Log.v(TAG , " wujy setShopModeStatus 1111 ");
			ThirdParty.setShopModeStatus(false);
		}
		else
		{
			Log.v(TAG , " wujy setShopModeStatus 2222 ");
			ThirdParty.setShopModeStatus(true);
		}
	}
	
	private void setSKDModeStatus(int keyCode)
	{
		int project_id = ThirdParty.getProjectIDValue();
		if(ThirdParty.getSKDModeStatus())
		{
			ThirdParty.setSKDModeStatus(false);
		}
		else
		{
			if(project_id>=SKD_PROJEC_ID_START && project_id<=SKD_PROJECT_ID_END)
			{
				ThirdParty.setSKDModeStatus(true);
			}
		}
	}	


	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void updateMenuInfos() {
		// TODO Auto-generated method stub
//		short[] sn = TV.readD(TVDefine.SN);
//		for(int i=0;i<22;i++) {
//			Debug.v(TAG, "sn["+i+"]"+sn[i]);
//		}
		ProjectInfo.sn="";//Utils.short2String(sn);
		menuInfos[5+3][1]=""+ThirdParty.getProjectIDValue();
		menuInfos[0][1]=""+ThirdParty.getSNString();
		menuInfos[1][1]=""+ThirdParty.getRegiterCodeString();
		menuInfos[2][1]=""+ThirdParty.getHardwareVersion();
		menuInfos[3][1]=""+ThirdParty.getSoftwareVersion();
		menuInfos[4+3][1]=""+ThirdParty.getBootcodeVersion();
		
		menuInfos[4][1]=""+ThirdParty.getProjectName();
		menuInfos[5][1]=""+ThirdParty.getFrcCodeVersion();
		menuInfos[6][1]=""+ThirdParty.getStbcCodeVersion();
		
		if(ThirdParty.isSupportPanelID()){
			super.setItemEnable(9);
			menuInfos[9][1]=""+ThirdParty.getPanelIDValue();
		}else{
			super.setItemDisable(9);
			menuInfos[9][1]=""+"OFF";
		}
		
		if(ThirdParty.getSTRstatus()) {
			menuInfos[13][1]=""+"ON";
		} else {
			menuInfos[13][1]=""+"OFF";
		}
		
		if(ThirdParty.getShopModeStatus()) {
			menuInfos[14][1]=""+"ON";
		} else {
			menuInfos[14][1]=""+"OFF";
		}
		
		if(ThirdParty.getSKDModeStatus()) {
			menuInfos[15][1]=""+"ON";
		} else {
			menuInfos[15][1]=""+"OFF";
		}		
		
		MainMenu.updateDetailInfoLayer();
 	}
 
	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void enterSubMenu(int pos) {
		// TODO Auto-generated method stub
		super.enterSubMenu(pos);
		
		if(pos == 2){
			counter++;
			if(counter>=10)
			{
				MainMenu.mSubMenu = new DModeEmmcInfo();
			}
		}
		if (pos == 10)
			MainMenu.mSubMenu = new DModeUsbUpdate(mDModeServerMenuContext);
		else if (pos == 11)
			MainMenu.mSubMenu = new DModeNonstandard();
		else if(pos == 12)
			MainMenu.mSubMenu = new DModeNonstandardDTV();
	}

//	private void updateFrequenArea(int frequence) 
//	{
//	    short flage=0;
//		for(int i=0;i<DEFS.FreQuenceArray.length;i++)
//		{
//			if(frequence==DEFS.FreQuenceArray[i])
//			{   
//				flage=1;
//				areaIndex=i;break;			
//			}	    
//		}
//		if(flage==0)
//		{
//			areaIndex=0;
//			TV.writeV(TVDefine.AreaFrequence, DEFS.FreQuenceArray[0]); //default value
//		}
//		
//		menuInfos[7][1]=""+DEFS.FreQuenceArea[areaIndex]+" "+ TV.readV(TVDefine.AreaFrequence);
//	}
}
