package com.backfire.widget;

import net.yeeyo.doudizhu.feiyu.R;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.view.MotionEvent;

import com.backfire.doudizhu.common.BitmapManager;
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;
import com.backfire.room.data.ConfigCenter;
import com.backfire.room.data.Player;
import com.backfire.room.data.ResCenter;
import com.backfire.room.data.RoomInfo;
import com.backfire.room.data.State.GameState;
import com.backfire.room.data.State.Identity;

public class SeatButton extends PButton {
	private boolean isVisible = false;
	private static BitmapManager bpool = null;
	private static Bitmap icon_border = null;
	private static Bitmap residueCardIcon = null;

	public SeatButton(IClickListener iClickListener, int msg, int x, int y,
			int w, int h) {
		super(iClickListener, msg, x, y, w, h);
		bpool = DdzApplication.getApplication().getBmpPool();
		icon_border = bpool.getBitmap(R.drawable.icon_border,
				ConfigCenter.room_seat_W, ConfigCenter.room_seat_H);
		residueCardIcon = bpool.getBitmap(R.drawable.card_tag,
				ConfigCenter.room_seat_card_icon_W,
				ConfigCenter.room_seat_card_icon_H);
	}

	public void clear() {
		isVisible = false;
		listener = null;
		enable = false;
	}

	public void draw(PCanvas canvas, int gameState, Player player, int seatIdx) {
		if (null == player)
			return;
		// 所有的要处理的信息先在这儿定义
		int seatId = 0;
		int icon = 0;
		int side = 0;
		int state = 0;
		int cardNum = 0;
		int sex = 0;
		String nick = "";
		long money = 0;
		boolean isDeposite = false;
		boolean isReady = false;
		synchronized (player) {
			// 所有的要处理的信息然后在这儿初始化
			seatId = player.getSeatId();
			icon = player.getIcon();
			side = player.getSide();
			state = player.getState();
			cardNum = player.getCardNum();
			isDeposite = player.isDeposit();
			isReady = player.isReady();
			sex = player.getSex();
			nick = player.getNick();
			money = player.getMoney();
			if (gameState > GameState.GAME_QIANG_ZHUANG) {
				icon = ResCenter.getIcon(icon, side, sex);
			}
		}
		// 1.绘制固定显示
		drawIcon(canvas, seatIdx, icon);
		if (seatId != RoomInfo.getInstance().getSeatId()) {// 自己的就不要显示了
			drawResidueCard(canvas, seatIdx, cardNum, seatId);
		}
		drawIdentity(canvas, side, seatIdx);
		drawNickAndMoney(canvas, nick, money, seatIdx);
		// 2.绘制状态显示
		drawState(canvas, gameState, seatIdx, isDeposite, isReady);
	}

	private void drawNickAndMoney(PCanvas canvas, String nick, long money,
			int seatIdx) {
		// 昵称
		
		Paint paint = CriticalPool.getPaint();
		paint.setColor(Color.argb(255, 255, 255, 255));
		paint.setTextSize(ConfigCenter.side_nick_font_size);
		paint.setStyle(Style.FILL);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setFakeBoldText(true); 
		Rect rect = CriticalPool.getRect();
		paint.getTextBounds(nick, 0, 1, rect); 
		drawText(canvas, nick,ConfigCenter.room_seat_X[seatIdx]+ConfigCenter.side_nick_offset_W, ConfigCenter.room_seat_Y[seatIdx]+ConfigCenter.side_nick_offset_H, 
						ConfigCenter.side_nick_W, ConfigCenter.side_nick_H,paint,rect);

		
		paint = CriticalPool.getPaint();
		paint.setColor(Color.argb(255, 255, 255, 255));
		paint.setTextSize(ConfigCenter.side_money_font_size);
		paint.setStyle(Style.FILL);
		paint.setTypeface(Typeface.DEFAULT_BOLD);
		paint.setFakeBoldText(true); 
		rect = CriticalPool.getRect();
		paint.getTextBounds(ConfigCenter.getMoney(money), 0, 1, rect); 
		drawText(canvas, ConfigCenter.getMoney(money),ConfigCenter.room_seat_X[seatIdx]+ConfigCenter.side_money_offset_W, ConfigCenter.room_seat_Y[seatIdx]+ConfigCenter.room_seat_H-ConfigCenter.side_money_offset_H, 
						ConfigCenter.side_nick_W, ConfigCenter.side_nick_H,paint,rect);
		// 金币
//		paint = CriticalPool.getPaint();
//		paint.setTextSize(ConfigCenter.side_money_font_size);
//		paint.setColor(Color.argb(255, 255, 255, 255));
//		paint.setFakeBoldText(true);
//		paint.setTextAlign(Align.RIGHT);
//		x = ConfigCenter.room_seat_X[seatIdx] + ConfigCenter.room_seat_W
//				- ConfigCenter.side_money_offset_W;
//		y = ConfigCenter.room_seat_Y[seatIdx] + ConfigCenter.room_seat_H
//				+ ConfigCenter.side_money_offset_H
//				- ConfigCenter.side_money_font_size / 2;
//		maxLength = ConfigCenter.side_money_W
//				/ ConfigCenter.side_money_font_size * 2;
//		String sMoney = ConfigCenter.getLobbyMoney(money);
//		String subMoney = sMoney.length() > maxLength ? sMoney.substring(0,
//				maxLength) : sMoney;
//		canvas.drawText(subMoney, x, y, paint);
	}

	public void drawState(PCanvas canvas, int gameState, int seatIdx,
			boolean isDeposite, boolean isReady) {
		if (GameState.GAME_READY == gameState && isReady) {
			Bitmap bitmap = bpool.getBitmap(R.drawable.ready_text,
					ConfigCenter.operate_two_W, ConfigCenter.operate_tag_H);
			if (null != bitmap) {
				int posX = rect.right + ConfigCenter.operate_tag_offset_W;
				int posY = rect.bottom - ConfigCenter.operate_tag_offset_H;
				if (ConfigCenter.SeatPos1 == seatIdx)
					posX = rect.left - ConfigCenter.operate_two_W
							- ConfigCenter.operate_tag_offset_W;
				canvas.drawBitmap(bitmap, posX, posY, null);
			}
		}

		if (isDeposite) {
			Bitmap bitmap = bpool.getBitmap(R.drawable.desposit_tag,
					ConfigCenter.desposit_tag_W, ConfigCenter.desposit_tag_H);
			if (null != bitmap) {
				int posX = rect.right - ConfigCenter.desposit_tag_W / 2;
				int posY = rect.bottom - ConfigCenter.desposit_tag_H / 2;
				if (ConfigCenter.SeatPos1 == seatIdx)
					posX = rect.left - ConfigCenter.desposit_tag_W / 2;
				canvas.drawBitmap(bitmap, posX, posY, null);
			}
		}
	}

	public void drawOperate(PCanvas canvas, int seatIdx, int operateAction) {
		int icon = ResCenter.getOperate(operateAction);
		if (icon <= 0)
			return;

		int width = ConfigCenter.getOperateTextW(operateAction);
		Bitmap bitmap = bpool
				.getBitmap(icon, width, ConfigCenter.operate_tag_H);
		if (null == bitmap)
			return;

		int posX = rect.right + ConfigCenter.operate_tag_offset_W;
		if (ConfigCenter.SeatPos1 == seatIdx)
			posX = rect.left - width - ConfigCenter.operate_tag_offset_W;
		int posY = rect.bottom - ConfigCenter.operate_tag_offset_H;
		canvas.drawBitmap(bitmap, posX, posY, null);
	}

	public void drawIdentity(PCanvas canvas, int side, int seatIdx) {
		if (side != 0 && side != Identity.LANDLORD && side != Identity.COMMONER)
			return;
		Bitmap bitmap = null;
		if (Identity.LANDLORD == side) {
			bitmap = bpool.getBitmap(R.drawable.landlord_tag,
					ConfigCenter.side_tag_W, ConfigCenter.side_tag_H);
		} else if (Identity.COMMONER == side) {
			bitmap = bpool.getBitmap(R.drawable.peasant_tag,
					ConfigCenter.side_tag_W, ConfigCenter.side_tag_H);
		}
		if (null == bitmap)
			return;
		int posX = rect.right + ConfigCenter.side_tag_offset_W;
		if (ConfigCenter.SeatPos1 == seatIdx)
			posX = rect.left - ConfigCenter.side_tag_W
					- ConfigCenter.side_tag_offset_W;
		canvas.drawBitmap(bitmap, posX, rect.top
				+ ConfigCenter.side_tag_offset_H, null);
	}

	public void drawIcon(PCanvas canvas, int seatIdx, int icon) {
		if (icon_border != null) {
			canvas.drawBitmap(icon_border, rect.left, rect.top, null);
		}
		if (icon != 0) {
			Bitmap userIcon = bpool.getBitmap(icon,
					ConfigCenter.room_seat_image_W,
					ConfigCenter.room_seat_image_H);
			if (userIcon != null) {
				if (ConfigCenter.SeatPos1 != seatIdx) {// 镜像翻转
					float scaleX = ConfigCenter.room_seat_image_W
							/ (float) userIcon.getWidth();
					float posX = ConfigCenter.room_seat_image_X[seatIdx]
							+ ConfigCenter.room_seat_image_W;

					float[] valuse = { -scaleX, 0, posX, 0, scaleX,
							ConfigCenter.room_seat_image_Y[seatIdx], 0, 0, 1 };

					Matrix matrix = new Matrix();
					matrix.setValues(valuse);
					canvas.drawBitmap(userIcon, matrix, null);
				} else {
					canvas.drawBitmap(userIcon, ConfigCenter.room_seat_image_W,
							ConfigCenter.room_seat_image_H,
							ConfigCenter.room_seat_image_X[seatIdx],
							ConfigCenter.room_seat_image_Y[seatIdx], null);
				}
			}
		}
	}

	public void drawResidueCard(Canvas canvas, int seatIdx, int cardNum,
			int seatId) {
		canvas.drawBitmap(residueCardIcon,
				ConfigCenter.room_seat_card_icon_X[seatIdx],
				ConfigCenter.room_seat_card_icon_Y[seatIdx], null);

		Paint paint = CriticalPool.getPaint();
		paint.setTextSize(ConfigCenter.seat_card_num_Font_size);
		paint.setColor(Color.argb(255, 255, 255, 255));
		paint.setFakeBoldText(true);
		int x = ConfigCenter.room_seat_card_num_X[seatIdx];
		int y = ConfigCenter.room_seat_card_num_Y[seatIdx]
				+ ConfigCenter.seat_card_num_Font_size / 2;
		canvas.drawText(Integer.toString(cardNum), x, y, paint);

		// 测试代码
		/*
		 * paint=CriticalPool.getPaint(); paint.setTextSize(20);
		 * paint.setColor(Color.BLACK);
		 * canvas.drawText("idx="+seatIdx+",sd="+seatId, x-30, y-20, paint);
		 */
	}

	public boolean dispatch(MotionEvent event) {
		boolean result = false;
		if (isVisible && 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) {
					listener.sendMsg(msg);
					result = true;
				}
			}
		}
		return result;
	}

	public void setVisible(boolean isVisible) {
		this.isVisible = isVisible;
	}

	public void setEnable(boolean enable) {
		this.enable = enable;
	}
	private void drawText(Canvas canvas, String text, int x, int y, int w, int h, Paint paint,Rect rect) {
		if (text == null || text.length() == 0) {
			return;
		}
		
		int textWidth = (int)(paint.measureText(text));
		if (textWidth > w) {
			int position = paint.breakText(text, true, w-2, null);
			text = text.substring(0, position) + "..";
			canvas.drawText(text, x, y+(h+rect.bottom-rect.top)/2-ConfigCenter.getFontAddHeight(paint), paint);
		} else {
			int center = textWidth / 2;
			canvas.drawText(text, x - center + w/2,  y+(h+rect.bottom-rect.top)/2-ConfigCenter.getFontAddHeight(paint), paint);
		}
	}
}
