package com.backfire.room.surface;

import java.util.ArrayList;

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

import com.backfire.arithmetic.Card;
import com.backfire.arithmetic.CardAssist;
import com.backfire.doudizhu.common.BitmapManager;
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.HandlerMsg;
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.widget.SeatButton;

public class UserSurface {
	public static final int delayShowResidueTime = 2;
	public static long startTime = 0;// gameover开始时间

	private BitmapManager bpool = null;
	private SeatButton[] seatButtons;

	private boolean isDisplayLastOutCard = true;

	public UserSurface(IClickListener listener) {
		bpool = DdzApplication.getApplication().getBmpPool();
		seatButtons = new SeatButton[3];
		for (int i = 0; i < seatButtons.length; i++) {
			seatButtons[i] = new SeatButton(listener, HandlerMsg.MSG_INFOS[i],
					ConfigCenter.room_seat_X[i], ConfigCenter.room_seat_Y[i],
					ConfigCenter.room_seat_W, ConfigCenter.room_seat_H);
			seatButtons[i].setEnable(true);
			seatButtons[i].setVisible(true);
		}
	}

	public void reset() {
		isDisplayLastOutCard = true;
		startTime = 0;
	}

	public boolean dispatch(MotionEvent event) {
		boolean result = false;
		if (null == seatButtons)
			return result;
		synchronized (seatButtons) {
			for (SeatButton seatButton : seatButtons) {
				if (null == seatButton)
					continue;
				result = seatButton.dispatch(event);
				if (result)
					break;
			}
		}
		return result;
	}

	public void draw(PCanvas canvas, int gameState, int selfState) {
		RoomInfo info = RoomInfo.getInstance();
		boolean isRefight=info.isRefight();
		int selfSeatId=info.getSeatId();
		// ArrayList<Card> cards = CardAssist.copyList(info.getMyCards());
		for (int i = 0; i < 3; i++) {
			Player player = info.getPlayer(i + 1);
			if (player != null&&(!isRefight||(i+1==selfSeatId))) {
				int seatIdx = info.getSeatIdx(player.getSeatId());
				if (null == seatButtons || seatIdx >= seatButtons.length
						|| seatIdx < 0 || null == seatButtons[seatIdx]) {
					continue;
				}
				seatButtons[seatIdx].draw(canvas, gameState, player, seatIdx);
			}
		}
		//再战过程为了保持不闪屏，直接跳过后面的绘制
		if(isRefight)return;
		// 绘制出上家的操作
		drawPreOperate(canvas, info);
		// 绘制丢出去的牌
		if (isDisplayLastOutCard)
			drawOutCard(canvas, info.getSeatIdx(info.getLastOutCardSeatId()),
					info.getLastOutCards());
		// 绘制gameover后各个玩家的手牌
		if (GameState.GAME_OVER == gameState)
			drawGameOverResidue(canvas);
	}

	private void drawPreOperate(PCanvas canvas, RoomInfo info) {
		int preSeatId = 0;
		Player player = null;
		synchronized (info) {
			preSeatId = info.getPreSeatId();
			player = info.getPlayer(preSeatId);
		}
		if (null == player)
			return;
		int operatAction = 0;
		int seatIdx = 0;
		synchronized (player) {
			operatAction = player.getOperateAction();
			seatIdx = info.getSeatIdx(preSeatId);
		}
		if (null == seatButtons || null == seatButtons[seatIdx])
			return;
		seatButtons[seatIdx].drawOperate(canvas, seatIdx, operatAction);
	}

	private void drawHandCard(PCanvas canvas, ArrayList<Card> arrayList) {
		if (null == arrayList)
			return;
		ArrayList<Card> tmpCards = CardAssist.copyList(arrayList);
		int size = tmpCards.size();
		int handcardW = size
				* ConfigCenter.self_hand_card_Off_W
				+ (ConfigCenter.self_hand_card_W - ConfigCenter.self_hand_card_Off_W);
		for (int i = 0; i < size; i++) {
			Card card = tmpCards.get(i);
			Bitmap bitmap = ResCenter.getCardImg(card);
			int startX = (DdzApplication.getApplication().screenWidth - handcardW) / 2;
			int posX = startX + i * ConfigCenter.self_hand_card_Off_W;
			int posY = ConfigCenter.self_hand_card_PosY;
			if (null != bitmap) {
				canvas.drawBitmap(bitmap, posX, posY, null);
				if (card.isSmallLaizi()) {
					Bitmap flower = ResCenter.getFlower();
					if (flower != null) {
						canvas.drawBitmap(flower, posX,
								ConfigCenter.self_hand_card_PosY, null);
					}
				}
			}
		}

	}

	private void drawOutCard(PCanvas canvas, int seatIdx,
			ArrayList<Card> arrayList) {
		if (null == arrayList)
			return;
		ArrayList<Card> tmpCards = CardAssist.copyList(arrayList);
		int size = tmpCards.size();
		int handcardW = size * ConfigCenter.out_card_Off_W
				+ (ConfigCenter.out_card_W - ConfigCenter.out_card_Off_W);
		for (int i = 0; i < size; i++) {
			Card card = tmpCards.get(i);
			boolean isLaizi = card.isLaiZi();
			if (isLaizi) {// 获取不是癞子资源
				card.setSmallLaizi(true);
			}
			Bitmap bitmap = ResCenter.getCardImg(card);
			int startX = ConfigCenter.out_card_Start_X[seatIdx];
			int posX = 0;
			int posY = ConfigCenter.out_card_PosY[seatIdx];
			switch (seatIdx) {
			case ConfigCenter.SeatPos1:
				posX = startX - ConfigCenter.out_card_all_W
						+ (ConfigCenter.out_card_all_W - handcardW) + i
						* ConfigCenter.out_card_Off_W;
				break;
			case ConfigCenter.SeatPos2:
				posX = startX + i * ConfigCenter.out_card_Off_W;
				break;
			case ConfigCenter.SeatPos3:
				posX = (DdzApplication.getApplication().screenWidth - handcardW)
						/ 2 + i * ConfigCenter.out_card_Off_W;
			}
			canvas.drawBitmap(bitmap, ConfigCenter.out_card_W,
					ConfigCenter.out_card_H, posX, posY, null);
			if (isLaizi) {
				Bitmap flower = ResCenter.getFlower();
				if (flower != null) {
					canvas.drawBitmap(flower, ConfigCenter.out_card_W,
							ConfigCenter.out_card_H, posX, posY, null);
				}
			}
		}
	}

	/** 绘制gameover后各个玩家的剩余手牌 **/
	private void drawGameOverResidue(PCanvas canvas) {
		if (0 == startTime) {
			startTime = System.currentTimeMillis();
			return;
		} else {
			long gapTime = (System.currentTimeMillis() - startTime) / 1000;
			if (gapTime < delayShowResidueTime)
				return;
			if (isDisplayLastOutCard) {
				isDisplayLastOutCard = false;
				return;
			}
		}

		RoomInfo roomInfo = RoomInfo.getInstance();
		for (int i = 0; i < 3; i++) {
			int seatId = i + 1;
			int seatIdx = roomInfo.getSeatIdx(seatId);
			Player player = roomInfo.getPlayer(seatId);
			ArrayList<Card> residueCards = null;
			if (null != player) {
				synchronized (player) {
					residueCards = player.getLimitedCards();
				}
			}
			if (null == residueCards || residueCards.isEmpty())
				continue;
			drawOutCard(canvas, seatIdx, residueCards);
		}
	}
}
