
package com.tcl.factory.view;
 
//import com.tcl.fac.function.CommandExecute;
//import com.tcl.fac.menu.MainMenu;
//import com.tcl.fac.menu.PmodePicRelate;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import android.view.KeyEvent;

import com.tcl.factory.FactoryManager;
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;
//haha
public class PModeWhiteBalanceMenu extends SubMenu {
 
	private final String TAG = "WhiteBalance";
	public static  String MenuInfos_PModeWhiteBalanceMenu[][]={
		{"Source",""},
		{"Color",""},
		{"R gain",""},
		{"G gain",""},
		{"B gain",""},
		{"R offset",""},
		{"G offset",""},
		{"B offset",""},
		{"White Balance init",">"},
		{"LoadDefaultGamma",">"}
//		{"Pic.Enhance",""},
//		{"Picture related",""}
	};
	
	public static  String MenuInfos_PModeWhiteBalanceMenuGamma[][]={
		{"Source",""},
		{"Color",""},
		{"R gain",""},
		{"G gain",""},
		{"B gain",""},
		{"R offset",""},
		{"G offset",""},
		{"B offset",""},
		{"White Balance init",">"},
		{"LoadDefaultGamma",">"},
		{"Gamma",""}
	};
	

	public PModeWhiteBalanceMenu()
	{
	  TV.setSource(DEFS.SOURCE_HDMI, 1);
	  if(ThirdParty.isWhiteBalanceShowGamma()){
		  menuInfos = MenuInfos_PModeWhiteBalanceMenuGamma;
	  } else{
		  menuInfos = MenuInfos_PModeWhiteBalanceMenu;
	  }
	  menuLabel = "Factory\\WhiteBalance";
	  super.setTAG();

	  updateMenuInfos();   
	}
	private int sourcetype=0;
	private int currentTmpType=0;

	public void KeyEvent(int keyCode,int pos){
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_LEFT:
		case KeyEvent.KEYCODE_DPAD_RIGHT:
		{
			switch(pos)
			{
			/*case 0: changeNextInputSource(keyCode);break;*///whiteBalance only used on HDMI
			case 1: changeColorType(keyCode);break;
			case 2: Rgain(keyCode);break;
			case 3: Ggain(keyCode);break;
			case 4: Bgain(keyCode);break;
			case 5:	Roffset(keyCode);break;
			case 6:	Goffset(keyCode);break;
			case 7:	Boffset(keyCode);break;
			case 8:	WhiteBalanceInit();break;
			case 9: ThirdParty.loadDefaultGamma();break;
	//		case 9:	picEhanceStatus();break;
	//		case 10:MainMenu.mSubMenu=new PmodePicRelate(); break;
			}
		}
		break;
		default:
			break;
		}
	}

	private void WhiteBalanceInit() 
	{
		//TV.writeV(TVDefine.WhiteBalance, 0);
		ThirdParty.whiteBalanceInit();
 	}

	private void Boffset(int keyCode) 
	{		
		int value=TV.readV(TVDefine.BGainOffset) ;
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.BGainOffset, value);
	}
	private void Goffset(int keyCode) 
	{
		int value=TV.readV(TVDefine.GGainOffset) ;
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.GGainOffset, value);
	}
	private void Roffset(int keyCode) {
		int value=TV.readV(TVDefine.RGainOffset) ;
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.RGainOffset, value);
	}
	private void Bgain(int keyCode)
	{	
		int value=TV.readV(TVDefine.BGain);
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.BGain, value);
 	
	}
	private void Ggain(int keyCode) 
	{
		int value=TV.readV(TVDefine.GGain);
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.GGain, value);
	}
	private void Rgain(int keyCode) 
	{
		int value=TV.readV(TVDefine.RGain);
		value=dataDeal(value,keyCode);
		TV.writeV(TVDefine.RGain, value);
	}

	public void  updateCurrInputColorTmp(){
		String str="";		
		currentTmpType=TV.readV(TVDefine.ColorTemperature);
		if(currentTmpType==DEFS.TMP_USER)
		{
			currentTmpType=DEFS.TMP_STANDARD;
			TV.writeV(TVDefine.ColorTemperature, currentTmpType);
		}
		switch(currentTmpType)
		{
		case DEFS.TMP_USER:str="user"; break;
		case DEFS.TMP_STANDARD:str="normal"; break;
		case DEFS.TMP_COOL:str="cold"; break;
		case DEFS.TMP_WARM:str="warm"; break;
		case DEFS.TMP_WARM2:str="warm2";break;
		}
		
		currentTmpType=currentTmpType;
		menuInfos[1][1]=str;
	}

	public void  updateCurrentInputSource(){
		menuInfos[0][1]=TV.getSourceType();
		String str=menuInfos[0][1];
		if(str.contains("ATV")){
			sourcetype=0;
		}
		else if(str.contains("AV")){
			sourcetype=1;
		}
		else if(str.contains("YPBPR")){
			sourcetype=2;
		}
		else if(str.contains("VGA")){
			sourcetype=3;
		}
		else if(str.contains("HDMI")){
			sourcetype=4;
		}
		else {
			TV.setSource(DEFS.SOURCE_HDMI, 1);
			sourcetype=4;
		}

//		currentSourceType=TV.getSourceType();
	}


	
	void changeNextInputSource(int keyCode){		

		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
			sourcetype++;
			if(sourcetype>4)
				sourcetype=0;
		}
		else{
			sourcetype--;
			if(sourcetype<0)
				sourcetype=4;
		}
		switch(sourcetype){
		
		case 0:TV.setSource(DEFS.SOURCE_ATV, 1);break;
		case 1:TV.setSource(DEFS.SOURCE_AV, 1);break;
		case 2:TV.setSource(DEFS.SOURCE_YPBPR, 1);break;
		case 3:TV.setSource(DEFS.SOURCE_VGA, 1);break;
		case 4:TV.setSource(DEFS.SOURCE_HDMI, 1);break;
		}
	}
	
	void changeColorType(int keyCode){
		int typeValue=0;
		typeValue=TV.readV(TVDefine.ColorTemperature);

		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
			typeValue++;
			if(typeValue>4)
				typeValue=1;
		}
		else{	
			typeValue--;
			if(typeValue<1)
				typeValue=4;
		}
		TV.writeV(TVDefine.ColorTemperature, typeValue);
	}
	private int dataDeal(int mValue,int EventCode){
		if(EventCode==KeyEvent.KEYCODE_DPAD_LEFT){
			mValue--;
		}
		else if(EventCode == KeyEvent.KEYCODE_DPAD_RIGHT){	
			mValue++;
		}

		if(currentTmpType==DEFS.TMP_STANDARD && TV.getSourceType().equalsIgnoreCase("HDMI")) {
			if(mValue>255)mValue = 0;
			else if(mValue<0)mValue = 255;
		}
		else {
			if(mValue>127)mValue = -128;
			else if(mValue<-128)mValue = 127;
		}
		
		return mValue;
	}

	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void updateMenuInfos() {
		// TODO Auto-generated method stub
		updateCurrentInputSource();
		updateCurrInputColorTmp();
				
		menuInfos[2][1]=""+TV.readV(TVDefine.RGain);
		menuInfos[3][1]=""+TV.readV(TVDefine.GGain);
		menuInfos[4][1]=""+TV.readV(TVDefine.BGain);
//		super.setItemDisable(5);
		menuInfos[5][1]=""+TV.readV(TVDefine.RGainOffset);
//		super.setItemDisable(6);
		menuInfos[6][1]=""+TV.readV(TVDefine.GGainOffset);
//		super.setItemDisable(7);
		menuInfos[7][1]=""+TV.readV(TVDefine.BGainOffset);
		if(ThirdParty.isWhiteBalanceShowGamma()){
			menuInfos[10][1]="" + getGammaOffset();
		}
		super.setItemDisable(5);
		super.setItemDisable(6);
		super.setItemDisable(7);
	}

	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void enterSubMenu(int pos) 
	{
		// TODO Auto-generated method stub
		super.enterSubMenu(pos);
 		switch(pos)
		{
//		case 10:MainMenu.mSubMenu=new PModePICRelated(); break;
		}
 	}
	
	private String getGammaOffset(){
		FactoryManager mFactory = FactoryManager.getInstance(MainMenu._Instance);
		int value = -1;
		value = mFactory.doGetGammaOffset();
		return String.valueOf(value);
	}

}