package taurus.util;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;

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

public class CardUtil {


    // 补充：找最小单牌（如未实现）
    public static CardObj findMinSingleCard(List<CardObj> cardList) {
        if (cardList == null || cardList.isEmpty()) {
            return null;
        }
        CardObj minCard = cardList.get(0);
        for (CardObj card : cardList) {
            if (card.getCardMod() < minCard.getCardMod()) {
                minCard = card;
            }
        }
        return minCard;
    }

    /**
     * 从手牌中找出最大的单牌
     *
     * @param handCards 手牌列表
     * @return 最大的单牌，如果没有单牌则返回null
     */
    public static CardObj findMaxSingleCard(List<CardObj> handCards) {
        if (handCards == null || handCards.isEmpty()) {
            return null;
        }
        handCards.sort((c1, c2) -> c2.cardMod - c1.cardMod);
        return handCards.get(0);
    }


    /**
     * 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 ITArray toTArray1(CardObj list) {
        ITArray result = new TArray();
        result.addInt(list.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;
    }

    /**
     * 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 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;
                }
            }
        }
    }

}
