package com.tcl.factory.view;

import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.Debug;

import android.app.AlertDialog;
import android.content.ComponentName;
import android.content.DialogInterface;
import android.content.Intent;
import android.tclwidget.TCLAlertDialog;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
 
public class DModeAfterSaleMenu extends SubMenu
{
	private final static String TAG = "DModeAfterSaleMenu";
	public static  String MenuInfos_DModeAfterSaleMenu[][]={
		{"GetUserID",""},
		{"BGM","On"},
		{"NVM Reset",">"},
		{"Reset Shop",">"},
		{"NRM","OFF"},
		{"DVB-T2 and country...","On"},
		{"HbbTV mode","On"},
		{"Serial IO Ctrl","On"},
		{"Running time",""},
		{"HbbTv portal",">"},
		{"Reset All",">"},
        {"Remote diagnosis",">"},		
	};
	private Context mContext;
	public DModeAfterSaleMenu(Context context)
	{
		mContext = context;
		super.menuInfos=MenuInfos_DModeAfterSaleMenu;
		super.menuLabel = "Service menu";
		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 0:
				if(!ThirdParty.mFactoryAdapter.getGetUserIDStatus()){ 
					if(!ThirdParty.mFactoryAdapter.setGetUserIDStatus(false)){
						Toast.makeText(mContext, "get User ID fail!", Toast.LENGTH_SHORT).show();
					}
				} 
				break;			
			case 1:
				super.StatusHandler(TVDefine.AfterSaleMenu_BGM) ; 
				break;		
			case 2:
				resetConfirmDialog(0);
				break;	
			case 3:
				resetConfirmDialog(2);
				break;
			case 4:{
				super.StatusHandler(TVDefine.AfterSaleMenu_NMR) ; 
				break;
			}			
			case 5: {
				int value = TV.readV(TVDefine.AfterSaleMenu_DVBT2_mode);
				if(keyCode==KeyEvent.KEYCODE_DPAD_LEFT){
					value--;
				}
				else if(keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){	
					value++;
				}
				if(value>2)
					value=0;
				else if(value<0)
					value=2;
				
				TV.writeV(TVDefine.AfterSaleMenu_DVBT2_mode,value);
				break;
			}
			case 6:
				super.StatusHandler(TVDefine.AfterSaleMenu_HbbTV_mode) ; 
				break;	
				
			case 7:
				if(!ThirdParty.mFactoryAdapter.getUartStatus()) {
					ThirdParty.mFactoryAdapter.openSerialPort();
				}
				else {
					ThirdParty.mFactoryAdapter.closeSerialPort();
				}
				break;
			case 9:
				inputTitleDialog();
				break;		
			case 10:
				//Toast.makeText(mContext, "ResetALL", Toast.LENGTH_SHORT).show();
				//TV.writeV(TVDefine.ResetAll);
				resetConfirmDialog(1);
				break;	
		        case 11:
		            startRemoteDiagnosis();
		            break;
			}
		}
		break;
		default:
			break;
		}
	}
 
	@Override
	public void updateMenuInfos() {
		/*
		if(ThirdParty.mFactoryAdapter.getGetUserIDStatus()){ 
			menuInfos[0][1]="ON"; 
		} else {
			menuInfos[0][1]="Off";
		}
		*/
		super.updateStatusInfo(TVDefine.AfterSaleMenu_GetUserID, 0);
		super.updateStatusInfo(TVDefine.AfterSaleMenu_BGM, 1);
		super.updateStatusInfo(TVDefine.AfterSaleMenu_NMR,4);		
		super.updateValueInfo(TVDefine.AfterSaleMenu_DVBT2_mode, 5);
		super.updateStatusInfo(TVDefine.AfterSaleMenu_HbbTV_mode, 6);
		if(ThirdParty.mFactoryAdapter.getUartStatus())	
			menuInfos[7][1]="ON"; 
		else 
			menuInfos[7][1]="OFF";
		menuInfos[8][1] = ""+ThirdParty.getRunningTimeString();
	}
	
	private void inputTitleDialog() {
		Debug.v(TAG, "inputTitleDialog1");
		final EditText inputServer = new EditText(mContext);
		inputServer.setFocusable(true);
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Please input Hbbtv potal URL:").setIcon(TCLAlertDialog.INFO_IMAGE).setView(
				inputServer).setNegativeButton("Cancle", null);
		builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				String inputName = inputServer.getText().toString();
				if(ThirdParty.OpenHbbtvPortal(inputName)){
					Toast.makeText(mContext, "Hbbtv potal URL connecting...", Toast.LENGTH_LONG).show();
				}else{
					Toast.makeText(mContext, "Hbbtv potal URL open fail!", Toast.LENGTH_SHORT).show();
				}
			}
		});
		Debug.v(TAG, "inputTitleDialog2");
		builder.show();
	}
	
	private void resetConfirmDialog(final int select){
		Debug.v(TAG, "resetConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if(select == 0)
		{
			builder.setTitle("NVM Reset").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else if(select == 1)
		{
			builder.setTitle("Reset All").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else
		{
			builder.setTitle("Reset Shop").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				if(select == 0){
					TV.writeV(TVDefine.NVMReSet);
				}
				else if(select == 1){
					TV.writeV(TVDefine.ResetAll);
				}
				else {
					TV.writeV(TVDefine.ShopInit);
				}
			}
		});
		builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
			}
		});
		builder.show();
	}

    private void startRemoteDiagnosis() {
        Debug.v(TAG, "startRemoteDiagnosis");
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.tcl.common.tvfaultdiagnosis",
                "com.tcl.common.tvfaultdiagnosis.MainActivity"));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        mContext.startActivity(intent);
    }
	
}
