package com.tcl.factory.view;


import java.util.List;

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.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;


public abstract class SubMenu 
{
	
	protected static String   TAG = "SubMenu"; 
	private  int menuPosition = 0;
	private  int[] DisableFocusePositon={1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
	protected String[][] menuInfos;
	protected String menuLabel;
	
	public static  String TVDefineName_CityID[]={
	"Ĭ��",
	"���ɹŵ���"
	};
	public static String TVDefineName_PowerMode[] = {
	"STANDBY",
	"ON",
	"LAST"
	};
	public static String TVDefineName_ColorTemperature[] = {
		"User",
	"Standard",
	"Cool",
	"Warm"
	};
	public static String TVDefineName_AudioMode[] = {
		"Music","Movies","News","Standard","User"};
	public static String TVDefineName_PictureMode[] = {
		"Standard","Vivid","Dynamic","Mild","User"};
	
	public static String TVDefineName_AudioFormat[]={
	"BG",
	"DK",
	"I",
	"M",
	"L"
	};
	public static String TVDefineName_AudioScene[]={
		"VOL_WALL",
	"VOL_DESKTOP"
	};

	public static String TVDefineName_DVBT2Mode[] = {
	"Off",
	"On",
	"By country"
	};

	public static String TVDefineName_LEDIndicatorMode[] = {
	"On",
	"Off",	
	"Breathing",	
	};
	/**
     * refresh the factory UI
     */
	public  abstract  void updateMenuInfos();
	
	
	/**
     * keyEvent handler
     */
	public  abstract  void KeyEvent(int  keyCode,int pos);
	
	/**
     * enter the submenu
     */
	protected void enterSubMenu(int pos)
	{
		setMenuPosition(pos);
		Debug.v(TAG, "enterSubMenu : pos = " + pos);
	}

	
	/**
     * set TAG to the class name
     */
	public void setTAG()
	{
		TAG = this.getClass().getName();
	}
	
	
	/**
     * @return the menu's name
     * 
     */
	public String getMenuLable()
	{
		return menuLabel;
	}
	
	
	/**
     * @return the menuInfos of the object that is using it
     */
	protected String[][]  getMenuInfos()
	{
		return menuInfos;
	}
	
	
	/**
     * @return  menuInfos 's length
     */
	protected int getMenuInfosLength()
	{
		return menuInfos.length;
	}
	
 
	/**
     * ���� Ӧ�ò˵�����һ�˵������λ??
     */
	protected void setMenuPosition(int pos)
	{
		menuPosition=pos;
	}
	
	
	/**
     * @return menu position that is selected
     */
	protected int  getMenuPosition()
	{
		return menuPosition;
	}

	
	/**
     * @return  the next display item's index
     */
	protected int getNextDisplayItem(int index) 
	{
		if(index>DisableFocusePositon.length||index<0)
		    return 1;
		return DisableFocusePositon[index];
	}
	
	
	/**
     * enable the selection of the menu's selection
     */
	protected void setItemEnable(int i) 
	{
		if(i>DisableFocusePositon.length||i<0)
	    return ;
		DisableFocusePositon[i]=1;
	}
	
	
	/**
     * disable the selection of the menu's selection
     */
	protected void setItemDisable(int i) 
	{
		if(i>DisableFocusePositon.length||i<0)
	    return ;
		DisableFocusePositon[i]=0;
	}

	protected void ValueHandler(int TVDefineID, int keyCode)
	{
		int value = TV.readV(TVDefineID);
		value = valueDeal(TVDefineID, keyCode, value);
		TV.writeV(TVDefineID, value);
		Debug.v(TAG , "ValueHandler : " + TVDefineID + " write value " + value);
	}
	protected void ValueHandler(int TVDefineID, int keyCode, int value2)
	{
		int value = TV.readV(TVDefineID, value2);
		value = valueDeal(TVDefineID, keyCode, value);
		TV.writeV(TVDefineID, value2, value);
		Debug.v(TAG , "ValueHandler : " + TVDefineID + " write value " + value + "value2" + value2);
	}
	protected void ValueHandler(int TVDefineID, int keyCode, int value2, int value3)
	{
		int value = TV.readV(TVDefineID, value2,  value3);
		value = valueDeal(TVDefineID, keyCode, value);
		TV.writeV(TVDefineID, value2, value3, value);
		Debug.v(TAG , "ValueHandler : " + TVDefineID + " write value " + value + "value2" + value2 + "value3" + value3);
	}

	protected void ValueHandler(int keyCode)
	{
		if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
			ThirdParty.preNonstandardDTV();
		} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
			ThirdParty.nextNonstandardDTV();
		}
	}

	protected void ValueHandlerEEPROM(int TVDefineID, int EEPROMAddress, int keyCode)
	{
		int value = 1;
		value = TV.readV(TVDefine.NVM, EEPROMAddress);
	    value = valueDeal(TVDefineID, keyCode, value);
		TV.writeV(TVDefineID, value);
		TV.writeV(TVDefine.NVM, EEPROMAddress, value);
		Debug.v(TAG , "ValueHandlerEEPROM : EEPROMAddress " + EEPROMAddress + " write " + value + "; TVDefineID:" + TVDefineID);
	}
	
	
	protected void StatusHandler( int TVDefineID )
	{
		if(TV.readS(TVDefineID))
		{
			TV.writeS(TVDefineID, DEFS.Disable);
			Debug.v(TAG , "StatusHandler : " + TVDefineID + " Disabled");
		}
		else
		{
			TV.writeS(TVDefineID, DEFS.Enable);
			Debug.v(TAG , "StatusHandler : " + TVDefineID + " Enabled");
		}
	}
	
	// TVDefineID = 0 : nothing to do with the TV, just change value of specified EEPROMAddress
		protected void StatusHandlerEEPROM(int TVDefineID, int EEPROMAddress)
		{
			if(TV.readV(TVDefine.NVM, EEPROMAddress) == DEFS.COMMON_ON)
			{
				TV.writeS(TVDefineID, DEFS.Disable);  // execute in time
				TV.writeV(TVDefine.NVM, EEPROMAddress, DEFS.COMMON_OFF);
				Debug.v(TAG , "StatusHandlerEEPROM : EEPROMAddress " + EEPROMAddress + " write 0; TVDefineID:" + TVDefineID + " disabled");
			}
			else
			{
				TV.writeS(TVDefineID, DEFS.Enable); 
				TV.writeV(TVDefine.NVM, EEPROMAddress, DEFS.COMMON_ON);
				Debug.v(TAG , "StatusHandlerEEPROM : EEPROMAddress " + EEPROMAddress + " write 1; TVDefineID:" + TVDefineID + " enabled");
			}
		}
		
	protected void updateStatusInfo(int TVDefineID, int index)
	{
		if(TV.readS(TVDefineID))	
			menuInfos[index][1]="< ON >"; 
		else 
			menuInfos[index][1]="< OFF >";
		Debug.v(TAG, "updateStatusInfo : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	// update menuInfo[index, 1] according to EEPROM
	protected void updateStatusInfoEEPROM(int EEPROMAddress, int index)
	{
		if(TV.readV(TVDefine.NVM, EEPROMAddress)==DEFS.COMMON_OFF)
			menuInfos[index][1]="< OFF >";
		else
			menuInfos[index][1]="< ON >";
		Debug.v(TAG, "updateStatusInfoEEPROM : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	protected void updateValueInfo(int TVDefineID, int index)
	{	
		int value = TV.readV(TVDefineID);
		menuInfos[index][1] = "< "+TVDefineID2Name(TVDefineID, value)+" >";
		Debug.v(TAG, "updateValueInfo : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	protected void updateValueInfo(int index)
	{	
		menuInfos[index][1] = "< "+ ThirdParty.getNonstandardDTV()+" >";
		Debug.v(TAG, "updateValueInfo : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	protected void updateValueInfo(int TVDefineID, int value1,int index)
	{	
		int value = TV.readV(TVDefineID,value1);
		menuInfos[index][1] = "< "+ TVDefineID2Name(TVDefineID, value)+" >";
		Debug.v(TAG, "updateValueInfo : menuInfos[" + index + ",1] " + menuInfos[index][1]);
		
	}	
	protected void updateValueInfo(int TVDefineID, int value1, int value2,int index)
	{	
		int value = TV.readV(TVDefineID,value1,value2);
		menuInfos[index][1] = "< "+ TVDefineID2Name(TVDefineID, value)+" >";
		Debug.v(TAG, "updateValueInfo : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	protected void updateValueInfoEEPROM(int EEPROMAddress, int index)
	{
		int value = TV.readV(TVDefine.NVM, EEPROMAddress);
		menuInfos[index][1] =  "< "+ TVDefineID2Name(EEPROMAddress, value)+" >";
		Debug.v(TAG, "updateValueInfoEEPROM : menuInfos[" + index + ",1] " + menuInfos[index][1]);
	}
	
	public  boolean numKeyEvent(int  keyCode,int pos) {
		return false;
	}
	
	protected int valueDeal(int TVDefineID, int keyCode, int value)
	{
		int max = -1;   // error
		int min = -1;   // error
		
		switch(TVDefineID)
		{
		case TVDefine.AudioScene:
			max = 1;
			min = 0;
			break;
		case TVDefine.NatureLightMode:
		case TVDefine.PowerMode:
		case TVDefine.AfterSaleMenu_DVBT2_mode:
		case TVDefine.StandbyLEDIndicatorMode:
			max = 2;
			min = 0;
			break;
		case TVDefine.AudioFormat:
			max = 3; 
			min = 0;
			break;
		case TVDefine.ColorTemperature:
		{	max = 3;
			min = 1;
		}
			break;
		case TVDefine.PictureMode:
		case TVDefine.Hotel_PictureMode:
		case TVDefine.AudioMode:
		case TVDefine.Hotel_SoundMode:
			max = 4; 
			min = 0;
			break;
		case TVDefine.Hotel_InputSource:
			max = 8; 
			min = 0;
			break;
		case TVDefine.InputSource:
			max = 7; 
			min = 0;
			break;
		case TVDefine.Brightness:
		case TVDefine.Contrast:
		case TVDefine.Color:
		case TVDefine.Sharpness:
		case TVDefine.BackLight:
			max = 100; // ?
			min = 0;
			break;
		case TVDefine.NatureLightAPL2:
			max = 255;
			min = 0;
			break;
		case TVDefine.NatureLightBP:
			max = 255; 
			min = 0;
			break;
			
//		case TVDefine.ProjectID:
//			max = DEFS.projectInfo.getProjectInfoSize(); 
//			min = 0;
//			break;
		case TVDefine.CityID:
			max = TVDefineName_CityID.length-1; 
			min = 0;
			break;
		case TVDefine.Hotel_PresetVolume:
			max = TV.readV(TVDefine.Hotel_MaxVolume);
			min = 0;
			break;
		case TVDefine.AudioVolume:
//			if(TV.readV(TVDefine.NVM, ConfigType.ADDR_Hotel_Flag) == DEFS.COMMON_ON)
//				max = TV.readV(TVDefine.NVM, ConfigType.ADDR_Hotel_MaxVolume); 
			max = TV.readV(TVDefine.AudioVolumeMax);
			Log.d("lyf", "valueDeal max="+max);
			min = 0;
			break;
		case TVDefine.MemorySpan:
		case TVDefine.LVDSSpan:  
			max = 100;
			min = 0;
			break;
		case TVDefine.MemoryStep:
		case TVDefine.LVDSStep:
			max = 100;
			min = 0;
			break;
		case TVDefine.Curve:
			max = 32767;
			min = -32768;
			break;
		case TVDefine.Overscan:
			max = 32767;
			min = -32768;
			break;
		case TVDefine.SSC_FRC_SPAN:
		case TVDefine.SSC_OSD_SPAN:
			max = 40;
			min = 0;
			break;
		case TVDefine.SSC_FRC_STEP:
		case TVDefine.SSC_OSD_STEP:
			max = 30;
			min = 0;
		case TVDefine.Hotel_MaxVolume:
		case TVDefine.AudioVolumeMax:
			max = 100;//35;
			min = 0;
			break;
		
		case TVDefine.RATIO_MIN:
			max = 255;
			min = 0;
			break;
		case TVDefine.ADC_MAX:
			max = 255;
			min = TV.readV(TVDefine.ADC_MIN);
			break;
		case TVDefine.ADC_MIN:		
			max = TV.readV(TVDefine.ADC_MAX);
			min = 0;
			break;
		default:
		{
//			max = 100;
//			min = 0;
			max = 32767;
			min = -32768;
		}
		}
 
		 
 
		if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
		{
			value--;
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
		{	
			value++;
		}
		Log.d("lyf", "valueDeal  = "+value);
	
		if(value > max) value = min;
		if(value < min) value = max;
		return value;
	}
	
	protected void setProjectID(int keyCode) 
	{
		int value = 0;
		int projectID = TV.readV(TVDefine.ProjectID);
		List projectIDList = ThirdParty.getProjectIDList();
		int max_size = projectIDList.size();
		Debug.v(TAG,"max_size = " + max_size);
		for(value=0;value<max_size;value++)
		{
			Debug.v(TAG,"["+projectIDList.size()+"]["+projectIDList.get(value)+"]");
		}
		for(value=0;value<max_size;value++)
		{
			Debug.v(TAG,"["+projectIDList.size()+"]["+projectIDList.get(value)+"]");
			if((Integer) projectIDList.get(value) == projectID)
				break;
		}
		if(value == max_size) value = 0;
		
		if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
		{
			value--;
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
		{
			value++;
		}
		if(value>=max_size)value = 0;
		else if(value<0) value = max_size-1;
		
		projectID = (Integer) projectIDList.get(value);
		TV.writeV(TVDefine.ProjectID,projectID);
		
	}
	
	protected void setPanelID(int keyCode) {
		int value = TV.readV(TVDefine.PanelID);
		int panelMaxID = TV.readV(TVDefine.PanelMaxID);
		Debug.v(TAG,"panelMaxID = " + panelMaxID);
		if(value > panelMaxID) {
			value = 1;
		}
		if(value < 0) {
			value = panelMaxID;
		}
		if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT)
		{
			value--;
		}
		else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)
		{
			value++;
		}
		if(value > panelMaxID){
			value = 1;
		}else if(value<0){
			value = panelMaxID;
		} 
		TV.writeV(TVDefine.PanelID,value);
		
	}
	
	private String TVDefineID2Name(int TVDefineID, int index)
	{
		String TVDefineName = "";
		Debug.d(TAG, "TVDefineID = " + TVDefineID +";TVDefineID2Name idnex = " + index);
 		switch(TVDefineID)
		{
		case TVDefine.PictureMode:
			if(index >= TVDefineName_PictureMode.length){
				index = TVDefineName_PictureMode.length - 1;
			}
			TVDefineName = TVDefineName_PictureMode[index]; 
			break;
		case TVDefine.Hotel_PictureMode:
			if(index >= TVDefineName_PictureMode.length){
				index = TVDefineName_PictureMode.length - 1;
			}
			TVDefineName = TVDefineName_PictureMode[index]; 
			break;
		case TVDefine.Hotel_SoundMode:
			if(index >= TVDefineName_AudioMode.length){
				index = TVDefineName_AudioMode.length - 1;
			}
			TVDefineName = TVDefineName_AudioMode[index]; 
			break;
		case TVDefine.AudioMode:
			index = index -1;
			if(index >= TVDefineName_AudioMode.length){
				index = TVDefineName_AudioMode.length - 1;
			}
			TVDefineName = TVDefineName_AudioMode[index]; 
			break;
		case TVDefine.ColorTemperature:
			if(index >= TVDefineName_ColorTemperature.length){
				index = TVDefineName_ColorTemperature.length - 1;
			}
			TVDefineName = TVDefineName_ColorTemperature[index]; 
			break;
 		case TVDefine.PowerMode:
 			if(index >= TVDefineName_PowerMode.length){
				index = TVDefineName_PowerMode.length - 1;
			}
			TVDefineName = TVDefineName_PowerMode[index];	
			break;
		case TVDefine.CityID:
			if(index>=TVDefineName_CityID.length)index=TVDefineName_CityID.length-1;
			TVDefineName = TVDefineName_CityID[index];
			break;
		case TVDefine.AudioFormat:
			if(index>=TVDefineName_AudioFormat.length){
				index=TVDefineName_AudioFormat.length-1;
			}
			TVDefineName = TVDefineName_AudioFormat[index]; 
			break;
		case TVDefine.AudioScene:
			if(index >= TVDefineName_AudioScene.length){
				index = TVDefineName_AudioScene.length - 1;
			}
			TVDefineName = TVDefineName_AudioScene[index];
			break;
		case TVDefine.AudioBalance:   // (-50,50)
			TVDefineName = "" + (index - 50);
			break;
		case TVDefine.ChannelPreSet:
			TVDefineName = "CH:"+index;
			break;
 		case TVDefine.AfterSaleMenu_DVBT2_mode:
 			if(index >= TVDefineName_DVBT2Mode.length){
				index = TVDefineName_DVBT2Mode.length - 1;
			}
			TVDefineName = TVDefineName_DVBT2Mode[index];	
			break;		
 		case TVDefine.StandbyLEDIndicatorMode:
 			if(index >= TVDefineName_LEDIndicatorMode.length){
				index = TVDefineName_LEDIndicatorMode.length - 1;
			}
			TVDefineName = TVDefineName_LEDIndicatorMode[index];	
			break;	
		default:
				TVDefineName = "" + index;
		}
		return TVDefineName;
	}
	
	
	
	public int dataDeal(int keyCode,int typeValue,int range){
		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
			typeValue++;
			if(typeValue>range)
				typeValue=0;
		}
		else{	
			typeValue--;
			if(typeValue<0)
				typeValue=range;
		}
		return typeValue;
	}
	
	public int dataDeal(int keyCode,int typeValue,int mix,int max){
		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
			typeValue++;
			if(typeValue>max)
				typeValue=mix;
		}
		else{	
			typeValue--;
			if(typeValue<mix)
				typeValue=max;
		}
		return typeValue;
	}
	
	
	
	
	
}
