package com.tcl.factory.view;


import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.tclwidget.TCLAlertDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tcl.customerapi.R;
import com.tcl.factory.bsp.TCL;

 

public class DModeUsbUpdate extends SubMenu
{
	public static  String MenuInfos_DModeUsbUpdate[][]={
		{"Main Upgrade",">"},
		{"[HDCP] Upgrade",">"},
		{"[HDCP2_0] Upgrade",">"},
		{"[FRC_IMAGE] Upgrade",">"},
		{"DONGLE Upgrade[RC65]",">"},
		{"DONGLE Upgrade[RC71]",">"},
		{"MAC Upgrade",">"},
		{"LocalDimming Upgrade",">"},
		{"LocalDimming Version",">"},
		{"CI Key Upgrade",">"},
		{"WideVine Key Upgrade",">"},
		{"Mini_LVDS Upgrade",">"}
	};
//	private int modeType=0;
//	// jiangt add for RC71
//	private TextView hintText = null;
//	public static int TIMEOUT_MAXNUM = 14;
//	public static int NO_DONGLE_PLUG_IN = 6;
//	public static int FAIL_FIND_BIN_IN_UDISK = 7;
//	public static int FAIL_SET_DONGLE_ISP_MODE = 8;
//	public static int SUCCESS_BURN_DONGLE = 9;
//	public static int FAIL_BURN_DONGLE = 10;
//	public static int SUCCESS_INIT_DONGLE = 11;
//	public static int FAIL_INIT_DONGLE = 12;
//	public static int RC71_Dongle_Update_ALL_FAIL = 13;
//	private static boolean threadRunnig = false;

//	public static final String[] upgradeStatus = { "Fail!", "Success!",
//			"File Not Found!", "Please Plug USB!",
//			"Upgradeing!!!Please Wait...", "Rebooting!!!Please Wait...",
//			"NO_DONGLE_PLUG_IN", "FAIL_FIND_BIN_IN_UDISK",
//			"FAIL_SET_DONGLE_ISP_MODE", "SUCCESS_BURN_DONGLE",
//			"FAIL_BURN_DONGLE", "SUCCESS_INIT_DONGLE", "FAIL_INIT_DONGLE",
//			"Dongle_Update_ALL_FAIL" };

//	public static int UPGRATE_END_FAIL = 0;
//	public static int UPGRATE_END_SUCCESS = 1;
//	public static int UPGRATE_END_FILE_NOT_FOUND = 2;
//	public static int UPGRATE_END_NO_USB = 3;
//	public static int UPGRATE_START = 4;
//	public static int TIMEOUT = 5;
//	private int count = 0;
//
//	// end
	private Context mDModeUsbUpdateContext;
	public DModeUsbUpdate(Context context)
	{
		mDModeUsbUpdateContext = context;
		menuInfos=MenuInfos_DModeUsbUpdate;
		menuLabel = "Main menu\\Server\\UsbUpdate";
		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:TCL.usbUpgrade();break;
			case 1:TCL.HdmiKeyUpgrade();break;
			case 2:TCL.WidiKeyUpgrade();break;
			case 3:FPCImageUpgrade();break;
			case 4:upgradeRC65(keyCode);break;
			case 5:upgradeRC71(keyCode);break;
			case 6:TCL.MACUpgrade();break;
			case 7:TCL.LocalDimmingUpgrade();break;
			case 8:showLocalDimmingVersion();break;
			case 9:TCL.CIKeyUpgrade();break;
			case 10:TCL.WideVineKeyUpgrade();break;
			case 11:TCL.VB1_LVDS_Upgrade();break;
			}
		}
		break;
		default:
			break;
		}
	}

	private void showLocalDimmingVersion() {
		menuInfos[8][1] = TCL.LocalDimmingVersion();
	}

	private void FPCImageUpgrade() {
		String path = TCL.getUSBMountPath(mDModeUsbUpdateContext);
		if(null==path || ("".equals(path)) ){
			Toast.makeText(mDModeUsbUpdateContext, "NO_USB_MOUNT", Toast.LENGTH_SHORT).show();
			return;
		} else {
			showDialog();
			Log.i("progressValue", "FPCImageUpgrade() start ");
			handler.sendEmptyMessageDelayed(1, 2500);
//			TCL.FPCImageUpgrade();Log.i("progressValue", "FPCImageUpgrade() end ");
		}		
	}
	
	private void showDialog() {
		mDialogPasswordSet = TCLAlertDialog.makeFullDialog(mDModeUsbUpdateContext, 0);//com.android.internal.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
		LayoutInflater Inflater = (LayoutInflater) mDModeUsbUpdateContext
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = Inflater.inflate(
				R.layout.dialog_progress, null);
		RelativeLayout.LayoutParams lpcontent = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT);
			lpcontent.width = 734;
			lpcontent.height = 160;
			
		mDialogPasswordSet.setCustomContentView(layout, lpcontent);
//		seekbar = (ProgressBar) layout.findViewById(R.id.seekbar);
		textview_right = (TextView) layout.findViewById(R.id.righttextview);
		
		RelativeLayout.LayoutParams lptitle = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT);
		lptitle.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
		lptitle.height = RelativeLayout.LayoutParams.WRAP_CONTENT;

		mDialogPasswordSet.setTitle(mDModeUsbUpdateContext, mDialogPasswordSet,
				TCLAlertDialog.INFO_IMAGE,
				"[FRC_IMAGE] Upgrade",
				lptitle);
//		mDialogPasswordSet.setTitle("[FRC_IMAGE] Upgrade");
		mDialogPasswordSet.show();
		//mDialogPasswordSet.setCancelable(false);
		handler.sendEmptyMessageDelayed(0, 1500);
	}
	TCLAlertDialog mDialogPasswordSet;
//	ProgressBar seekbar;
	TextView textview_right;
	public void setProgress(int progress) {
		if(mDialogPasswordSet != null && mDialogPasswordSet.isShowing()) {
//			textview_right.setText(progress+"%");
//			seekbar.setProgress(progress);
			textview_right.setText("Upgrade...");
		}
	}
	
	int progressValue = 0;
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			Log.d(TAG, "handleMessage:" + msg.what);

			switch (msg.what) {
			case 0:
				progressValue = TCL.getFPCImageUpgradeProgress();
				Log.i("progressValue", "progressValue = " + progressValue);
//				progressValue += 10; 
//				if(progressValue <= 100) {
					setProgress(progressValue);
//					handler.sendEmptyMessageDelayed(0, 1500);
//				} else {
//					progressValue = 0;
//				}
				break;
			case 1:
				new Thread(new Runnable() {
					
					@Override
					public void run() {
						boolean result = TCL.FPCImageUpgrade();
						Log.i("progressValue", "result = " + result +",FPCImageUpgrade() end ");
						if(result) { 
							handler.sendEmptyMessageDelayed(2, 1500);
						} else {
							handler.sendEmptyMessageDelayed(3, 1500);
						}
					}
				}).start();
				
				break;
			case 2:
				textview_right.setText("Upgrade success!");
				break;
			case 3:
				textview_right.setText("Upgrade failed!");
				break;
			}
		}
    };

	@Override
	public void updateMenuInfos() {
		// TODO Auto-generated method stub
	}
	
	public void upgradeRC65(int keyCode)
	{
		//rc71
//		USBUpgradeDialog usbUpgradeDialog = new USBUpgradeDialog(ThirdParty.mContext);
//		usbUpgradeDialog.show();
//		usbUpgradeDialog.UpgradeDialogConfirm();
		//rc65
		USBUpgradeRC65Dialog usbUpgradeDialog = new USBUpgradeRC65Dialog(mDModeUsbUpdateContext);
		usbUpgradeDialog.show();
		usbUpgradeDialog.UpgradeDialogConfirm();
	}
	public void upgradeRC71(int keyCode)
	{
		//rc71
		USBUpgradeDialog usbUpgradeDialog = new USBUpgradeDialog(mDModeUsbUpdateContext);
		usbUpgradeDialog.show();
		usbUpgradeDialog.UpgradeDialogConfirm();
//		//rc65
//		USBUpgradeRC65Dialog usbUpgradeDialog = new USBUpgradeRC65Dialog(mDModeUsbUpdateContext);
//		usbUpgradeDialog.show();
//		usbUpgradeDialog.UpgradeDialogConfirm();
	}
	 
}
