package extend.zp;

import java.util.*;

public class RoomCard {
	public ArrayList<Integer>	cardList;
	EXRoom						room;
	public ArrayList<Integer>    subCardList;

	public RoomCard(EXRoom table) {
		this.cardList = new ArrayList<Integer>();
		this.room = table;
		this.subCardList = new ArrayList<>();
	}

	public void init() {
		this.cardList.clear();
		this.subCardList.clear();
		this.initCard();
		this.shuffle();
	}

	private void initCard() {
		for (int index = 1; index <= 10; index++) {
			for (int index2 = 0; index2 < 4; index2++) {
				this.cardList.add(100 + index);
				this.cardList.add(200 + index);
			}
		}
	}

//	private void initCard2() {
//		List list = Arrays.asList(102, 103, 105, 106, 106, 107, 108, 108, 109, 110, 110, 110, 110, 201, 202, 207, 209, 209, 210, 210);
//		List list2 = Arrays.asList(101, 101, 104, 104, 105, 107, 107, 109, 201, 201, 203, 203, 203, 203, 204, 206, 207, 208, 209, 210);
//		List list3 = Arrays.asList(205, 106, 108, 208, 209, 105, 106, 206, 102, 105, 104, 108, 102, 107, 202, 106, 206, 202, 104, 204, 102, 108, 208, 207, 208, 205, 205, 201, 202, 101, 103, 206, 103, 209, 208, 210, 102, 105, 109, 108);
//
//		this.cardList.addAll(list2);
//		this.cardList.addAll(list);
//		this.cardList.addAll(list3);
//	}

	private void shuffle() {
		for(int i = 0;i < 100;i++) {
			Collections.shuffle(this.cardList);
		}
	}

	public int pop() {
		if (this.cardList.size() == 0) {
			if(this.subCardList.size() > 0) {
				int card = this.subCardList.remove(0);
				return card;
			}else {
				return 0;
			}
		}else {
			int card = this.cardList.remove(0);
			return card;
		}
	}

	public int popsub() {
		if (this.subCardList.size() == 0) {
			int card = this.cardList.remove(0);
			return card;
		}else {
			int card = this.subCardList.remove(0);
			return card;
		}
	}

	public int getCount() {
		return this.cardList.size() + this.subCardList.size();
	}

	public ArrayList<Integer> deal() {
		ArrayList<Integer> dealCards = new ArrayList<Integer>();
		for (int index = 0; index < 20; index++) {
			dealCards.add(this.pop());
		}

		return dealCards;
	}

	public void reInitCards(List<Integer> cards) {
//		Global.logger.info("sub before===>"+subCardList.size());
//		this.subCardList.addAll(cards);
		//从底牌移除
//		Global.logger.info("before===>"+cardList.size());
		for(Integer card : cards) {
			for(int i = cardList.size() - 1;i >= 0;i--) {
//				Global.logger.info("bidui===>"+cardList.get(i)+":"+Integer.valueOf(card));
				if(cardList.get(i).intValue() == card.intValue()) {
					if(cardList.remove(i) > 0) {
						this.subCardList.add(card);
					}
					break;
				}
			}
		}
//		Global.logger.info("sub after===>"+subCardList.size());
//		Global.logger.info("after===>"+cardList.size());
	}
}
