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;

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

	public VolumnDialog(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("VOL");
		value=(TextView)findViewById(R.id.value);

		valueBar = (ProgressBar) findViewById(R.id.progress_bar);
		valueBar.setProgress(TV.readV(TVDefine.AudioVolume));
		timer.schedule(task, 100, 1000);
		value.setText(""+TV.readV(TVDefine.AudioVolume));
	}
	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() {

			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) 
	{
		// TODO Auto-generated method stub
		if (event.getAction() == KeyEvent.ACTION_DOWN) 
		{
			volValue=TV.readV(TVDefine.AudioVolume);
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_VOLUME_DOWN:
			case KeyEvent.KEYCODE_DPAD_LEFT: // set volume down
				if(volValue>0)
				{
					count=0;
					valueBar.setProgress(--volValue);
					value.setText(""+volValue);
					TV.writeV(TVDefine.AudioVolume, volValue);
				}
 				break;	
			case KeyEvent.KEYCODE_VOLUME_UP:
			case KeyEvent.KEYCODE_DPAD_RIGHT: // set volume up
			{
				count=0;
				if(volValue<100)
					valueBar.setProgress(++volValue);
				value.setText(""+volValue);
				TV.writeV(TVDefine.AudioVolume, volValue);
 			}
			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 299:	
				volValue=(TV.readV(TVDefine.SidePanelKeyCode)&0x00ff);
				if(volValue<=100)
				{
					value.setText(""+volValue);
					TV.writeV(TVDefine.AudioVolume, volValue);
 					valueBar.setProgress(volValue);
				}
				break;
			default:
				break;
			}
		}
		return true;
	 

	}
}