package extend.mj.uitl;

import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import extend.mj.*;

import java.util.*;

public class WinCard {
	
	public List<Integer>		cardList;
	public int 					laizi_count;
	public int 					laizi1_count;
	public int 					laizi2_count;
	public int 					laizi1 = 0;
	public int 					laizi2 = 0;
	public boolean				zi_mo = false;
	public boolean				ting_pai = false;
	public Map<Integer, Integer> cbCardMap;
	public int					addCard = 0;
	public boolean				selfCard = false;

	public WinCard(List<Integer> cardInhand, int addCard, int laizi1, int laizi2, boolean islaizi, boolean selfCard, boolean ting_pai) {
		this.cardList = new ArrayList<Integer>();
		this.cardList.addAll(cardInhand);
		this.cbCardMap = new HashMap<Integer, Integer>();
		this.ting_pai = ting_pai;
		this.selfCard = selfCard;
		this.addCard = addCard;
		if (selfCard)
		{
			this.cardList.add(addCard);
			if (islaizi) {
				this.laizi1 = laizi1;
				this.laizi2 = laizi2;
				this.laizi1_count = Util.cardNum(laizi1, this.cardList);
				this.laizi2_count = Util.cardNum(laizi2, this.cardList);
				this.laizi_count = this.laizi1_count + this.laizi2_count;
				Util.removeCard(this.cardList, laizi1, this.laizi1_count);
				Util.removeCard(this.cardList, laizi2, this.laizi2_count);
			}
		}
		else {
			if (islaizi) {
				this.laizi1 = laizi1;
				this.laizi2 = laizi2;
				this.laizi1_count = Util.cardNum(laizi1, this.cardList);
				this.laizi2_count = Util.cardNum(laizi2, this.cardList);
				this.laizi_count = this.laizi1_count + this.laizi2_count;
				Util.removeCard(this.cardList, laizi1, this.laizi1_count);
				Util.removeCard(this.cardList, laizi2, this.laizi2_count);
			}
			this.cardList.add(addCard);
		}

		Collections.sort(this.cardList);
		for(int i = 0; i < this.cardList.size(); i++)
		{
			int card = this.cardList.get(i);
			if (this.cbCardMap.containsKey(card))
			{
				int num = this.cbCardMap.get(card);
				this.cbCardMap.put(card, num+1);
			}
			else
			{
				this.cbCardMap.put(card, 1);
			}
		}
	}

	public WinCard(List<Integer> cardInhand, int laizi1, int laizi2, boolean islaizi, boolean ting_pai) {
		this.cardList = new ArrayList<Integer>();
		this.cardList.addAll(cardInhand);
		this.cbCardMap = new HashMap<Integer, Integer>();
		this.ting_pai = ting_pai;
		this.selfCard = true;
		this.addCard = 0;
		if (islaizi) {
			this.laizi1 = laizi1;
			this.laizi2 = laizi2;
			this.laizi1_count = Util.cardNum(laizi1, this.cardList);
			this.laizi2_count = Util.cardNum(laizi2, this.cardList);
			this.laizi_count = this.laizi1_count + this.laizi2_count;
			Util.removeCard(this.cardList, laizi1, this.laizi1_count);
			Util.removeCard(this.cardList, laizi2, this.laizi2_count);
		}
		Collections.sort(this.cardList);
		for(int i = 0; i < this.cardList.size(); i++)
		{
			int card = this.cardList.get(i);
			if (this.cbCardMap.containsKey(card))
			{
				int num = this.cbCardMap.get(card);
				this.cbCardMap.put(card, num+1);
			}
			else
			{
				this.cbCardMap.put(card, 1);
			}
		}
	}

	public boolean checkWin(Map<Integer, WinCardType> map, EXPlayer player, EXRoom room, boolean zimo) {
	    return checkWinOpList(map, player, player.opCardList, room, zimo);
	}

	public boolean checkWinOpList(Map<Integer, WinCardType> map, EXPlayer player, List<OpCard> opList, EXRoom room, boolean zimo) {
		if (this.cardList.size() == 0) {
			return false;
		}

		this.zi_mo = zimo;

		boolean hu = AnalyseChiHuCard(map, opList, room.config);
		if (hu)
		{
			if (zimo)
			{
				if (room.card.cardList.size() == 0 && room.config.getBoolean(Config.ROOM_CONFIG_HAI_DI_NAO_YUE))
				{
					WinCard.putWinCardType(map, WinCardType.HAI_DI_NAO_YUE, 1,WinCardType.HAI_DI_NAO_YUE_SCORE, true, "海底捞月");
				}
			}
			else {

			}
		}

		if (zimo)
		{
			if (room.config.getBoolean(Config.ROOM_CONFIG_MENG_QING))
			{
				for (Map.Entry<Integer, WinCardType> entry : map.entrySet()) {
					WinCardType cardType = entry.getValue();
					if (cardType.type ==  WinCardType.JIHU || cardType.type == WinCardType.PINGHU)
					{
						boolean flag = true;
						for(int i = 0; i < opList.size(); i++)
						{
							OpCard op_card = opList.get(i);
							if (op_card.type != RuleWeight.TYPE_SELF_KONG && op_card.type != RuleWeight.TYPE_SELF_ZHAO)
							{
								flag = false;
							}
						}
						if (flag)
						{
							WinCard.putWinCardType(map, WinCardType.ZIMO_MEN_QING, 1,WinCardType.ZIMO_MEN_QING_SCORE, true, "门清自摸");
						}
						break;
					}
				}
			}
		}
		else
		{
			if (room.config.getBoolean(Config.ROOM_CONFIG_MENG_QING))
			{
				for (Map.Entry<Integer, WinCardType> entry : map.entrySet()) {
					WinCardType cardType = entry.getValue();
					if (cardType.type ==  WinCardType.JIHU || cardType.type == WinCardType.PINGHU)
					{
						boolean flag = true;
						for(int i = 0; i < opList.size(); i++)
						{
							OpCard op_card = opList.get(i);
							if (op_card.type != RuleWeight.TYPE_SELF_KONG && op_card.type != RuleWeight.TYPE_SELF_ZHAO)
							{
							    flag = false;
							}
						}
						if (flag)
						{
							WinCard.putWinCardType(map, WinCardType.MEN_QING, 1,WinCardType.MENGQING_SCORE, true, "门清捉炮");
						}
						break;
					}
				}
			}


			if (room.config.getBoolean(Config.ROOM_CONFIG_JIHU_NOTCHIHU))
			{
				WinCardType jihuType = map.get(WinCardType.JIHU);
				if (jihuType != null)
				{
					//鸡胡不能吃胡
					return false;
				}
			}

			if (room.config.getBoolean(Config.ROOM_CONFIG_SPECAIL_ZIMO))
			{
				int hu_score = 0;
				if (room.config.getBoolean(Config.ROOM_CONFIG_USE_BASE_HU_SCORE))
				{
					hu_score = WinCardType.BASE_HU_SCORE;
				}
				else
				{
					player.hu_score = room.maxPlayers - 1;
					player.di_score = room.maxPlayers - 1;
				}

				int dahu_score = 0;
				if (room.config.getBoolean(Config.ROOM_CONFIG_DAHU_CHENG))
				{
					dahu_score = 1;
				}
				int cheng_score = 1;
				for (Map.Entry<Integer, WinCardType> entry : map.entrySet()) {
					WinCardType cardType = entry.getValue();
					{
						if (room.config.getBoolean(Config.ROOM_CONFIG_DAHU_CHENG))
						{
							if (cardType.IsDaHu())
							{
								dahu_score *= cardType.score;
							}
							else
							{
								cheng_score *= cardType.score;
							}
						}
						else
						{
							if (cardType.IsDaHu())
							{
								dahu_score += cardType.score*WinCardType.DA_HU_BEI_SCORE;
							}
							else
							{
								cheng_score *=  cardType.score;
							}
						}
					}
				}

				if (dahu_score > 0)
				{
					hu_score *= dahu_score;
				}

				hu_score *= cheng_score;

				if (hu_score < 20)
				{
					return false;
				}
			}
		}

		return hu;
	}

	public final static WinCardType putWinCardType(Map<Integer, WinCardType> map, int type, int value,int score, boolean isDaHu, String desc) {
		if (map == null)
			return null;
		if (isDaHu)
		{
			map.remove(WinCardType.JIHU);
			map.remove(WinCardType.PINGHU);
		}
		if (map.containsKey(type)) {
			WinCardType wct = map.get(type);
			wct.value += value;
			//Global.logger.info("putWinCardType: " + wct.toString());
			return wct;
		} else {
			WinCardType wct = new WinCardType(type, value,score, isDaHu, desc);
			map.put(type, wct);
			//Global.logger.info("putWinCardType: " + wct.toString());
			return wct;
		}
	}

	public boolean AnalyseChiHuCard(Map<Integer, WinCardType> map,  List<OpCard> opCards, ITObject config)
	{
		Map<Integer, Integer> tempCardList = new HashMap<Integer, Integer>();
		tempCardList.putAll(this.cbCardMap);

		if (AnalyseChiHuCardEx(map, tempCardList, 0, opCards, config))
		{
			return true;
		}

		return false;
	}

	boolean AnalyseChiHuCardEx(Map<Integer, WinCardType> map, Map<Integer, Integer> cardListParam, int curCard, List<OpCard> opCards, ITObject config)
	{
		Map<Integer, Integer> tempCardList = new HashMap<>();
		tempCardList.putAll(cardListParam);

		if (curCard > 0)
		{
			tempCardList.put(curCard, tempCardList.getOrDefault(curCard, 0)+1);
		}

		List<Integer> clearCardList = new ArrayList<>();
		for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
			if (entry.getValue() == 0)
			{
				clearCardList.add(entry.getKey());
			}
		}

		for(int i = 0; i < clearCardList.size(); i++)
		{
			tempCardList.remove(clearCardList.get(i));
		}

		//变量定义
		List<TagAnalyseItem> tagAnalyseItemList = new ArrayList<TagAnalyseItem>();
		List<Integer> wChiHuKind = new ArrayList<Integer>();

		//分析麻将
		AnalyseCard(tempCardList, opCards, tagAnalyseItemList, config);

		//胡牌分析
		if (tagAnalyseItemList.size() > 0)
		{
			//牌型分析
			for (int i = 0;i < tagAnalyseItemList.size();i++)
			{
				//变量定义
				boolean bLianCard=false,bPengCard=false,b258Card=false;
				TagAnalyseItem pAnalyseItem= tagAnalyseItemList.get(i);

				if (pAnalyseItem.huxi < 11)
				{
					if (this.zi_mo || this.zi_mo == false && config.getInt(Config.ROOM_CONFIG_QING_ZUI_HU) > 0)
					{
						boolean erxiaosheng = false;
						boolean bajiuzi = false;
						boolean jiazuoren = false;
						for (int j = 0;j < pAnalyseItem.cbOpCard.size(); j++) {
							OpCard cbWeaveKind = pAnalyseItem.cbOpCard.get(j);
							if (cbWeaveKind.type == RuleWeight.TYPE_CHOW) {
								if (cbWeaveKind.card % 100 == 5)
								{
									erxiaosheng = true;
								}
								else if (cbWeaveKind.card % 100 == 6)
								{
									bajiuzi = true;
								}
								else if (cbWeaveKind.card % 100 == 7)
								{
									jiazuoren = true;
								}
							}
						}

						if (erxiaosheng == false || bajiuzi == false || jiazuoren == false)
						{
							continue;
						}
					}
					else {
						continue;
					}
				}

				//牌型分析
				for (int j = 0;j < pAnalyseItem.cbOpCard.size(); j++) {
					OpCard cbWeaveKind = pAnalyseItem.cbOpCard.get(j);
					if (cbWeaveKind.type == RuleWeight.TYPE_PONG
							|| cbWeaveKind.type == RuleWeight.TYPE_KONG
							|| cbWeaveKind.type == RuleWeight.TYPE_SELF_KONG
							|| cbWeaveKind.type == RuleWeight.TYPE_ZHAO
							|| cbWeaveKind.type == RuleWeight.TYPE_SELF_ZHAO) {
						bPengCard = true;
					}

					if (cbWeaveKind.type == RuleWeight.TYPE_CHOW) {
						bLianCard = true;
					}
				}

				//牌型判断
				if (bLianCard==false && bPengCard==false)
				{
					return false;
				}

				if (bLianCard && bPengCard == false)
				{
					wChiHuKind.add(WinCardType.PINGHU);
					putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
				}
				else if (bLianCard && bPengCard)
				{
					if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
					{
						wChiHuKind.add(WinCardType.PINGHU);
						putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
					}
					else
					{
						wChiHuKind.add(WinCardType.JIHU);
						putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
					}
				}

				//特殊--碰碰胡
				if ((bLianCard==false)&&(bPengCard==true))
				{
					if (config.getBoolean(Config.ROOM_CONFIG_PENGPENGHU))
					{
						if (config.getBoolean(Config.ROOM_CONFIG_SPECIAL_PENG_PENG_HU))
						{
							if (pAnalyseItem.cbCardEye > 0 && pAnalyseItem.cbCardEyeList.size() == 0)
							{
								if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
								{
									wChiHuKind.add(WinCardType.PINGHU);
									putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
								}
								else
								{
									wChiHuKind.add(WinCardType.JIHU);
									putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
								}
							}
							else if (pAnalyseItem.cbCardEyeList.size() > 0)
							{
							    boolean flag = true;
								for(int x = 0; x < pAnalyseItem.cbCardEyeList.size(); x++)
								{
									ITArray xCardList = pAnalyseItem.cbCardEyeList.get(x);
									int card1 = xCardList.getInt(0);
									int card2 = xCardList.getInt(1);
									if (card1 != card2)
									{
										if (card1 % 100 != 4 && card2 % 100 != 4)
										{
											flag = false;
										}
									}
								}

								if (flag)
								{
									wChiHuKind.add(WinCardType.PENGPENGHU);
									putWinCardType(map, WinCardType.PENGPENGHU, 1,WinCardType.PENGPENGHU_SCORE, true, "碰碰胡");
								}
								else {
									if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
									{
										wChiHuKind.add(WinCardType.PINGHU);
										putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
									}
									else
									{
										wChiHuKind.add(WinCardType.JIHU);
										putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
									}
								}
							}
						}
						else {
							if (pAnalyseItem.cbCardEyeList.size() > 0)
							{
								boolean flag = true;
								for(int x = 0; x < pAnalyseItem.cbCardEyeList.size(); x++)
								{
									ITArray xCardList = pAnalyseItem.cbCardEyeList.get(x);
									int card1 = xCardList.getInt(0);
									int card2 = xCardList.getInt(1);
									if (card1 != card2)
									{
										if (card1 % 100 != 4 && card2 % 100 != 4)
										{
											flag = false;
										}
									}
								}

								if (flag)
								{
									wChiHuKind.add(WinCardType.PENGPENGHU);
									putWinCardType(map, WinCardType.PENGPENGHU, 1,WinCardType.PENGPENGHU_SCORE, true, "碰碰胡");
								}
								else {
									if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
									{
										wChiHuKind.add(WinCardType.PINGHU);
										putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
									}
									else
									{
										wChiHuKind.add(WinCardType.JIHU);
										putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
									}
								}
							}
							else {
								if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
								{
									wChiHuKind.add(WinCardType.PINGHU);
									putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
								}
								else
								{
									wChiHuKind.add(WinCardType.JIHU);
									putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
								}
							}
						}
					}
					else {
						if (config.getBoolean(Config.ROOM_CONFIG_JIHU_SHI_PINGHU))
						{
							wChiHuKind.add(WinCardType.PINGHU);
							putWinCardType(map, WinCardType.PINGHU, 1,WinCardType.PINGHU_SCORE, false, "平胡");
						}
						else
						{
							wChiHuKind.add(WinCardType.JIHU);
							putWinCardType(map, WinCardType.JIHU, 1,WinCardType.JIHU_SCORE, false, "鸡胡");
						}
					}
				}
			}
		}

		//结果判断
		if (wChiHuKind.size() > 0)
		{
			return true;
		}

		return false;
	}

	public boolean IsShangFu(int card)
	{
		if (card == 101 || card == 801)
		{
			return true;
		}
		return false;
	}

	public boolean IsDaRen(int card)
	{
		if (card == 102 || card == 103)
		{
			return true;
		}
		return false;
	}

	boolean IsLuShou(int card)
	{
		if (card == 802 || card == 803)
		{
			return true;
		}
		return false;
	}

	public boolean IsDaRenLuShou(int card)
	{
		if (card == 102 || card == 103 || card == 802 || card == 803)
		{
			return true;
		}
		return false;
	}

	public boolean IsShangDaRen(int card)
	{
		if (card / 100 == 1)
		{
			return true;
		}
		return false;
	}

	public boolean IsFuLuShou(int card)
	{
		if (card / 100 == 8)
		{
			return true;
		}
		return false;
	}

	public boolean IsShangDaRenFuLuShou(int card)
	{
		if (card / 100 == 1 || card / 100 == 8)
		{
			return true;
		}
		return false;
	}

	public boolean AnalyseCardFuLuShou(int cbLessKindItem, TagKindItem[] pKindItem, int eyeCard, List<OpCard> opCards, List<TagAnalyseItem> tagAnalyseItemList, ITObject config) {
		//变量定义
		TagAnalyseItem AnalyseItem = new TagAnalyseItem();

		//设置结果
		for (int i = 0; i < opCards.size(); i++) {
			OpCard src_card = opCards.get(i);
			OpCard op_card = new OpCard(src_card.type, src_card.card);
			AnalyseItem.cbOpCard.add(src_card);
			if (src_card.type == RuleWeight.TYPE_CHOW)
			{
				if (IsShangDaRenFuLuShou(src_card.card))
				{
					AnalyseItem.huxi += 4;
				}
			}
			else if (src_card.type == RuleWeight.TYPE_PONG)
			{
				if (IsShangFu(src_card.card))
				{
					AnalyseItem.huxi += 3 * 4;
				}
				else {
					AnalyseItem.huxi += 2;
				}
			}
			else
			{
				if (IsShangFu(src_card.card))
				{
					AnalyseItem.huxi += 4 * 4;
				}
				else {
					if (config.getBoolean(Config.ROOM_CONFIG_GANG_ZHAO_LIU_XI))
					{
						AnalyseItem.huxi += 6;
					}
					else {
						AnalyseItem.huxi += 4;
					}
				}
			}
		}

		//设置牌型
		boolean useOther = false;
		boolean usePong = false;
		if (pKindItem != null)
		{
			for (int i = 0; i < cbLessKindItem; i++) {
				OpCard op_card = new OpCard(pKindItem[i].type, pKindItem[i].card);
				AnalyseItem.cbOpCard.add(op_card);
				if (op_card.type == RuleWeight.TYPE_CHOW)
				{
					if (IsShangDaRenFuLuShou(op_card.card))
					{
						AnalyseItem.huxi += 4;
					}
					if (!this.selfCard && (pKindItem[i].cbCardList[0] == this.addCard) || (pKindItem[i].cbCardList[1] == this.addCard) || (pKindItem[i].cbCardList[2] == this.addCard))
					{
						useOther = true;
					}
				}
				else if (op_card.type == RuleWeight.TYPE_PONG)
				{
				    if (IsShangFu(op_card.card))
					{
						AnalyseItem.huxi += 3 * 4;
					}
					else {
						AnalyseItem.huxi += 3;
						if (!this.selfCard && op_card.card == this.addCard)
						{
							usePong = true;
						}
					}
				}
				else
				{
					if (!this.selfCard && op_card.card == this.addCard)
					{
						useOther = true;
					}
					if (IsShangFu(op_card.card))
					{
						AnalyseItem.huxi += 4 * 4;
					}
					else {
						if (config.getBoolean(Config.ROOM_CONFIG_GANG_ZHAO_LIU_XI))
						{
							AnalyseItem.huxi += 6;
						}
						else {
							AnalyseItem.huxi += 4;
						}
					}
				}
			}
		}

		if (eyeCard > 0) {
			AnalyseItem.cbCardEye = eyeCard;
			if (!this.selfCard && eyeCard == this.addCard)
			{
				useOther = true;
			}
			if (IsShangFu(eyeCard))
			{
				AnalyseItem.huxi += 8;
			}
			else if (IsDaRenLuShou(eyeCard)) {
				AnalyseItem.huxi += 4;
			}
			else if (eyeCard == 104 || eyeCard == 804)
			{
				AnalyseItem.huxi += 8;
			}
		}

		if (useOther == false && usePong)
		{
			AnalyseItem.huxi -= 1;
		}

		//插入结果
		tagAnalyseItemList.add(AnalyseItem);
		if (AnalyseItem.huxi >= 11)
		{
			return true;
		}
		return false;
	}

	public boolean AnalyseCardFuLuShouJiang(int cbLessKindItem, TagKindItem[] pKindItem, int card1, int card2, int card3, int card4, List<OpCard> opCards, List<TagAnalyseItem> tagAnalyseItemList, ITObject config) {
		//变量定义
		TagAnalyseItem AnalyseItem = new TagAnalyseItem();

		//设置结果
		//设置结果
		boolean diJin = false;
		boolean shangDaRenFuLuShou = false;
		boolean ban_shangDaRenFuLuShou = false;
		boolean hasKan = false;
		for (int i = 0; i < opCards.size(); i++) {
			OpCard src_card = opCards.get(i);
			OpCard op_card = new OpCard(src_card.type, src_card.card);
			AnalyseItem.cbOpCard.add(src_card);
			if (src_card.type == RuleWeight.TYPE_CHOW)
			{
				if (IsShangDaRenFuLuShou(src_card.card))
				{
					shangDaRenFuLuShou = true;
					AnalyseItem.huxi += 4;
				}
			}
			else if (src_card.type == RuleWeight.TYPE_PONG)
			{
				hasKan = true;
				if (IsShangFu(src_card.card))
				{
					AnalyseItem.huxi += 3 * 4;
				}
				else {
					AnalyseItem.huxi += 2;
				}
			}
			else
			{
				hasKan = true;
				if (IsShangFu(src_card.card))
				{
					AnalyseItem.huxi += 4 * 4;
				}
				else {
					if (config.getBoolean(Config.ROOM_CONFIG_GANG_ZHAO_LIU_XI))
					{
						AnalyseItem.huxi += 6;
					}
					else {
						AnalyseItem.huxi += 4;
					}
				}
			}
		}

		//设置牌型
		boolean useOther = false;
		boolean usePong = false;
		if (pKindItem != null)
		{
			for (int i = 0; i < cbLessKindItem; i++) {
				OpCard op_card = new OpCard(pKindItem[i].type, pKindItem[i].card);
				AnalyseItem.cbOpCard.add(op_card);
				if (op_card.type == RuleWeight.TYPE_CHOW)
				{
					if (IsShangDaRenFuLuShou(op_card.card))
					{
						shangDaRenFuLuShou = true;
						AnalyseItem.huxi += 4;
					}
					if (!this.selfCard && (pKindItem[i].cbCardList[0] == this.addCard) || (pKindItem[i].cbCardList[1] == this.addCard) || (pKindItem[i].cbCardList[2] == this.addCard))
					{
						useOther = true;
					}
				}
				else if (op_card.type == RuleWeight.TYPE_PONG)
				{
					hasKan = true;
					if (IsShangFu(op_card.card))
					{
						AnalyseItem.huxi += 3 * 4;
					}
					else {
						AnalyseItem.huxi += 3;
						if (!this.selfCard && op_card.card == this.addCard)
						{
							usePong = true;
						}
					}
				}
				else
				{
					if (!this.selfCard && op_card.card == this.addCard)
					{
						useOther = true;
					}
					hasKan = true;
					if (IsShangFu(op_card.card))
					{
						AnalyseItem.huxi += 4 * 4;
					}
					else {
						if (config.getBoolean(Config.ROOM_CONFIG_GANG_ZHAO_LIU_XI))
						{
							AnalyseItem.huxi += 6;
						}
						else {
							AnalyseItem.huxi += 4;
						}
					}
				}
			}
		}

		ITArray eyeCard1 = new TArray();
		eyeCard1.addInt(card1);
		eyeCard1.addInt(card2);
		AnalyseItem.cbCardEyeList.add(eyeCard1);

		ITArray eyeCard2 = new TArray();
		eyeCard2.addInt(card3);
		eyeCard2.addInt(card4);
		AnalyseItem.cbCardEyeList.add(eyeCard2);

		if (!this.selfCard && (card1 == this.addCard || card2 == this.addCard || card3 == this.addCard || card4 == this.addCard))
		{
			useOther = true;
		}

		//一对上、福，就可以胡了
		if (card1 == card2 && IsShangFu(card1))
		{
			AnalyseItem.huxi += 12;
		}
		else if (card1 == card2 && (card1 == 104 || card1 == 804))
		{
			AnalyseItem.huxi += 12;
		}
		else if (card1 == 101 && card2 == 104 || card2 == 101 && card1 == 104)
		{
			AnalyseItem.huxi += 12;
		}
		else if (card1 == 801 && card2 == 804 || card2 == 801 && card1 == 804)
		{
			AnalyseItem.huxi += 12;
		}
		else if ((card1 == 101 || card2 == 101 ) && card1 != card2)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card1 == 104 || card2 == 104 ) && card1 != card2)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card1 == 801 || card2 == 801 ) && card1 != card2)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card1 == 804 || card2 == 804 ) && card1 != card2)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}

		if (card3 == card4 && IsShangFu(card3))
		{
			AnalyseItem.huxi += 12;
		}
		else if (card3 == card4 && (card3 == 104 || card3 == 804))
		{
			AnalyseItem.huxi += 12;
		}
		else if (card3 == 101 && card4 == 104 || card4 == 101 && card3 == 104)
		{
			AnalyseItem.huxi += 12;
		}
		else if (card3 == 801 && card4 == 804 || card4 == 801 && card3 == 804)
		{
			AnalyseItem.huxi += 12;
		}
		else if ((card3 == 101 || card4 == 101) && card3 != card4)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card3 == 104 || card4 == 104) && card3 != card4)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card3 == 801 || card4 == 801) && card3 != card4)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}
		else if ((card3 == 804 || card4 == 804) && card3 != card4)
		{
			ban_shangDaRenFuLuShou = true;
			AnalyseItem.huxi += 4;
		}

		if (shangDaRenFuLuShou || ban_shangDaRenFuLuShou || hasKan) {
			if (card1 != card2 && IsDaRen(card1) && IsDaRen(card2) || card3 != card4 && IsDaRen(card3) && IsDaRen(card4))
			{
				if (!diJin) {
					diJin = true;
					AnalyseItem.huxi += 4;
				}
			}
			if (card1 != card2 && IsLuShou(card1) && IsLuShou(card2) || card3 != card4 && IsLuShou(card3) && IsLuShou(card4))
			{
				if (!diJin) {
					diJin = true;
					AnalyseItem.huxi += 4;
				}
			}
			if ((card1 == card2 && !IsShangFu(card1) && card1 != 104 && card1 != 804) ||
					(card3 == card4 && !IsShangFu(card3) && card3 != 104 && card3 != 804))
			{
				if (!diJin) {
					diJin = true;
					AnalyseItem.huxi += 3;
				}
			}
			else if ((card1 != card2 && ((card1 / 100 != 1 && card1 / 100 != 8 && card1 % 100 == 4) || (card2 / 100 != 1 && card2 / 100 != 8 && card2 % 100 == 4)) && !IsShangFu(card1)) ||
					(card3 != card4 && ((card3 / 100 != 1 && card3 / 100 != 8 && card3 % 100 == 4) || (card4 / 100 != 1 && card4 / 100 != 8 && card4 % 100 == 4)) && !IsShangFu(card3)))
			{
				if (!diJin) {
					diJin = true;
					AnalyseItem.huxi += 3;
				}
			}
		}

		if (useOther == false && usePong)
		{
			AnalyseItem.huxi -= 1;
		}

		//插入结果
		tagAnalyseItemList.add(AnalyseItem);
		if (AnalyseItem.huxi >= 11)
		{
			return true;
		}
		return false;
	}

	boolean AnalyseCard(Map<Integer, Integer> tempCardList, List<OpCard> opCards, List<TagAnalyseItem> tagAnalyseItemList, ITObject config) {
		//计算数目
		int cbCardCount = 0;
		for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
			cbCardCount += entry.getValue();
		}

		//效验数目
		if ((cbCardCount < 1) || (cbCardCount > 19)) {
			return false;
		}

		//变量定义
		List<TagKindItem> tagKindList = new ArrayList<TagKindItem>();

		do {
			int cbLessKindItem = (cbCardCount - 1) / 3;
			if (cbLessKindItem + opCards.size() != 6) {
				break;
			}

			//单吊判断
			if (cbLessKindItem == 0) {
				//效验参数
				if (!((cbCardCount == 1) && (opCards.size() == 6))) {
					return false;
				}

				//牌眼判断
				for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
					int card = entry.getKey();
					int num = entry.getValue();
					if (num == 1) {
						return AnalyseCardFuLuShou(cbLessKindItem, null, card, opCards, tagAnalyseItemList, config);
					}
				}

				return false;
			}

			//拆分分析
			if (cbCardCount >= 3) {
				for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
					int card = entry.getKey();
					int num = entry.getValue();
					//同牌判断
					if (num >= 2 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) == 1) {
						TagKindItem kindItem = new TagKindItem();
						kindItem.type = RuleWeight.TYPE_PONG;
						kindItem.card = card;
						kindItem.cbCardList[0] = card;
						kindItem.cbCardList[1] = card;
						kindItem.cbCardList[2] = card / 100 * 100 + 4;
						tagKindList.add(kindItem);
					}

					if (num >= 3) {
						TagKindItem kindItem = new TagKindItem();
						kindItem.type = RuleWeight.TYPE_PONG;
						kindItem.card = card;
						kindItem.cbCardList[0] = card;
						kindItem.cbCardList[1] = card;
						kindItem.cbCardList[2] = card;
						tagKindList.add(kindItem);
					}

					//连牌判断
					if (card % 100 >= 1 && card % 100 <= 4 && num > 0) {
						for (int i = 1; i <= num; i++) {
							if (tempCardList.getOrDefault(card + 1, 0) >= i && tempCardList.getOrDefault(card + 2, 0) >= i && (card+1)%100 != 4 && (card+2)%100 != 4) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card + 1;
								kindItem.cbCardList[2] = card + 2;
								tagKindList.add(kindItem);
							}
							if (tempCardList.getOrDefault(card + 1, 0) >= i && (card+1)%100 != 4 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) >= i) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card + 1;
								kindItem.cbCardList[2] = card / 100 * 100 + 4;
								tagKindList.add(kindItem);
							}
							if (tempCardList.getOrDefault(card + 2, 0) >= i && (card+2)%100 != 4 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) >= i) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card / 100 * 100 + 4;
								kindItem.cbCardList[2] = card + 2;
								tagKindList.add(kindItem);
							}
						}
					}
				}
			}

			//组合分析
			if (tagKindList.size() >= cbLessKindItem) {
				//变量定义
				Integer[] cbIndex = new Integer[6]; //{0,1,2,3};
				cbIndex[0] = 0;
				cbIndex[1] = 1;
				cbIndex[2] = 2;
				cbIndex[3] = 3;
				cbIndex[4] = 4;
				cbIndex[5] = 5;

				TagKindItem[] pKindItem = new TagKindItem[6];
				pKindItem[0] = null;
				pKindItem[1] = null;
				pKindItem[2] = null;
				pKindItem[3] = null;
				pKindItem[4] = null;
				pKindItem[5] = null;

				int count = 0;
				//开始组合
				do {
					count++;
					//设置变量
					Map<Integer, Integer> cbCardMap_temp = new HashMap<Integer, Integer>();
					cbCardMap_temp.putAll(tempCardList);

					for (int i = 0; i < cbLessKindItem; i++) {
						pKindItem[i] = tagKindList.get(cbIndex[i]);
					}

					//数量判断
					boolean bEnoughCard = true;
					for (int i = 0; i < cbLessKindItem * 3; i++) {
						//存在判断
						int cbCardIndex = pKindItem[i / 3].cbCardList[i % 3];
						if (cbCardMap_temp.getOrDefault(cbCardIndex, 0) <= 0) {
							bEnoughCard = false;
							break;
						} else {
							if (cbCardMap_temp.containsKey(cbCardIndex)) {
								cbCardMap_temp.put(cbCardIndex, cbCardMap_temp.get(cbCardIndex) - 1);
							}
						}
					}

					//胡牌判断
					if (bEnoughCard == true) {
						//牌眼判断
						int cbCardEye = 0;
						for (Map.Entry<Integer, Integer> entry : cbCardMap_temp.entrySet()) {
							int card = entry.getKey();
							int num = entry.getValue();
							if (num == 1) {
								cbCardEye = card;
								break;
							}
						}

						//组合类型
						if (cbCardEye != 0) {
							boolean result = AnalyseCardFuLuShou(cbLessKindItem, pKindItem, cbCardEye, opCards, tagAnalyseItemList, config);
							if (ting_pai && result)
							{
								return true;
							}
						}
					}

					//设置索引
					if (cbIndex[cbLessKindItem - 1] == (tagKindList.size() - 1)) {
						int i = cbLessKindItem - 1;
						for (; i > 0; i--) {
							if ((cbIndex[i - 1] + 1) != cbIndex[i]) {
								int cbNewIndex = cbIndex[i - 1];
								for (int j = (i - 1); j < cbLessKindItem; j++) {
									cbIndex[j] = cbNewIndex + j - i + 2;
								}
								break;
							}
						}
						if (i == 0) break;
					} else {
						cbIndex[cbLessKindItem - 1]++;
					}

				} while (true);
				//Global.logger.info("count:" + count);
			}

			if (tagAnalyseItemList.size() > 0) {
				break;
			}
		}
		while (false);
		tagKindList.clear();

		do {
			int cbLessKindItem = (cbCardCount - 4) / 3;
			if (cbLessKindItem + opCards.size() != 5) {
				break;
			}

			//单吊判断
			if (cbLessKindItem == 0) {
				//效验参数
				if (!((cbCardCount == 4) && (opCards.size() == 5))) {
					return false;
				}

				//牌眼判断
				ArrayList<ITArray> eyeList = new ArrayList<ITArray>();
				ArrayList<Integer> eyeCardList = new ArrayList<Integer>();
				for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
					int card = entry.getKey();
					int num = entry.getValue();
					if (num > 0) {
						for(int i = 0; i < num; i++)
						{
							eyeCardList.add(card);
						}
					}
				}

				if (eyeCardList.size() != 4)
				{
					return false;
				}

				if (eyeCardList.get(0) / 100 == eyeCardList.get(1) / 100 && eyeCardList.get(2) / 100 == eyeCardList.get(3) / 100)
				{
					int card1 = eyeCardList.get(0);
					int card2 = eyeCardList.get(1);
					int card3 = eyeCardList.get(2);
					int card4 = eyeCardList.get(3);

					boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, null, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
					if (ting_pai && result)
					{
					    return true;
					}
				}

				if (eyeCardList.get(0) / 100 == eyeCardList.get(2) / 100 && eyeCardList.get(1) / 100 == eyeCardList.get(3) / 100)
				{
					int card1 = eyeCardList.get(0);
					int card2 = eyeCardList.get(2);
					int card3 = eyeCardList.get(1);
					int card4 = eyeCardList.get(3);

					boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, null, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
					if (ting_pai && result)
					{
						return true;
					}
				}

				if (eyeCardList.get(0) / 100 == eyeCardList.get(3) / 100 && eyeCardList.get(2) / 100 == eyeCardList.get(1) / 100)
				{
					int card1 = eyeCardList.get(0);
					int card2 = eyeCardList.get(3);
					int card3 = eyeCardList.get(2);
					int card4 = eyeCardList.get(1);

					boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, null, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
					if (ting_pai && result)
					{
						return true;
					}
				}

				if (tagAnalyseItemList.size() > 0)
				{
					return true;
				}
				else {
					return false;
				}
			}

			//拆分分析
			if (cbCardCount >= 3) {
				for (Map.Entry<Integer, Integer> entry : tempCardList.entrySet()) {
					int card = entry.getKey();
					int num = entry.getValue();
					//同牌判断
					if (num >= 2 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) == 1) {
						TagKindItem kindItem = new TagKindItem();
						kindItem.type = RuleWeight.TYPE_PONG;
						kindItem.card = card;
						kindItem.cbCardList[0] = card;
						kindItem.cbCardList[1] = card;
						kindItem.cbCardList[2] = card / 100 * 100 + 4;
						tagKindList.add(kindItem);
					}

					if (num >= 3) {
						TagKindItem kindItem = new TagKindItem();
						kindItem.type = RuleWeight.TYPE_PONG;
						kindItem.card = card;
						kindItem.cbCardList[0] = card;
						kindItem.cbCardList[1] = card;
						kindItem.cbCardList[2] = card;
						tagKindList.add(kindItem);
					}

					//连牌判断
					if (card % 100 >= 1 && card % 100 <= 4 && num > 0) {
						for (int i = 1; i <= num; i++) {
							if (tempCardList.getOrDefault(card + 1, 0) >= i && tempCardList.getOrDefault(card + 2, 0) >= i && (card+1)%100 != 4 && (card+2)%100 != 4) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card + 1;
								kindItem.cbCardList[2] = card + 2;
								tagKindList.add(kindItem);
							}
							if (tempCardList.getOrDefault(card + 1, 0) >= i && (card+1)%100 != 4 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) >= i) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card + 1;
								kindItem.cbCardList[2] = card / 100 * 100 + 4;
								tagKindList.add(kindItem);
							}
							if (tempCardList.getOrDefault(card + 2, 0) >= i && (card+2)%100 != 4 && tempCardList.getOrDefault(card / 100 * 100 + 4, 0) >= i) {
								TagKindItem kindItem = new TagKindItem();
								kindItem.type = RuleWeight.TYPE_CHOW;
								kindItem.card = card;
								kindItem.cbCardList[0] = card;
								kindItem.cbCardList[1] = card / 100 * 100 + 4;
								kindItem.cbCardList[2] = card + 2;
								tagKindList.add(kindItem);
							}
						}
					}
				}
			}

			//组合分析
			if (tagKindList.size() >= cbLessKindItem) {
				//变量定义
				Integer[] cbIndex = new Integer[5]; //{0,1,2,3};
				cbIndex[0] = 0;
				cbIndex[1] = 1;
				cbIndex[2] = 2;
				cbIndex[3] = 3;
				cbIndex[4] = 4;

				TagKindItem[] pKindItem = new TagKindItem[5];
				pKindItem[0] = null;
				pKindItem[1] = null;
				pKindItem[2] = null;
				pKindItem[3] = null;
				pKindItem[4] = null;

				//开始组合
				int count = 0;
				do {
					count++;
					//设置变量
					Map<Integer, Integer> cbCardMap_temp = new HashMap<Integer, Integer>();
					cbCardMap_temp.putAll(tempCardList);

					for (int i = 0; i < cbLessKindItem; i++) {
						pKindItem[i] = tagKindList.get(cbIndex[i]);
					}

					//数量判断
					boolean bEnoughCard = true;
					for (int i = 0; i < cbLessKindItem * 3; i++) {
						//存在判断
						int cbCardIndex = pKindItem[i / 3].cbCardList[i % 3];
						if (cbCardMap_temp.getOrDefault(cbCardIndex, 0) <= 0) {
							bEnoughCard = false;
							break;
						} else {
							if (cbCardMap_temp.containsKey(cbCardIndex)) {
								cbCardMap_temp.put(cbCardIndex, cbCardMap_temp.get(cbCardIndex) - 1);
							}
						}
					}

					//胡牌判断
					if (bEnoughCard == true) {
						//牌眼判断
						ArrayList<ITArray> eyeList = new ArrayList<ITArray>();
						ArrayList<Integer> eyeCardList = new ArrayList<Integer>();
						for (Map.Entry<Integer, Integer> entry : cbCardMap_temp.entrySet()) {
							int card = entry.getKey();
							int num = entry.getValue();
							if (num > 0) {
								for(int i = 0; i < num; i++)
								{
									eyeCardList.add(card);
								}
							}
						}

						if (eyeCardList.size() != 4)
						{
							return false;
						}

						if (eyeCardList.get(0) / 100 == eyeCardList.get(1) / 100 && eyeCardList.get(2) / 100 == eyeCardList.get(3) / 100)
						{
							int card1 = eyeCardList.get(0);
							int card2 = eyeCardList.get(1);
							int card3 = eyeCardList.get(2);
							int card4 = eyeCardList.get(3);

							boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, pKindItem, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
							if (ting_pai && result)
							{
								return true;
							}
						}

						if (eyeCardList.get(0) / 100 == eyeCardList.get(2) / 100 && eyeCardList.get(1) / 100 == eyeCardList.get(3) / 100)
						{
							int card1 = eyeCardList.get(0);
							int card2 = eyeCardList.get(2);
							int card3 = eyeCardList.get(1);
							int card4 = eyeCardList.get(3);

							boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, pKindItem, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
							if (ting_pai && result)
							{
								return true;
							}
						}

						if (eyeCardList.get(0) / 100 == eyeCardList.get(3) / 100 && eyeCardList.get(2) / 100 == eyeCardList.get(1) / 100)
						{
							int card1 = eyeCardList.get(0);
							int card2 = eyeCardList.get(3);
							int card3 = eyeCardList.get(2);
							int card4 = eyeCardList.get(1);

							boolean result = AnalyseCardFuLuShouJiang(cbLessKindItem, pKindItem, card1, card2, card3, card4, opCards, tagAnalyseItemList, config);
							if (ting_pai && result)
							{
								return true;
							}
						}
					}

					//设置索引
					if (cbIndex[cbLessKindItem - 1] == (tagKindList.size() - 1)) {
						int i = cbLessKindItem - 1;
						for (; i > 0; i--) {
							if ((cbIndex[i - 1] + 1) != cbIndex[i]) {
								int cbNewIndex = cbIndex[i - 1];
								for (int j = (i - 1); j < cbLessKindItem; j++) {
									cbIndex[j] = cbNewIndex + j - i + 2;
								}
								break;
							}
						}
						if (i == 0) break;
					} else {
						cbIndex[cbLessKindItem - 1]++;
					}

				} while (true);
				//Global.logger.info("count:" + count);
			}

			if (tagAnalyseItemList.size() > 0) {
				break;
			}
		}
		while (false);

		if (tagAnalyseItemList.size() > 0) {
			return true;
		}
		return false;
	}
}
