package extend.mj;

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

import com.game.Global;

import extend.mj.uitl.WinCard;


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

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

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


	private void initCard() {

		for (int index = 1; index <= 9; index++) {
			for (int index2 = 0; index2 < 4; index2++) {
				this.cardList.add(100 + index);
				this.cardList.add(200 + index);
				this.cardList.add(300 + index);
			}
		}
		
		boolean laizi = room.config.getBoolean(Config.ROOM_CONFIG_HONGZHONG);
		boolean laizi8 = room.config.getBoolean(Config.ROOM_CONFIG_HONGZHONG8);
		if(laizi) {
			int laiziNum = laizi8 ? 8 :4;
			WinCard.zhongWinNum = laiziNum;
			for(int i =0;i<laiziNum;++i) {
				this.cardList.add(Config.HONGZHONG);
			}
		}
		
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);
//		this.cardList.add(102);

	}

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

	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;
//		this.room.roundSettle();
	}

	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 < 13; index++) {
			dealCards.add(this.pop());
		}
		
//		dealCards.add(101);
//		dealCards.add(101);
//		dealCards.add(101);
//		dealCards.add(101);	
//		dealCards.add(Config.HONGZHONG);
//		dealCards.add(Config.HONGZHONG);
//		dealCards.add(Config.HONGZHONG);
//		dealCards.add(Config.HONGZHONG);	
//		dealCards.add(102);
//		dealCards.add(102);
//		dealCards.add(102);
//		dealCards.add(102);
//		dealCards.add(103);
//		dealCards.add(103);
//		dealCards.add(103);
//		dealCards.add(103);	
//		dealCards.add(104);
		return dealCards;

	}
}
