package extend.zp.player.rulestate;

import com.game.Global;
import com.game.Util;
import com.game.player.state.PlayerWaitState;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;

import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.RuleWeight;
import extend.zp.tip.Tip;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;

public class PRChi {

	public static void prChi(int card, EXPlayer owner, boolean drawChi) {
		EXPlayer activePlayer = (EXPlayer) owner.room.playerMapBySeat.get(owner.room.activeSeat);
		activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		Tip actionTip = owner.actionTip;
		ITArray opcard = actionTip.opcard;
		int chowcard1 = opcard.getInt(0);
		int chowcard2 = opcard.getInt(1);
		__chow(owner, chowcard1, chowcard2, card, activePlayer, false, drawChi);
		if (actionTip.bi_list != null && actionTip.biid != null) {
			ITObject obj = actionTip.bi_list.getTObject(actionTip.biid.getInt(0));
			ITArray biopcard = obj.getTArray("opcard");
			__chow(owner, biopcard.getInt(0), biopcard.getInt(1), card, owner, true, drawChi);
			if (obj.containsKey("bi_list")) {
				ITArray bi_list1 = obj.getTArray("bi_list");
				ITObject obj1 = bi_list1.getTObject(actionTip.biid.getInt(1));
				ITArray biopcard1 = obj1.getTArray("opcard");
				__chow(owner, biopcard1.getInt(0), biopcard1.getInt(1), card, owner, true, drawChi);
			}
		}

	}

	private static final void __chow(EXPlayer player, int chowcard1, int chowcard2, int card, EXPlayer activePlayer, boolean bi, boolean drawChi) {
		int[] chiGroup = new int[3];
		chiGroup[0] = chowcard1;
		chiGroup[1] = card;
		chiGroup[2] = chowcard2;
		int haoCard = 0;
		int haoType = RuleWeight.HAO_NONE;
		player.notPutSet.add(card);
		if (chowcard1 - chowcard2 == 1)
		{
			if (chowcard2 % 100 == 1)
			{
			    player.notPutSet.add(chowcard2 + 2);
			}
			else if (chowcard2 % 100 == 9)
			{
				player.notPutSet.add(chowcard2 - 1);
			}
			else
			{
				player.notPutSet.add(chowcard2 - 1);
				player.notPutSet.add(chowcard2 + 2);
			}
		}
		else if (chowcard1 - chowcard2 == 2)
		{
			player.notPutSet.add(chowcard2 + 1);
		}
		else if (chowcard2 - chowcard1 == 1)
		{
			if (chowcard1 % 100 == 1)
			{
				player.notPutSet.add(chowcard1 + 2);
			}
			else if (chowcard1 % 100 == 9)
			{
				player.notPutSet.add(chowcard1 - 1);
			}
			else
			{
				player.notPutSet.add(chowcard1 - 1);
				player.notPutSet.add(chowcard1 + 2);
			}
		}
		else if (chowcard2 - chowcard1 == 2)
		{
			player.notPutSet.add(chowcard1 + 1);
		}

		if (!Util.checkCard(chowcard1, player.cardInhand)) {
			for (int i = 0; i < player.kanGroup.size(); i++) {
				if (chowcard1 == player.kanGroup.get(i)[0]) {
					player.kanGroup.remove(i);
					CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, chowcard1, chowcard1, chowcard1, 0));
					player.cardInhand.add(chowcard1);
					player.cardInhand.add(chowcard1);
					break;
				}
			}
			for (int i = 0; i < player.tiGroup.size(); i++) {
				if (chowcard1 == player.tiGroup.get(i)[0]) {
					player.tiGroup.remove(i);
					CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_TI, chowcard1, chowcard1, chowcard1, 0));

					//////////////////////////////////////////////////
//					player.cardInhand.add(chowcard1);
//					player.cardInhand.add(chowcard1);
//					player.cardInhand.add(chowcard1);
					//============================================
					int[] kanGroup = new int[3];
					kanGroup[0] = chowcard1;
					kanGroup[1] = chowcard1;
					kanGroup[2] = chowcard1;

					player.kanGroup.add(kanGroup);
					player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, chowcard1, chowcard1, chowcard1, Paixing.caclHuxi(RuleWeight.TYPE_KAN, chowcard1), true, RuleWeight.HAO_NONE, 0));
					////////////////////////////////////////////////

					haoCard = chowcard1;
					haoType = RuleWeight.HAO_NUI;
					break;
				}
			}
		}
		else {
			CardUtil.removeCard(player.cardInhand, chowcard1, 1);
		}

		if (!Util.checkCard(chowcard2, player.cardInhand)) {
			for (int i = 0; i < player.kanGroup.size(); i++) {
				if (chowcard2 == player.kanGroup.get(i)[0]) {
					player.kanGroup.remove(i);
					CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, chowcard2, chowcard2, chowcard2, 0));
					player.cardInhand.add(chowcard2);
					player.cardInhand.add(chowcard2);
					break;
				}
			}
			for (int i = 0; i < player.tiGroup.size(); i++) {
				if (chowcard2 == player.tiGroup.get(i)[0]) {
					player.tiGroup.remove(i);
					CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_TI, chowcard2, chowcard2, chowcard2, 0));
//					player.cardInhand.add(chowcard2);
//					player.cardInhand.add(chowcard2);
//					player.cardInhand.add(chowcard2);

					int[] kanGroup = new int[3];
					kanGroup[0] = chowcard2;
					kanGroup[1] = chowcard2;
					kanGroup[2] = chowcard2;

					player.kanGroup.add(kanGroup);
					player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, chowcard2, chowcard2, chowcard2, Paixing.caclHuxi(RuleWeight.TYPE_KAN, chowcard2), true, RuleWeight.HAO_NONE, 0));


					haoCard = chowcard2;
					haoType = RuleWeight.HAO_NUI;
					break;
				}
			}
		}
		else {
			CardUtil.removeCard(player.cardInhand, chowcard2, 1);
		}

		if (bi) {
			if (!Util.checkCard(card, player.cardInhand)) {
				for (int i = 0; i < player.kanGroup.size(); i++) {
					if (card == player.kanGroup.get(i)[0]) {
						player.kanGroup.remove(i);
						CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_KAN, card, card, card, 0));
						player.cardInhand.add(card);
						player.cardInhand.add(card);
						break;
					}
				}
				for (int i = 0; i < player.tiGroup.size(); i++) {
					if (card == player.tiGroup.get(i)[0]) {
						player.tiGroup.remove(i);
						CardUtil.removeOpcard(player.opCardList, new OpCard(RuleWeight.TYPE_TI, card, card, card, 0));
//						player.cardInhand.add(card);
//						player.cardInhand.add(card);
//						player.cardInhand.add(card);

						int[] kanGroup = new int[3];
						kanGroup[0] = card;
						kanGroup[1] = card;
						kanGroup[2] = card;

						player.kanGroup.add(kanGroup);
						player.opCardList.add(new OpCard(RuleWeight.TYPE_KAN, card, card, card, Paixing.caclHuxi(RuleWeight.TYPE_KAN, card), true, RuleWeight.HAO_NONE, 0));




						break;
					}
				}
			}
			else {
				CardUtil.removeCard(player.cardInhand, card, 1);
			}
		}
		else {
			if (haoCard == 0)
			{
				for (int i = 0; i < player.kanGroup.size(); i++) {
					if (card == player.kanGroup.get(i)[0]) {
						haoCard = card;
						haoType = RuleWeight.HAO_WAI;
						player.notPutSet.add(card);
						break;
					}
				}
			}

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

			if (haoCard == 0)
			{
				for (int i = 0; i < player.weiGroup.size(); i++) {
					if (card == player.weiGroup.get(i)[0]) {
						if (player.playerid == activePlayer.playerid)
						{
							haoCard = card;
							haoType = RuleWeight.HAO_NUI;
						}
						else if (player.playerid != activePlayer.playerid)
						{
							haoCard = card;
							haoType = RuleWeight.HAO_WAI;
						}
						break;
					}
				}
			}

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

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

				//加上本次，相当于吃了4张一样的
				if (handNum == 0 && chiNum == 4)
				{
					haoCard = card;
					haoType = RuleWeight.HAO_SHAN;
				}
				else if (handNum == 1 && chiNum == 3) {
					haoCard = card;
					haoType = RuleWeight.HAO_SHAN;
				}
				else if (handNum == 2 && chiNum == 2)
				{
					haoCard = card;
					haoType = RuleWeight.HAO_WAI;
				}
			}
		}



		player.chowGroup.add(chiGroup);
		int huXi = 0;
		if (chiGroup[0] % 100 != chiGroup[1] % 100) {
			if (chiGroup[0] % 100 + chiGroup[1] % 100 + chiGroup[2] % 100 == 19) {
			//if (chiGroup[0] % 100 + chiGroup[1] % 100 + chiGroup[2] % 100 == 6 || chiGroup[0] % 100 + chiGroup[1] % 100 + chiGroup[2] % 100 == 19) {
				huXi += Paixing.caclHuxi(RuleWeight.TYPE_CHOW, chiGroup[0]);
			}
		}

		if (haoCard == 0)
		{
			player.opCardList.add(new OpCard(RuleWeight.TYPE_CHOW, chiGroup[0], chiGroup[1], chiGroup[2], huXi));
		}
		else
		{
			player.opCardList.add(new OpCard(RuleWeight.TYPE_CHOW, chiGroup[0], chiGroup[1], chiGroup[2], huXi, false, haoType, haoCard));
		}

		ITArray opcard = TArray.newInstance();
		opcard.addInt(chiGroup[0]);
		opcard.addInt(chiGroup[2]);
		EXMainServer.gameCtr.actionCard(player, card, card, bi ? RuleWeight.TYPE_BI : RuleWeight.TYPE_CHOW, activePlayer.seat, opcard);
	}
}
