package com.tcl.factory.view;

import java.util.ArrayList;

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

import com.tcl.factory.thirdparty.ThirdParty;

 

public class PModePresetChannel extends SubMenu
{
	public static  String MenuInfos_PModePresetChannel[][]={
		{"Factory Area",""},
		{"Channel Preset",">"},
	};
	public static String AreaStr[]={"","","","","","","",""};  //max size 8
	
	private int modeType=0;
	// add by wangst,20150522;增加一个序号与频道号对应的list
	private ArrayList<Integer> channelList = new ArrayList<Integer>();
	public PModePresetChannel()
	{
		menuInfos=MenuInfos_PModePresetChannel;
		menuLabel = "Factory\\Channel PreSet";
		super.setTAG();
		modeType=0;
		menuInfos[0][1]="HZ";
		setChannelList();
		updateMenuInfos();
	}
	
	
	public void KeyEvent(int keyCode,int pos)
	{
		switch(keyCode)
		{
		case KeyEvent.KEYCODE_DPAD_LEFT:
		case KeyEvent.KEYCODE_DPAD_RIGHT:
		{
			switch(pos)
			{
				case 0:areaSelect(keyCode) ;break;
				case 1:channelPrestDo() ;break;
			}
		}
		break;
		default:
			break;
		}
	}
	private String runStatus[] = {">", "Waiting", "OK", "Fail"};
	private int statusIndex = 0;

	private void channelPrestDo() {
		if (null == channelList || modeType > channelList.size() - 1) {
			Log.d("PModePresetChannel", "channelList null-3");
			return;
		}
		// TV.writeV(TVDefine.ChannelPreSet, modeType);
		statusIndex = 1;
		MainMenu._Instance.updateListView(10);
		Message msgMessage = new Message();
		msgMessage.what = 0;
		handler.sendMessageDelayed(msgMessage, 500);
			
	}
	
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Message msgMessage = new Message();
			switch (msg.what) {
			case 0: 
				if(null == channelList || modeType > channelList.size() - 1){
					Log.d("PModePresetChannel", "channelList null-2");
					return;
				}
				boolean rs = (ThirdParty.channelPreSet(channelList.get(modeType)) == 0);
				if (rs) {
					statusIndex = 2;
				} else {
					statusIndex = 3;
				}
				msgMessage.what = 1;
				handler.sendMessageDelayed(msgMessage, 1000);
				break;
			case 1: 
				MainMenu._Instance.updateListView(10);
				msgMessage.what = 2;
				handler.sendMessageDelayed(msgMessage, 3000);
				break;
			case 2: 
				statusIndex = 0;
				MainMenu._Instance.updateListView(1500);
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 在初始化时，根据配置项，决定list中保存那些频道预置号
	 * 然后在切换的时候，就可以根据此list的下标找到频道预置号
	 * @author wangst
	 * @Date 20150522
	 */
	private void setChannelList(){
		int i=0;
		if(null != channelList || channelList.size() > 0){
			channelList.clear();
		}
		Log.d("PModePresetChannel", "setChannelList");
		channelList.add(0);
		channelList.add(1);
		channelList.add(2);
		channelList.add(3);
		channelList.add(4);
		channelList.add(5);
		channelList.add(6);
		if(ThirdParty.hasYNPresetChannel()){
			channelList.add(7);   // 越南
			AreaStr[0]="YN";
		}
		if(ThirdParty.hasTHPresetChannel()){
			channelList.add(8);   // 泰国
			AreaStr[1]="TH";
		}
		if(ThirdParty.hasPHPresetChannel()){
			channelList.add(9);   // 菲律宾
			AreaStr[2]="PH";
		}
		if(ThirdParty.hasINPresetChannel()){
			channelList.add(10);   // 印度尼西亚
			AreaStr[3]="IN";
		}
		if(ThirdParty.hasRUPresetChannel()){
			channelList.add(11);   // 俄罗斯
			AreaStr[4]="RU";
		}
		if(ThirdParty.hasCVTPresetChannel()){
			channelList.add(12);   // CVT
			AreaStr[5]="CVT";
		}
		if(ThirdParty.hasTNSPresetChannel()){
			channelList.add(13);   // TNS工厂频点
		}
		if(ThirdParty.hasEGPresetChannel()){
			channelList.add(14);   // 埃及工厂频点
		}	
		if(ThirdParty.hasINDPresetChannel()){
			channelList.add(18);   // 印度工厂频点
		}	
		if(ThirdParty.hasCOLPresetChannel()){
			channelList.add(20);   // 哥伦比亚工厂频点
		}	
	}
	
	private void areaSelect(int keyCode) {
		
		if(null == channelList || channelList.size() < 1){
			Log.d("PModePresetChannel", "channelList null-1");
			return;
		}
		String str="";
	    modeType=dataDeal(keyCode,modeType,channelList.size() -1);  
	    int number = channelList.get(modeType);
	    Log.d("PModePresetChannel", "number = " + number);
	    switch(number)
	    {
	       case 0:str="HZ";break;
	       case 1:str="PL";break;
	       case 2:str="NM";break;
	       case 3:str="WX";break;
	       case 4:str="Default";break;
	       case 5:str="ALG";break;
	       case 6:str="HZ_ODM";break;
	       case 7:str="YN";break;
	       case 8:str="TH";break;
	       case 9:str="PH";break;
	       case 10:str="IN";break;
	       case 11:str="RU";break;
	       case 12:str="CVT";break;
	       case 13:str="TNS";break;
	       case 14:str="EG";break;
	       case 15:str="MX";break;
	       case 18:str="IND";break;
	       case 20:str="COLUMBIA";break;
	    }
	    menuInfos[0][1]=str;
	    statusIndex = 0;
	}

	public void updateADCRuningState() {
		menuInfos[1][1] = runStatus[statusIndex];
	}
	 
	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void updateMenuInfos() {
		updateADCRuningState();
	}
	 
}
