
package com.tcl.factory.view;
 
//import com.tcl.fac.function.CommandExecute;
//import com.tcl.fac.menu.MainMenu;
//import com.tcl.fac.menu.PmodePicRelate;
import android.util.Log;
import android.view.KeyEvent;

import com.tcl.factory.bsp.TV;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;
//haha
public class ParamSettingADC extends SubMenu {
 
	public static  String MenuInfos_ParamSettingADC[][]={
		{"Source",""},
		{"R gain",""},
		{"G gain",""},
		{"B gain",""},
		{"R offset",""},
		{"G offset",""},
		{"B offset",""},
		{"setAutoADCColor",">"},
		{"ADC status",""},
	};
	
	private String runStatus[] = {">", "Waiting", "OK", "Fail"};
	private String rsStatus[] = {"Fail", "OK", "NG"};
	private int statusIndex = 0;
	private int rsStatusIndex = 2;
	
	public ParamSettingADC()
	{
	  TV.setSource(DEFS.SOURCE_YPBPR, 1);
	  
	  menuInfos=MenuInfos_ParamSettingADC;
	  menuLabel = "Factory\\ParamSettingADC";
	  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;
			case 7:	setAutoADCColor();break;
			}
		}
		break;
		default:
			break;
		}
	}
	
	private void setAutoADCColor() {
//		final ProgressDialog progressDialog = ProgressDialog.show(ThirdParty.mContext, "Loading...", "Please wait...", true, false);
//		new Thread(new Runnable() {
//
//			@Override
//			public void run() {
//				
//				boolean rs = ThirdParty.setAutoADCColor();
//				
//				try {
//					Thread.currentThread().sleep(1000);
//				} catch (InterruptedException e) {
//					e.printStackTrace();
//				}
//				progressDialog.dismiss();
//				if(rs) {
//					Toast.makeText(ThirdParty.mContext, "Success!",Toast.LENGTH_SHORT).show();
//				} else {
//					Toast.makeText(ThirdParty.mContext, "Fail!",Toast.LENGTH_SHORT).show();
//				}
//			}
//		}).start();
		new Thread(new Runnable() {

			@Override
			public void run() {
				statusIndex = 1;
				Log.i("fadc", "begin...");
				boolean rs = ThirdParty.setAutoADCColor();
				Log.i("fadc", "result = ." + rs);
				
				if(rs) {
//					rsStatusIndex = 1;
					statusIndex = 2;
				} else {
//					rsStatusIndex = 0;
					statusIndex = 3;
				}
				Log.i("fadc", "statusIndex = ." + statusIndex);
				MainMenu._Instance.updateListView(100);
				try {
					Thread.currentThread().sleep(500);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				statusIndex = 0;
				MainMenu._Instance.updateListView(100);
			}
		}).start();
	}

	public void  updateCurrentInputSource(){
		menuInfos[0][1]=TV.getSourceType();
		String str=menuInfos[0][1];
		if(str.contains("YPBPR")){
			sourcetype=0;
		}
		else if(str.contains("VGA")){
			sourcetype=1;
		}
		else {
			sourcetype=0;
		}
	}

	void changeNextInputSource(int keyCode){		

		if(keyCode==KeyEvent.KEYCODE_DPAD_RIGHT){
			sourcetype++;
			if(sourcetype>1)
				sourcetype=0;
		}
		else{
			sourcetype--;
			if(sourcetype<0)
				sourcetype=1;
		}
		switch(sourcetype){
		
		case 0:TV.setSource(DEFS.SOURCE_YPBPR, 1);break;
		case 1:TV.setSource(DEFS.SOURCE_VGA, 1);break;
		}
	}
	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) {
			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();
				
		super.setItemDisable(1);
		super.setItemDisable(2);
		super.setItemDisable(3);
		super.setItemDisable(4);
		super.setItemDisable(5);
		super.setItemDisable(6);
		super.setItemDisable(8);
		menuInfos[1][1]=""+ThirdParty.getAutoADCColorRGain();
		menuInfos[2][1]=""+ThirdParty.getAutoADCColorGGain();
		menuInfos[3][1]=""+ThirdParty.getAutoADCColorBGain();
		menuInfos[4][1]=""+ThirdParty.getAutoADCColorROffset();
		menuInfos[5][1]=""+ThirdParty.getAutoADCColorGOffset();
		menuInfos[6][1]=""+ThirdParty.getAutoADCColorBOffset();
		
		updateADCRuningState();
		
		updateADCState();
	}
	
	public void updateADCRuningState() {
		
		menuInfos[7][1] = runStatus[statusIndex];
	}
	
	public void updateADCState() {
		rsStatusIndex = ThirdParty.getAutoADCStatus();
		menuInfos[8][1] = rsStatus[rsStatusIndex];
	}

	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void enterSubMenu(int pos) 
	{
		// TODO Auto-generated method stub
		super.enterSubMenu(pos);
 		switch(pos)
		{
		}
 	}

}