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.TextView;

import com.tcl.customerapi.R;
import com.tcl.factory.utils.Debug;


public class DialogHint extends Dialog {
	private String TAG = this.getClass().getName();
	private Timer timer = new Timer();
	private int count=0;
	public  boolean error=true;
	public String   errorHint="Error...";
	private TextView hintText=null;

	public DialogHint(Context context) 
	{
		super(context,R.style.CustomDialog);	
		WindowManager.LayoutParams lp=getWindow().getAttributes();
		lp.y=400;
		lp.x=50;
		setContentView(R.layout.fac_dialoghint);
		hintText=(TextView)findViewById(R.id.hint);
		timer.schedule(task, 100, 500);
		error=false;
	}
	Handler handler = new Handler() {
		public void handleMessage(Message msg) 
		{  
			 if(count==1)
			 {
				if(error)
				hintText.setText(errorHint);
				else
				hintText.setText(R.string.StatusOK);
				}
			else if(count>=4){
				timer.cancel();
				task.cancel();
				count=0;

				dismiss();

			}
			super.handleMessage(msg);
		}
	};


	TimerTask	task = new TimerTask() {
		@Override
		public void run() {
			Debug.v(TAG , "TimerTask");
			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) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_HOME:
			case KeyEvent.KEYCODE_MENU: // cancel the dialog
				dismiss();
				break;
			}
		}
		return true;
	}
	public void setDisplayMessage(String message){
		hintText.setText(message);
	}
}
