package extend.mj;

import com.game.Global;

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


public class RoomCard {
	public List<Integer>	cardList;
	EXRoom						room;
	public ArrayList<Integer>    subCardList;
	public Integer	laiziCard;
	public Integer	laiziCard2;
	public Integer	laiziCardBefore;
	public Integer	laiziCard2Before;

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

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

	private void initCard() {
		for (int index = 1; index <= 8; index++) {
			for (int index2 = 0; index2 < 4; index2++) {
				this.cardList.add(100*index + 1);
				this.cardList.add(100*index + 2);
				this.cardList.add(100*index + 3);
			}
			this.cardList.add(100*index + 4);
		}
	}

	private void initCard2() {
		List list = Arrays.asList(101, 101, 101, 202, 204, 302, 302, 304, 402, 403, 502, 502, 504, 801, 701, 701, 701);
		List list2 = Arrays.asList(101, 101, 101, 802, 202, 204, 302, 302, 304, 402, 403, 502, 502, 504, 801, 701, 701, 701);
		List list3 = Arrays.asList(701,202, 202, 203, 301,302, 303, 303, 304, 501, 502, 501, 502, 601, 602, 802);

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

	private void shuffle() {
		Collections.shuffle(this.cardList);
	}

	public boolean isLaizi(int card)
	{
	    return false;
	}

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

//	public int pop() {
//
//		if (this.cardList.size() == 0) {
//			this.room.bankerSeat = this.room.activeSeat;
////			this.room.roundSettle();
//		}
//
//		int card = this.cardList.remove(0);
//
//		return card;
//
//	}

	public int pop() {
		Global.logger.info("pop cardlist size=>"+this.cardList.size()+":sub size==>"+subCardList.size());
		int card = 0;
		if (this.cardList.size() == 0) {
			if(this.subCardList.size() > 0) {
				card = this.subCardList.remove(0);
			}else {
				this.room.bankerSeat = this.room.activeSeat;
			}
		}else {
			card = this.cardList.remove(0);
		}
		return card;

	}

	public int popsub() {
		Global.logger.info("pop sub cardlist size=>"+this.cardList.size()+":sub size==>"+subCardList.size());
		int card = 0;
		if (this.subCardList.size() == 0) {
			if (this.cardList.size() == 0) {
				this.room.bankerSeat = this.room.activeSeat;
			}else {
				card = this.cardList.remove(0);
			}
		}else {
			card = this.subCardList.remove(0);
		}
		return card;
	}

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

	public List<Integer> deal() {
		List<Integer> dealCards = new ArrayList<Integer>();

		for (int index = 0; index < 18; index++) {
			dealCards.add(this.pop());
		}

		return dealCards;
	}
}
