package com.backfire.widget;

import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.MotionEvent;

import com.backfire.doudizhu.common.ClickFilter;
import com.backfire.doudizhu.common.CriticalPool;
import com.backfire.doudizhu.common.DdzApplication;
import com.backfire.graphics.PCanvas;
import com.backfire.room.Interface.IClickListener;

public class RoomButton extends PButton{
	private int icon = 0;
	private boolean isVisible = true;
	private boolean isTouchDown = false;
	int width;
	int height;
	
	private int drawicon = 0;
	private boolean drawisVisible = false;
	private boolean drawenalbe= false;
	private boolean drawisTouchDown = false;
	
	public RoomButton(IClickListener iClickListener,int msg,int icon,int x,int y,int width,int height){
		super(iClickListener,msg,x,y,width,height);
		this.width = width;
		this.height =height;
		this.icon = icon;
	}
	public void clear(){
		 icon = 0;
		 isVisible = false;
		 isTouchDown = false;
		 listener = null;
		 rect = null;
		 enable = false;
		 
		drawicon = 0;
		drawisVisible = false;
		drawisTouchDown = false;
	}
	public void draw(PCanvas canvas){
		if(!drawisVisible) return;
		if(drawicon<=0) return;
		Bitmap bitmap = DdzApplication.getApplication().getBmpPool().getBitmap(drawicon,width,height);
		if(bitmap==null) return ;
		if(!drawenalbe){
			Paint paint = CriticalPool.getPaint();
			paint.setColorFilter(CriticalPool.getColorMatrixColorFilter());
			if(drawicon>0){
				bitmap = DdzApplication.getApplication().getBmpPool().getBitmap(drawicon,width,height);
				if(bitmap!=null){
					canvas.drawBitmap(bitmap, rect.left, rect.top, paint);
				}
			}else{
				canvas.drawBitmap(bitmap, rect.left, rect.top, paint);
			}
		}else if(drawisTouchDown){
			canvas.drawBitmap(bitmap, rect.left, rect.top, CriticalPool.getPaintDown());
		}else{
			canvas.drawBitmap(bitmap, rect.left, rect.top, CriticalPool.getPaintUp());
		}
	}
	public void initDraw(){
		drawicon = icon;
		drawisVisible = isVisible;
		drawisTouchDown = isTouchDown;
		drawenalbe = enable;
	}
	public void setDrawTouchDown(boolean flag){
		enable = flag;
	}
	
	public boolean dispatch(MotionEvent event){
		boolean result=false;
		if(!isVisible){
			isTouchDown = false;
		}else if (event.getAction() == MotionEvent.ACTION_UP) {
			int clickX = (int) event.getX();
			int clicky = (int) event.getY();
			if(enable && rect.contains(clickX, clicky)){
				if(ClickFilter.TestMultiClick())return result;
				if(listener!=null){
					result=true;
					listener.sendMsg(msg);
				}
			}
			isTouchDown = false;
		}else if (event.getAction() == MotionEvent.ACTION_DOWN) {
			int clickX = (int) event.getX();
			int clicky = (int) event.getY();
			if(enable && rect.contains(clickX, clicky)){
				result=true;
				isTouchDown = true;
			}else{
				isTouchDown = false;
			} 
		}
		return result;
	}
	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}
	public void setEnable(boolean enable){
		this.enable = enable;
	}
}
