
package com.tcl.factory.view;

import java.util.ArrayList; 
import java.util.List;
import android.util.Log;
import android.view.KeyEvent;
import android.content.Context;
import android.tclwidget.TCLAlertDialog;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Context;
import android.widget.Toast;
import android.os.Handler;
import android.os.Message;
import android.os.Looper;
import android.os.SystemProperties;
import android.content.Intent;
import android.content.BroadcastReceiver;

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


public class HotelBootLogoList extends SubMenu {
	public static  String MenuInfos_HotelBootLogoList[][]={
		{"Default Logo",""},
		{"None Logo",""},
		{"User Logo",""},
	};
	public static  String MenuInfos_HotelBootAnimList[][]={
		{"Default Animation",""},
		{"None Animation",""},
		{"User Animation",""},
	};
	public static  String MenuInfos_HotelBootVideoList[][]={
		{"None Video",""},
		{"User Video",""},
	};
	private int MAX_ITEM_NUM = 3;
	private final static int TIME_OUT_CHECK_BOOT_ANIMATION  = 105;	 //105 = 21*5 (*200MS) = 21 second 
	private final static int TIME_OUT_CHECK_BOOT_VIDEO  = 155;	 //155 = 31*5 (*200MS) = 31 second

	private final static String TAG = "HotelBootLogoList";
	private Context mContext;
	//private int position = 0;
	private int boot_type = 0; 
	private int powerOnIndex = 0;
	private int currentIndex = 0;
	public HotelBootLogoList(Context context, int index)
	{
		mContext = context;
		boot_type = index;
		super.setTAG();
		switch(boot_type)
		{
			case DEFS.HOTEL_BOOT_ANIM:
				menuInfos=MenuInfos_HotelBootAnimList;
				menuLabel = "Hotel\\BootAnimationList";
				break;
			case DEFS.HOTEL_BOOT_VIDEO:
				menuInfos=MenuInfos_HotelBootVideoList;
				menuLabel = "Hotel\\BootVidelList";
				MAX_ITEM_NUM = 2;
				break;
			case DEFS.HOTEL_BOOT_LOGO:
			default:
				menuInfos=MenuInfos_HotelBootLogoList;
				menuLabel = "Hotel\\BootLogoList";
				break;
		}	
		updateMenuInfos();   
	}


	public void KeyEvent(int keyCode,int pos){
		Log.d(TAG, "keyCode = "+keyCode+", pos ="+pos);
		/*
		switch(keyCode)
		{
			case KeyEvent.KEYCODE_DPAD_LEFT:
			break;	

			case KeyEvent.KEYCODE_DPAD_RIGHT:
			break;					
		}
		*/
	}

	@Override
	public void updateMenuInfos() {
		switch(boot_type)
		{
			case DEFS.HOTEL_BOOT_ANIM:
				powerOnIndex = ThirdParty.mFactoryAdapter.getHotelBootAnimation();
				break;
			case DEFS.HOTEL_BOOT_VIDEO:
				powerOnIndex = ThirdParty.mFactoryAdapter.getHotelBootVideo();
				break;
			case DEFS.HOTEL_BOOT_LOGO:
			default:
				powerOnIndex = ThirdParty.mFactoryAdapter.getHotelBootLogo();
				break;
		}	
		// TODO Auto-generated method stub
		Debug.d(TAG, "powerOnIndex = "+powerOnIndex);
		for(int i = 0;i<MAX_ITEM_NUM;i++)
		{
			if(powerOnIndex == i )
				menuInfos[i][1] = ""+"Selected";
			else
				menuInfos[i][1] = "...";
		}
	}

	@Override
	public void enterSubMenu(int pos) 
	{
		// TODO Auto-generated method stub
		super.setMenuPosition(pos);
		currentIndex = pos;
		if(ThirdParty.getHotelBootLogoCheckStatus()!=DEFS.HOTEL_BOOT_LOGO_CHECK_STATUS_NULL)
		{
			return;
		}
		switch(boot_type)
		{
			case DEFS.HOTEL_BOOT_LOGO: 
				LogoSelectConfirmDialog();
				break;
			case DEFS.HOTEL_BOOT_ANIM: 
				AnimationSelectConfirmDialog();
				break;
			case DEFS.HOTEL_BOOT_VIDEO: 
				VideoSelectConfirmDialog();
				break;	
		}
		Debug.v(TAG, "enterSubMenu : pos = " + pos);
	}

	private void LogoSelectConfirmDialog(){
		Debug.v(TAG, "LogoSelectConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if(currentIndex == DEFS.HOTEL_DEFAULT_LOGO)
		{
			builder.setTitle("Do you want to set up as a default logo?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else if(currentIndex == DEFS.HOTEL_NO_LOGO)
		{
			builder.setTitle("Do you want to cancle the boot logo?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else
		{
			builder.setTitle("Need to display and check the new logo, do you want to continue?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				ThirdParty.setPowerLogo(currentIndex);
				updateMenuInfos();
				if(MainMenu._Instance!=null)
				{
					MainMenu._Instance.updateView();
					MainMenu._Instance.MainMenuSetSelection(currentIndex);
				}
			}
		});
		builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){

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

	private void AnimationSelectConfirmDialog(){
		Debug.v(TAG, "AnimationSelectConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if(currentIndex == DEFS.HOTEL_DEFAULT_LOGO)
		{
			builder.setTitle("Do you want to set up as a default animation?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else if(currentIndex == DEFS.HOTEL_NO_LOGO)
		{
			builder.setTitle("Do you want to cancle the boot animation").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else
		{
			builder.setTitle("Need to display and check the new animation, do you want to continue?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

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

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

	private void UserBootAnimationSelectConfirmDialog(){
		Debug.v(TAG, "UserBootAnimationSelectConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Do you want to set up as the new animation?").setIcon(TCLAlertDialog.INFO_IMAGE);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

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

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

	private void VideoSelectConfirmDialog(){
		Debug.v(TAG, "VideoSelectConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		if(currentIndex == DEFS.HOTEL_NO_BOOT_VIDEO)
		{
			builder.setTitle("Do you want to cancle the boot boot video? ").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		else
		{
			builder.setTitle("Need to display and check the new video, do you want to continue?").setIcon(TCLAlertDialog.INFO_IMAGE);
		}
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				ThirdParty.setPowerVideo(currentIndex);
				if(currentIndex == DEFS.HOTEL_USER_BOOT_VIDEO
					&&(ThirdParty.getHotelBootLogoCheckStatus() ==DEFS.HOTEL_BOOT_LOGO_CHECK_STATUS_VIDEO)){
					PlayBootVideoThread();
				}
				else
				{
					updateMenuInfos();
					if(MainMenu._Instance!=null)
					{
						MainMenu._Instance.updateView();
						MainMenu._Instance.MainMenuSetSelection(currentIndex);
					}
				}				
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

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

	private void UserBootVideoSelectConfirmDialog(){
		Debug.v(TAG, "UserBootVideoSelectConfirmDialog");
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle("Do you want to set up as the new boot video?").setIcon(TCLAlertDialog.INFO_IMAGE);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				ThirdParty.setUserBootVideo();
				updateMenuInfos();
				if(MainMenu._Instance!=null)
				{
					MainMenu._Instance.updateView();
					MainMenu._Instance.MainMenuSetSelection(currentIndex);
				}
			}
		});
		builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){

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

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				UserBootAnimationSelectConfirmDialog();
				break;
			case 1:
				UserBootVideoSelectConfirmDialog();
				break;
			}
			super.handleMessage(msg);
		}
	};

	private void PlayBootAnimationThread(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				int i=TIME_OUT_CHECK_BOOT_ANIMATION; //timeout 20+1 seconds
				boolean result = false;
				boolean timeout_flg = false;
				while (("running".equals(SystemProperties.get("init.svc.offanim"))))
				{
					Debug.d(TAG, "wait user animation stop times i:" + i);
					if (("1".equals(SystemProperties.get(DEFS.HOTEL_PROPERTY_TEST_ANIM_FINISH_FLG)))){
						Debug.d(TAG, "offanimation play finish!!");
						result = true;
						break;
					}
					if (i--<=0) {
						Debug.d(TAG, "offanimation time out!!");
						timeout_flg = true;
						break;
					}
					try
					{
					Thread.currentThread().sleep(200);
					}catch (InterruptedException e)
					{}
            			}

				if((!timeout_flg)&&("1".equals(SystemProperties.get(DEFS.HOTEL_PROPERTY_TEST_ANIM_FINISH_FLG)))){
					Debug.d(TAG, "offanimation play finish!!");
					String str = SystemProperties.get(DEFS.HOTEL_PROPERTY_TEST_ANIM_STATUS,"");
					Debug.v(TAG, DEFS.HOTEL_PROPERTY_TEST_ANIM_STATUS+"=" + str);
					if(str == null||str.length()==0||"0".equals(str))
					{
						result = true;
					}
				}

				if(result){
					Message message=Message.obtain();
					message.what=0;
					handler.sendMessage(message);
				}else{
					Toast.makeText(mContext, "play animtion fail!!", Toast.LENGTH_SHORT).show();
				}				
				ThirdParty.setHotelBootLogoCheckStatus(DEFS.HOTEL_BOOT_LOGO_CHECK_STATUS_NULL);				
				Looper.loop();
			}
		}).start();
	}

	private void PlayBootVideoThread(){
		new Thread(new Runnable() {
			@Override
			public void run() {
				Looper.prepare();
				int i=TIME_OUT_CHECK_BOOT_VIDEO; //timeout 30+1 seconds
				boolean result = false;
				boolean timeout_flg = false;
				while (!ThirdParty.mFactoryAdapter.getHotelCheckBootVideoFinish())
				{
					if (i--<=0) {
						Debug.d(TAG, "check bootvideo play time out!!");
						timeout_flg = true;
						break;
					}
					try
					{
					Thread.currentThread().sleep(200);
					}catch (InterruptedException e)
					{}
            			}
				Debug.d(TAG, "bootvideo play finish!!");
				if(!timeout_flg){
					
					int ret = ThirdParty.mFactoryAdapter.getHotelCheckBootVideoPlayStatus();
					Debug.v(TAG, "getHotelCheckBootVideoPlayStatus = " + ret);
					if(ret == 0)
					{
						result = true;
					}
				}
				if(result){
					Message message=Message.obtain();
					message.what=1;
					handler.sendMessage(message);
				}else{
					Toast.makeText(mContext, "play video fail!!", Toast.LENGTH_SHORT).show();
				}
				if(!ThirdParty.getPModeStatus())
				{
					Intent intent = new Intent();
					intent.setAction("com.tcl.factory.pmode.off");
					mContext.sendBroadcast(intent);
				}
				ThirdParty.setHotelBootLogoCheckStatus(DEFS.HOTEL_BOOT_LOGO_CHECK_STATUS_NULL);
				Looper.loop();
			}
		}).start();
	}

}
