package extend.zp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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 List<Integer> getAllResult(){
		List<Integer> all = new ArrayList<>();
		if(subCardList.size() > 0)
			all.addAll(subCardList);
		all.addAll(cardList);
		return all;
	}

	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 shuffle() {
		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());
	}
}
