package extend.zp.uitl;

import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;

public class Paixing {
	public static boolean checkWin(EXPlayer player, int drawCard) {
		player.resetHuXi();
		boolean isWin = false;
		WinCard win = new WinCard(player.cardInhand, drawCard);
		EXPlayer activePlayer = (EXPlayer) player.room.playerMapBySeat.get(player.room.activeSeat);
		if (win.tryWin(player)) {
			if (player.huXi >= 15) {
				isWin = true;
			}
		}
		if (!isWin) {
			player.resetHuXi();
			if (player.getRoom().isTianHu) {
				if (player.tiCount >= 3) {
					player.isMingTang = false;
					isWin = true;
				}
				if (player.kanGroup.size() >= 5) {
					player.isMingTang = false;
					isWin = true;
				}
				if (player.tiCount >= 1 && player.kanGroup.size() >= 4) {
					player.isMingTang = false;
					isWin = true;
				}
				if (player.tiCount >= 2 && player.kanGroup.size() >= 3) {
					player.isMingTang = false;
					isWin = true;
				}
			}

			for (int i = 0; i < player.pongGroup.size(); i++) {
				if (drawCard == player.pongGroup.get(i)[0]) {
					player.kongCount++;
					WinCard win1 = new WinCard(player.cardInhand, 0);
					if (win1.tryWin(player)) {
						int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_PONG, drawCard);
						int huXi2 = Paixing.caclHuxi(RuleWeight.TYPE_PAO, drawCard);
						if (player.huXi - huXi1 + huXi2 >= 15) {
							isWin = true;
						}
					}
					player.kongCount--;
					break;
				}
			}
			if (player.seat != activePlayer.seat) {
				for (int i = 0; i < player.kanGroup.size(); i++) {
					if (drawCard == player.kanGroup.get(i)[0]) {
						player.kongCount++;
						WinCard win1 = new WinCard(player.cardInhand, 0);
						if (win1.tryWin(player)) {
							int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_KAN, drawCard);
							int huXi2 = Paixing.caclHuxi(RuleWeight.TYPE_PAO, drawCard);
							if (player.huXi - huXi1 + huXi2 >= 15) {
								isWin = true;
							}
						}
						player.kongCount--;
						break;
					}
				}
				for (int i = 0; i < player.weiGroup.size(); i++) {
					if (drawCard == player.weiGroup.get(i)[0]) {
						player.kongCount++;
						WinCard win1 = new WinCard(player.cardInhand, 0);
						if (win1.tryWin(player)) {
							int huXi1 = Paixing.caclHuxi(RuleWeight.TYPE_WEI, drawCard);
							int huXi2 = Paixing.caclHuxi(RuleWeight.TYPE_PAO, drawCard);
							if (player.huXi - huXi1 + huXi2 >= 15) {
								isWin = true;
							}
						}
						player.kongCount--;
						break;
					}
				}
			}
		}
		if (isWin) {
			player.handCard.clear();
			player.handCardEx.clear();
			for (int i = 0; i < win.stack.size(); i++) {
				OpCard opcard = new OpCard();
				opcard.card1 = win.stack.get(i).get(0);
				opcard.card2 = win.stack.get(i).get(1);
				opcard.type = 1;
				if (win.stack.get(i).size() > 2) {
					opcard.card3 = win.stack.get(i).get(2);
					opcard.type = 2;
				}
				opcard.huXi = win.stackHuxi.get(i);

				player.handCard.push(opcard);
			}
			if (!win.cardList.isEmpty()) {
				player.handCardEx.addAll(win.cardList);
			}
			
		}
		return isWin;
	}

	public static int caclHuxi(int type, int card) {
		int cardType = CardUtil.cardType(card);
		int huXi = 0;
		switch (type) {
		case RuleWeight.TYPE_TI:
			if (cardType == RuleWeight.SMALL_CARD) {
				huXi = 9;
			} else {
				huXi = 12;
			}
			break;
		case RuleWeight.TYPE_PAO:
			if (cardType == RuleWeight.SMALL_CARD) {
				huXi = 6;
			} else {
				huXi = 9;
			}
			break;
		case RuleWeight.TYPE_WEI:
		case RuleWeight.TYPE_CHOUWEI:
		case RuleWeight.TYPE_KAN:
		case RuleWeight.TYPE_CHOW:
			if (cardType == RuleWeight.SMALL_CARD) {
				huXi = 3;
			} else {
				huXi = 6;
			}
			break;
		case RuleWeight.TYPE_PONG:
			if (cardType == RuleWeight.SMALL_CARD) {
				huXi = 1;
			} else {
				huXi = 3;
			}
			break;
		default:
			break;
		}
		return huXi;
	}
}
