package extend.zp.uitl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.taurus.core.entity.ITArray;

import extend.zp.OpCard;

public class CardUtil {
	static public int cardNum(int eventCard, List<Integer> cardList) {
		int result = 0;
		for (Integer card : cardList) {
			if (card == eventCard) {
				result += 1;
			}
		}
		return result;
	}
	
	static public boolean checkCard(int eventCard,Map<Integer, Integer> cardMap) {
		if(cardMap.containsKey(eventCard)&&cardMap.get(eventCard)>=1) {
			return true;
		}
		return false;
	}
	
	static public void addCard(int eventCard,Map<Integer, Integer> cardMap,int add) {
		if(cardMap.containsKey(eventCard)) {
			int num = cardMap.get(eventCard);
			cardMap.put(eventCard, num+add);
		}else if(add >0){
			cardMap.put(eventCard, add);
		}
	}

	static public int cardType(int card) {
		return card / 100;
	}

	static public boolean isRedCard(int card) {
		return card % 100 == 2 || card % 100 == 7 || card % 100 == 10;
	}

	static public Map<Integer, Integer> getCardNumMap(List<Integer> cardList) {
		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		for (Integer card : cardList) {
			if (!result.containsKey(card)) {
				result.put(card, 1);
			} else {
				int num = result.get(card);
				result.put(card, (num + 1));
			}
		}
		return result;
	}

	static public void removeCard(List<Integer> cardList, int card, int count) {
		int curCount = 0;
		for (int i = 0; i < cardList.size(); i++) {
			if (count == curCount) {
				return;
			}
			if (cardList.get(i) == card) {

				cardList.remove(i);
				i--;
				curCount++;
			}
		}
	}

	static public void removeGroup(List<int[]> group, int card) {
		for (int i = 0; i < group.size(); i++) {
			int[] cardArray = group.get(i);
			if (cardArray[0] == card) {
				group.remove(cardArray);
				return;
			}
		}
	}

	static public void removeOpcard(List<OpCard> opcards, OpCard param) {
		for (int i = 0; i < opcards.size(); i++) {
			if (param.card1 == opcards.get(i).card1 && param.card2 == opcards.get(i).card2 && param.card3 == opcards.get(i).card3) {
				opcards.remove(i);
				return;
			}
		}
	}

	public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
		ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(byteOut);
		out.writeObject(src);

		ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
		ObjectInputStream in = new ObjectInputStream(byteIn);
		@SuppressWarnings("unchecked")
		List<T> dest = (List<T>) in.readObject();
		return dest;
	}

	public static void distinctList(List<Integer> cardList) {
		Set<Integer> set = new HashSet<>(cardList);
		cardList.clear();
		cardList.addAll(set);
	}
	
	/**
	 * TArray转list
	 * */
	public static final List<Integer> toList(ITArray list){
		List<Integer> tem = new ArrayList<>();
		for(int i=0;i<list.size();++i) {
			tem.add(list.getInt(i));
		}
		return tem;
	}
}
