package extend.pk.uitl;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import extend.pk.CardObj;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CardUtil {


	/**
	 * list to TArray
	 * 
	 * @param list
	 * @return
	 */
	public static final ITArray toTArray(List<CardObj> list) {
		ITArray result = new TArray();
		for (CardObj card : list) {
			result.addInt(card.card);
		}
		return result;
	}
	
	public static final CardObj getCard(int eventCard,List<CardObj> cardList) {
		for (CardObj card : cardList) {
			if(card.cardMod == eventCard) {
				return card;
			}
		}
		return null;
	}
	
	public static final CardObj getCard1(int eventCard,List<CardObj> cardList) {
		for (CardObj card : cardList) {
			if(card.card == eventCard) {
				return card;
			}
		}
		return null;
	}
	
	/**
	 * 检测牌数量
	 * 
	 * @param eventCard
	 * @param cardList
	 * @param num
	 * @return
	 */
	public static final boolean checkCard(int eventCard, List<CardObj> cardList, int num) {
		int result = 0;
		for (CardObj card : cardList) {
			if (card.cardMod == eventCard) {
				result++;
				if (result == num)
					return true;
			}
		}
		return false;
	}

	public static final boolean checkGoodCard(int eventCard, List<CardObj> cardList, int num) {
		int result = 0;
		for (CardObj card : cardList) {
			if (card.cardMod == eventCard) {
				result++;
				if (result >= num)
					return true;
			}
		}
		return false;
	}

	public static final boolean checkShunZi(int eventCard, List<CardObj> cardList) {
		int result = 0;
		if (checkGoodCard(eventCard+1, cardList, 1))
		{
			result++;
			if (checkGoodCard(eventCard+2, cardList, 1))
			{
				result++;
				if (checkGoodCard(eventCard+3, cardList, 1))
				{
					result++;
					if (checkGoodCard(eventCard+4, cardList, 1))
					{
						result++;
						if (checkGoodCard(eventCard+5, cardList, 1))
						{
							result++;
						}
					}
				}
			}
		}
		if (checkGoodCard(eventCard-1, cardList, 1))
		{
			result++;
			if (checkGoodCard(eventCard-2, cardList, 1))
			{
				result++;
				if (checkGoodCard(eventCard-3, cardList, 1))
				{
					result++;
					if (checkGoodCard(eventCard-4, cardList, 1))
					{
						result++;
						if (checkGoodCard(eventCard-5, cardList, 1))
						{
							result++;
						}
					}
				}
			}
		}

		if (result >= 4)
		{
			return true;
		}
		return false;
	}

	public static final boolean checkSevenShunzi(int eventCard, List<CardObj> cardList) {
		int result = 0;
		if (checkGoodCard(eventCard+1, cardList, 1))
		{
			result++;
			if (checkGoodCard(eventCard+2, cardList, 1))
			{
				result++;
				if (checkGoodCard(eventCard+3, cardList, 1))
				{
					result++;
					if (checkGoodCard(eventCard+4, cardList, 1))
					{
						result++;
						if (checkGoodCard(eventCard+5, cardList, 1))
						{
							result++;
							if (checkGoodCard(eventCard+6, cardList, 1))
							{
								result++;
								if (checkGoodCard(eventCard+7, cardList, 1))
								{
									result++;
								}
							}
						}
					}
				}
			}
		}
		if (checkGoodCard(eventCard-1, cardList, 1))
		{
			result++;
			if (checkGoodCard(eventCard-2, cardList, 1))
			{
				result++;
				if (checkGoodCard(eventCard-3, cardList, 1))
				{
					result++;
					if (checkGoodCard(eventCard-4, cardList, 1))
					{
						result++;
						if (checkGoodCard(eventCard-5, cardList, 1))
						{
							result++;
							if (checkGoodCard(eventCard-6, cardList, 1))
							{
								result++;
								if (checkGoodCard(eventCard-7, cardList, 1))
								{
									result++;
								}
							}
						}
					}
				}
			}
		}

		if (result >= 6)
		{
			return true;
		}
		return false;
	}

	public static final boolean checkFenJi(int eventCard, List<CardObj> cardList) {
		int result = 0;
		if (checkGoodCard(eventCard, cardList, 2))
		{
			result++;
			if (checkGoodCard(eventCard+1, cardList, 3))
			{
				result++;
			}
			if (checkGoodCard(eventCard-1, cardList, 3))
			{
				result++;
			}
		}

		if (result >= 2)
		{
			return true;
		}
		return false;
	}

	public static final boolean checkFourDui(int eventCard, List<CardObj> cardList) {
		int result = 0;
		if (checkGoodCard(eventCard, cardList, 1))
		{
			result++;
		}

		if (checkGoodCard(eventCard+1, cardList, 2))
		{
			result++;
			if (checkGoodCard(eventCard+2, cardList, 2))
			{
				result++;
				if (checkGoodCard(eventCard+3, cardList, 2))
				{
					result++;
					if (checkGoodCard(eventCard+4, cardList, 2))
					{
						result++;
					}
				}
			}
		}
		if (checkGoodCard(eventCard-1, cardList, 2))
		{
			result++;
			if (checkGoodCard(eventCard-2, cardList, 2))
			{
				result++;
				if (checkGoodCard(eventCard-3, cardList, 2))
				{
					result++;
					if (checkGoodCard(eventCard-4, cardList, 2))
					{
						result++;
					}
				}
			}
		}

		if (result >= 4)
		{
			return true;
		}
		return false;
	}

	public static final boolean checkQPai(int eventCard, List<CardObj> cardList) {
		int result = 0;
		if (eventCard >= 12)
		{
			result++;
			for (CardObj card : cardList) {
				if (card.cardMod >= 12) {
					result++;
				}
			}
		}

		if (result >= 5)
		{
			return true;
		}
	    return false;
	}

	/**
	 * TArray to list
	 * @param list
	 * @return
	 */
	public static final List<CardObj> toList(ITArray list) {
		List<CardObj> tem = new ArrayList<CardObj>();
		for(int i=0;i<list.size();++i) {
			tem.add(new CardObj(list.getInt(i)));
		}
		return tem;
	}
	
	/**
	 * 获取每张牌的数量MAP
	 * 
	 * @param cardList
	 * @return
	 */
	public static final Map<Integer, Integer> getCardNumMap(List<CardObj> cardList) {
		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		for (CardObj card : cardList) {
			if (!result.containsKey(card.cardMod)) {
				result.put(card.cardMod, 1);
			} else {
				int num = result.get(card.cardMod);
				result.put(card.cardMod, (num + 1));
			}
		}
		return result;
	}
	
	/**
	 * 获取每张牌的数量MAP
	 * 
	 * @param cardList
	 * @return
	 */
	public static final void getCardNumMap(Map<Integer, Integer> result,List<CardObj> cardList) {
		result.clear();
		for (CardObj card : cardList) {
			if (!result.containsKey(card.cardMod)) {
				result.put(card.cardMod, 1);
			} else {
				int num = result.get(card.cardMod);
				result.put(card.cardMod, (num + 1));
			}
		}
	}

	public static final void getCardNumMap(Map<Integer, Integer> result,List<CardObj> cardList, CardObj tempCard) {
		result.clear();
		result.put(tempCard.cardMod, 1);
		for (CardObj card : cardList) {
			if (!result.containsKey(card.cardMod)) {
				result.put(card.cardMod, 1);
			} else {
				int num = result.get(card.cardMod);
				result.put(card.cardMod, (num + 1));
			}
		}
	}
	
	/**
	 * 获取每张牌的数量MAP
	 * 
	 * @param cardList
	 * @return
	 */
	public static final Map<Integer, List<CardObj>> getCardListMap(List<CardObj> cardList) {
		Map<Integer, List<CardObj>> result = new HashMap<Integer, List<CardObj>>();
		for (CardObj card : cardList) {
			if (!result.containsKey(card.cardMod)) {
				List<CardObj> list = new ArrayList<CardObj>();
				list.add(card);
				result.put(card.cardMod, list);
			} else {
				List<CardObj> list = result.get(card.cardMod);
				list.add(card);
			}
		}
		return result;
	}
	
	static public void removeCard(List<CardObj> cardList, List<CardObj> cards) {
		for (int i = 0; i < cards.size(); i++) {
			for (int j = 0; j < cardList.size(); j++) {
				if (cardList.get(j).card ==cards.get(i).card) {
					cardList.remove(j);
					break;
				}
			}
		}
	}
}
