package extend.zp;

import com.taurus.core.util.Logger;

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

public class RoomCard {

	Logger logger = Logger.getLogger(getClass());

	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.initCard2();
		//this.initCard_tianhu();
		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(110,108,109,202,103,110,103,203,203,203,208,205,206, 202,204,210, 102,107,107);
//		List list2 = Arrays.asList(110,108,109,202,103,110,103,203,203,203,208,205,206, 202,204,210, 102,107,107);
//		List list3 = Arrays.asList(207, 105, 207, 207, 207, 207, 207, 207, 207, 207, 206, 203, 210, 206, 201, 205, 210, 202, 105, 101, 210, 106, 204, 107, 201, 209, 106, 205, 109, 209, 103, 104, 101, 105, 103, 202, 108, 109, 208);
//		List list  = Arrays.asList(103, 103, 103, 104, 104, 105, 106, 107, 109, 201, 202, 202, 203, 204, 205, 206, 209, 210, 210);
//		List list2 = Arrays.asList(101, 102, 102, 103, 107, 110, 110, 110, 201, 201, 202, 204, 205, 205, 206, 206, 207, 208, 210);
//		List list3 = Arrays.asList(107, 101, 204, 108, 207, 106, 203, 110, 201, 106, 207, 208, 204, 108, 108, 203, 105, 101, 102, 105, 208, 109, 102, 205, 209, 206, 209, 210, 106, 109, 105, 104, 109, 209, 107, 104, 207, 203, 101, 202, 108, 208);
		List list  = Arrays.asList(101,101, 102, 103, 201, 202, 104, 203, 105, 106, 107, 108, 109, 207, 208, 204, 210, 109, 110);
		List list2 = Arrays.asList(101,101, 102, 102, 103, 103, 104, 104, 105, 105, 106, 106, 107, 107, 108, 108, 109, 109, 110);
		List list3 = Arrays.asList( 208,210,101, 103,101, 109, 101, 106, 101, 105, 109, 205, 205, 208, 209, 207, 109, 203, 208, 102, 210, 106, 104, 107, 202, 105, 206, 206, 109, 202, 209, 210, 104, 206, 107, 101, 108, 210, 203, 101, 209, 106, 209);

		this.cardList.addAll(list);
		this.cardList.addAll(list);
		this.cardList.addAll(list3);
	}

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

		this.cardList.addAll(list);
		this.cardList.addAll(list);
		this.cardList.addAll(list3);
	}

	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 < 19; index++) {
			//logger.info("jefe card id:" + this.pop());
			dealCards.add(this.pop());
		}
		return dealCards;
	}

	public ArrayList<Integer> deal2() {
		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) {
		//从底牌移除
		for(Integer card : cards) {
			for(int i = cardList.size() - 1;i >= 0;i--) {
				if(cardList.get(i).intValue() == card.intValue()) {
					if(cardList.remove(i) > 0) {
						this.subCardList.add(card);
					}
					break;
				}
			}
		}
	}
}
