package com.tcl.factory.utils;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.TextView;

import com.tcl.customerapi.R;
import com.tcl.factory.def.DEFS;

public class TVInfoToast extends Dialog {
	private TextView  mTextView=null;
	private Context mContext;
	private final int TV_INFO_DELAY = 3;
	private static  int timecount=0;
	private static Timer timer = null; 
	private Handler handler = new Handler() {  
		@Override
		public void handleMessage(Message msg) {	
			timecount++;
			Debug.v("TVInfoToast==", "==  == "+timecount);
			if(timecount>=TV_INFO_DELAY){
				timecount=0;
				dismiss();
			}
			super.handleMessage(msg);
		}

	};
	public TVInfoToast(Context context) {
		super(context, R.style.CustomDialog);
		this.mContext = context;
	}
	public void showRightTopInfo(String displayStr) {
		timecount=0;
		mTextView = new TextView(mContext);
		mTextView.setTextColor(Color.WHITE);
		mTextView.setBackgroundResource(R.drawable.white_floor);
		mTextView.setGravity(Gravity.CENTER);
		mTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
		mTextView.setWidth(140);
		int endColorIndex = 0;
		int myColor = Color.BLACK;

		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.y = -(DEFS.DESKTOP_HEIGHT / 2) + 100;
		lp.x = DEFS.DESKTOP_WIDTH / 2 - 170;
		lp.type = WindowManager.LayoutParams.TYPE_TOAST;
		onWindowAttributesChanged(lp);

		mTextView.setText(displayStr);
		SpannableStringBuilder style = new SpannableStringBuilder(displayStr);
		style.setSpan(new ForegroundColorSpan(myColor), 0, endColorIndex,
				Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		mTextView.setText(style);
		setContentView(mTextView);
		synchronized(timer) {
			if(timer==null){
				timer=new Timer();
				timer.schedule(new MyTimerTask(),10, 1000);
				show();
			}
		}
	}

	private class MyTimerTask extends TimerTask{

		@Override
		public void run() {
			handler.sendEmptyMessage(0);
		}

	}
	@Override
	public void dismiss() {
		synchronized(timer) {
			if(timer!=null){
				timer.cancel();
				timer=null;
			}
		}
		super.dismiss();
	}

}
