
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 com.tcl.factory.bsp.TV;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.thirdparty.ThirdParty;
import com.tcl.factory.utils.AppInfo;

public class HotelAppList extends SubMenu {
	private final static int MAX_PAGE_NUM = 17;
	public static  String MenuInfos_HotelAppList[][]={
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
		{"",""},
	};
	
	private final static String TAG = "HotelAppList";
	private List<AppInfo> appInfoList = new ArrayList<AppInfo>();
	private LauncherHelper mLauncherHelper;
	private Context mContext;
	private int mCurrentPageIndex = 0;
	private int mAppSize = 0;
	private int mPageSize = 0;
	private int mCurrentAppIndex = 0;
	private int position = 0;
	
	public HotelAppList(Context context)
	{
		mContext = context;
		menuInfos=MenuInfos_HotelAppList;
		menuLabel = "Hotel\\AppList";
		super.setTAG();
		mLauncherHelper = new LauncherHelper(mContext);
		appInfoList = mLauncherHelper.getAppList();
		mCurrentAppIndex = mLauncherHelper.getCurLauncherAppIndex();
		mAppSize = appInfoList.size();
		mPageSize = (mAppSize-1)/MAX_PAGE_NUM;
		mCurrentPageIndex = mCurrentAppIndex/MAX_PAGE_NUM;
		updateMenuInfos();   
	}


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

			case KeyEvent.KEYCODE_DPAD_RIGHT:
				showNextPage();
			break;	
			
			case KeyEvent.KEYCODE_DPAD_DOWN: 
				if((pos>=MAX_PAGE_NUM-1)||(mCurrentPageIndex*MAX_PAGE_NUM+pos)>=mAppSize-1)
				{
					showNextPage();
				}
			break;
			case KeyEvent.KEYCODE_DPAD_UP: 
				if(pos<=0)
				{
					showPrePage();
				}
			break;			
		}
	}

	public void  showPrePage(){
		Log.d(TAG, "enter showPrePage mCurrentPageIndex = "+mCurrentPageIndex);
		if(mPageSize==0)
		{
			return;
		}
		if(mCurrentPageIndex==0)
			mCurrentPageIndex = mPageSize;
		else
			mCurrentPageIndex--;
		updateMenuInfos();   
		MainMenu._Instance.updateView();
		
	}

	public void  showNextPage(){
		Log.d(TAG, "enter showNextPage mCurrentPageIndex"+mCurrentPageIndex);
		if(mPageSize==0)
		{
			return;
		}
		if(mCurrentPageIndex==mPageSize)
			mCurrentPageIndex = 0;
		else
			mCurrentPageIndex++;
		updateMenuInfos();  
		MainMenu._Instance.updateView();
	}	

	@Override
	public void updateMenuInfos() {
		// TODO Auto-generated method stub
		int item_num = MAX_PAGE_NUM;
		int current_page_start_index = mCurrentPageIndex*MAX_PAGE_NUM;
		if(mCurrentPageIndex==mPageSize)
			item_num = mAppSize-current_page_start_index;
		
		for(int i = 0;i<MAX_PAGE_NUM;i++)
		{
			if(i >=item_num)
			{
				super.setItemDisable(i);
				menuInfos[i][0]="";
				menuInfos[i][1]="";
			}
			else
			{
				super.setItemEnable(i);
				menuInfos[i][0]=""+ appInfoList.get(current_page_start_index+i).getmAppName();
				Log.d(TAG,"jacky app = "+menuInfos[i][0]);
				if((current_page_start_index+i) == mLauncherHelper.getCurLauncherAppIndex())
					menuInfos[i][1] = ""+"< Selected >";
				else
					menuInfos[i][1] = "< ... >";
			}
		}
	}
	
	/* (non-Javadoc)
	 * Factory
	 */
	@Override
	public void enterSubMenu(int pos) 
	{
		// TODO Auto-generated method stub
		super.enterSubMenu(pos);
		LauncherSelectConfirmDialog(pos);
 	}

	private void LauncherSelectConfirmDialog(int pos){
		Log.d(TAG, "LauncherSelectConfirmDialog");
		mCurrentAppIndex = mCurrentPageIndex*MAX_PAGE_NUM+pos;
		position = pos;
		
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);

		builder.setTitle("Do you want to set this APP to default APP?").setIcon(TCLAlertDialog.INFO_IMAGE);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub
				boolean ret = mLauncherHelper.setLauncherApp(mCurrentAppIndex);
				if(ret)
				{
					updateMenuInfos();
					if(MainMenu._Instance!=null)
					{
						MainMenu._Instance.updateView();
						MainMenu._Instance.MainMenuSetSelection(position);
					}
					Toast.makeText(mContext, "Set app success!!", Toast.LENGTH_SHORT).show();
				}
				else
				{
					Toast.makeText(mContext, "Set app false!!", Toast.LENGTH_SHORT).show();
				}
			}
		});
		builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener(){

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

}
