package extend.zp.uitl;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import com.taurus.core.util.Logger;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.tip.Tip;

import java.util.ArrayList;
import java.util.Map;

public class Paixing {
	Logger logger = Logger.getLogger(getClass());

	public static boolean checkWin(EXPlayer player, int drawCard, boolean isSelfCard) {
		player.resetHuXi();
		boolean isWin = false;
		WinCard win = new WinCard(player, player.cardInhand, drawCard);
		EXPlayer activePlayer = (EXPlayer) player.room.playerMapBySeat.get(player.room.activeSeat);
		if (win.tryWin(player)) {
			if (player.huXi >= 8) {
				isWin = true;
			}
		}

		int duziNum = 0;
		int siduiNum = 0;
		ArrayList<Integer> duiList = new ArrayList<>();
		duiList.addAll(player.cardInhand);
		for (int i = 0; i < player.tiGroup.size(); i++) {
			int card = player.tiGroup.get(i)[0];
			duiList.add(card);
			duiList.add(card);
			duiList.add(card);
			duiList.add(card);
		}
		for (int i = 0; i < player.kanGroup.size(); i++) {
			int card = player.kanGroup.get(i)[0];
			duiList.add(card);
			duiList.add(card);
			duiList.add(card);
		}

		if (activePlayer != null & activePlayer.drawCard > 0) {
			duiList.add(activePlayer.drawCard);
		}

		Map<Integer, Integer> cardNumList = CardUtil.getCardNumMap(duiList);
		for (Map.Entry<Integer, Integer> card : cardNumList.entrySet()) {
			if (card.getValue() == 2 || card.getValue() == 3) {
				duziNum++;
			}
			else if (card.getValue() == 4) {
				siduiNum++;
			}
		}
		//Global.logger.info("jefe pxbf: duziNum "+ duziNum+"  siduiNum = "+siduiNum );
		if(duziNum==0&&siduiNum==1){
			duziNum = 1;
		}else if (duziNum==1&&siduiNum>0){
			duziNum = 2;
		}else if (duziNum>1&&siduiNum>0){
			duziNum = duziNum + siduiNum*2;
		}

		//

		if (duiList.size() >19)
		{
			if (duziNum==1){
				isWin = true;
			}
			if (duziNum == 9){
				isWin = true;
			}
			if(duziNum==0){
				isWin = true;
			}
			if (duziNum == 10 ){
				isWin = true;
			}
		}
		Global.logger.info("jefe iswin: "+ isWin+"  card = "+drawCard +" huxi:"+player.huXi+" playid:"+player);

		if (isWin) {
			player.handCard.clear();
			player.handCardEx.clear();
			int haoCard = 0;
			int haoType = 0;
			for (int x = 0; x < win.stack.size(); x++) {
				OpCard opcard = new OpCard();
				opcard.card1 = win.stack.get(x).get(0);
				opcard.card2 = win.stack.get(x).get(1);
				opcard.type = 1;
				haoCard = 0;
				haoType = 0;
				if (win.stack.get(x).size() > 2) {
					opcard.card3 = win.stack.get(x).get(2);
					opcard.type = 2;

					if (opcard.card1 != opcard.card2)
					{
						if (opcard.card1 == drawCard || opcard.card2 == drawCard || opcard.card3 == drawCard)
						{
							if (haoCard == 0)
							{
								for (int j = 0; j < player.kanGroup.size(); j++) {
									if (drawCard == player.kanGroup.get(j)[0]) {
										if (!isSelfCard)
										{
											haoCard = drawCard;
											haoType = RuleWeight.HAO_WAI;
										}
										else {
											haoCard = drawCard;
											haoType = RuleWeight.HAO_NUI;
										}
										break;
									}
								}
							}

							if (haoCard == 0)
							{
								for (int j = 0; j < player.pongGroup.size(); j++) {
									if (drawCard == player.pongGroup.get(j)[0]) {
										haoCard = drawCard;
										haoType = RuleWeight.HAO_WAI;
										break;
									}
								}
							}

							if (haoCard == 0)
							{
								for (int j = 0; j < player.weiGroup.size(); j++) {
									if (drawCard == player.weiGroup.get(j)[0]) {
										if (isSelfCard)
										{
											haoCard = drawCard;
											haoType = RuleWeight.HAO_NUI;
										}
										else
										{
											haoCard = drawCard;
											haoType = RuleWeight.HAO_WAI;
										}
										break;
									}
								}
							}

							if (haoCard == 0)
							{
								int handNum = CardUtil.cardNum(drawCard, player.cardInhand);
								for (int j = 0; j < player.chowGroup.size(); j++) {
									if (drawCard == player.chowGroup.get(j)[0]) {
										handNum++;
									}
									if (drawCard == player.chowGroup.get(j)[2]) {
										handNum++;
									}
								}

								//算出吃到的
								int chiNum = 1; //本次算一次
								for (int j = 0; j < player.chowGroup.size(); j++) {
									if (drawCard == player.chowGroup.get(j)[1]) {
										chiNum++;
									}
								}

								//加上本次，相当于吃了4张一样的
								if (handNum == 0 && chiNum == 4)
								{
									haoCard = drawCard;
									haoType = RuleWeight.HAO_SHAN;
								}
								else if (handNum == 1 && chiNum == 3) {
									haoCard = drawCard;
									haoType = RuleWeight.HAO_SHAN;
								}
								else if (handNum == 2 && chiNum == 2)
								{
									haoCard = drawCard;
									haoType = RuleWeight.HAO_WAI;
								}
							}
						}
					}
					else {
						if (opcard.card1 == drawCard)
						{
							for (int j = 0; j < player.chowGroup.size(); j++) {
								if (player.chowGroup.get(j)[1] == drawCard)
								{
									haoCard = drawCard;
									haoType = RuleWeight.HAO_WAI;
									break;
								}
							}

							int handNum = CardUtil.cardNum(drawCard, player.cardInhand) - 2;
							if (handNum > 0)
							{
								haoCard = drawCard;
								haoType = RuleWeight.HAO_WAI;
							}
						}
					}
				}
				else {
					if (opcard.card1 == drawCard || opcard.card2 == drawCard)
					{
						if (haoCard == 0)
						{
							for (int j = 0; j < player.kanGroup.size(); j++) {
								if (drawCard == player.kanGroup.get(j)[0]) {
									if (!isSelfCard)
									{
										haoCard = drawCard;
										haoType = RuleWeight.HAO_WAI;
									}
									else {
										haoCard = drawCard;
										haoType = RuleWeight.HAO_NUI;
									}
									break;
								}
							}
						}

						if (haoCard == 0)
						{
							for (int j = 0; j < player.pongGroup.size(); j++) {
								if (drawCard == player.pongGroup.get(j)[0]) {
									haoCard = drawCard;
									haoType = RuleWeight.HAO_WAI;
									break;
								}
							}
						}

						if (haoCard == 0)
						{
							for (int j = 0; j < player.weiGroup.size(); j++) {
								if (drawCard == player.weiGroup.get(j)[0]) {
									if (isSelfCard)
									{
										haoCard = drawCard;
										haoType = RuleWeight.HAO_NUI;
									}
									else
									{
										haoCard = drawCard;
										haoType = RuleWeight.HAO_WAI;
									}
									break;
								}
							}
						}

						if (haoCard == 0)
						{
							int handNum = CardUtil.cardNum(drawCard, player.cardInhand);
							for (int j = 0; j < player.chowGroup.size(); j++) {
								if (drawCard == player.chowGroup.get(j)[0]) {
									handNum++;
								}
								if (drawCard == player.chowGroup.get(j)[2]) {
									handNum++;
								}
							}

							//算出吃到的
							int chiNum = 1; //本次算一次
							for (int j = 0; j < player.chowGroup.size(); j++) {
								if (drawCard == player.chowGroup.get(j)[1]) {
									chiNum++;
								}
							}

							//加上本次，相当于吃了4张一样的
							if (handNum == 0 && chiNum == 4)
							{
								haoCard = drawCard;
								haoType = RuleWeight.HAO_SHAN;
							}
							else if (handNum == 1 && chiNum == 3) {
								haoCard = drawCard;
								haoType = RuleWeight.HAO_SHAN;
							}
							else if (handNum == 2 && chiNum == 2)
							{
								haoCard = drawCard;
								haoType = RuleWeight.HAO_WAI;
							}
						}
					}
				}
				opcard.huXi = win.stackHuxi.get(x);
				opcard.hao = haoType;
				opcard.haoCard = haoCard;
				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:
				huXi = 5;
			break;
		case RuleWeight.TYPE_PAO:
				huXi = 0;
			break;
		case RuleWeight.TYPE_WEI:
		case RuleWeight.TYPE_CHOUWEI:
				huXi = 4;
			break;
		case RuleWeight.TYPE_KAN:
				huXi = 3;
			break;
		case RuleWeight.TYPE_CHOW:
				huXi = 1;
			break;
		case RuleWeight.TYPE_PONG:
				huXi = 1;
			break;
		default:
			break;
		}
		return huXi;
	}
}
