package com.tcl.factory.view;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.tcl.customerapi.R;
import com.tcl.factory.bsp.TV;
import com.tcl.factory.bsp.TVDefine;
import com.tcl.factory.def.DEFS;
import com.tcl.factory.utils.Debug;

  
public class BalanceDialog extends Dialog {
	
	private String TAG = this.getClass().getName();
	private Timer timer = new Timer();
	private int count=0;
	private int balancevalue=0;
	private TextView hintText=null;
	private TextView value=null;
	private final int DELAY_TIME=3;
	private ProgressBar valueBar = null;

	public BalanceDialog(Context context) 
	{
		super(context,R.style.CustomDialog);	
		WindowManager.LayoutParams lp=getWindow().getAttributes();
		lp.y=DEFS.DESKTOP_HEIGHT / 2 - 100;
	
		setContentView(R.layout.fac_progress_bar);
		hintText=(TextView)findViewById(R.id.label);
		hintText.setText("Balance");
		value=(TextView)findViewById(R.id.value);
		valueBar = (ProgressBar) findViewById(R.id.progress_bar);
		valueBar.setProgress(TV.readV(TVDefine.AudioBalance));
		value.setText(""+TV.readV(TVDefine.AudioBalance));
		timer.schedule(task, 100, 1000);
	
	}
	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if(count==DELAY_TIME){
				timer.cancel();
				task.cancel();
				count=0;
				dismiss();
			}
			super.handleMessage(msg);
		}
	};

	TimerTask	task = new TimerTask() {
		@Override
		public void run() {
			Debug.v(TAG , "TimerTask : getDmodeHotKeyStatus--------ssssAAA");
			Message message = Message.obtain();
			message.what = 1;
			count++;
			handler.sendMessage(message);
		}
	};
	
	@Override
	public void onAttachedToWindow(){ 
//		TV.APPKeyGuard(DEFS.Enable);
		super.onAttachedToWindow();
	} 
	
	public boolean dispatchKeyEvent(KeyEvent event) 
	{
		Debug.v(TAG , "dispatchKeyEvent : event.getAction() = " + event.getAction() + ", event.getKeyCode() = " + event.getKeyCode());
 		if (event.getAction() == KeyEvent.ACTION_DOWN) 
		{
			balancevalue=TV.readV(TVDefine.AudioBalance);
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_VOLUME_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT: // set volume down
				if(balancevalue>0)
				{
					valueBar.setProgress(--balancevalue);
					value.setText(""+balancevalue);
					TV.writeV(TVDefine.AudioBalance, balancevalue);
				}
 				break;
			case KeyEvent.KEYCODE_VOLUME_UP:
			case KeyEvent.KEYCODE_DPAD_RIGHT: // set volume up
			{
				count=0;
				if(balancevalue<50)
					valueBar.setProgress(++balancevalue);
				value.setText(""+balancevalue);
				TV.writeV(TVDefine.AudioBalance, balancevalue);
			}
			break;
			case KeyEvent.KEYCODE_DPAD_CENTER:
			case KeyEvent.KEYCODE_ENTER: // cancel the dialog
				dismiss();
				break;
			case KeyEvent.KEYCODE_HOME:
			case KeyEvent.KEYCODE_MENU: // cancel the dialog
				dismiss();
				break;
			case 273:// set Balance 0
				balancevalue=(TV.readV(TVDefine.SidePanelKeyCode)&0x00ff);
				if(balancevalue<=100)
				{
					value.setText(""+balancevalue);
					TV.writeV(TVDefine.AudioBalance, balancevalue);
 					valueBar.setProgress(balancevalue);
				}
				break;
			default:

				break;
			}
		}
		return true;
	}
}
