package extend.mj;

import java.util.*;

import com.game.Global;
import com.game.Util;

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

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

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

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

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


	private void initCard() {
		boolean queyimen = this.room.config.getBoolean(Config.ROOM_CONFIG_QUEYIMEN);
		for (int index = 1; index <= 9; index++) {
			for (int index2 = 0; index2 < 4; index2++) {
				this.cardList.add(200 + index);
				this.cardList.add(300 + index);
				if(!queyimen) {
					this.cardList.add(100 + index);
				}
			}
		}
		
//		this.cardList.add(207);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
//		this.cardList.add(103);
	}

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

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

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

	private boolean IsGoodCard(List<Integer> dealCards, int drawCard) {
		if (Util.cardNum(drawCard, dealCards) >= 2)
		{
			return true;
		}

		if (drawCard < 400)
		{
			if (drawCard % 100 <= 7)
			{
				if (Util.cardNum(drawCard+1, dealCards) >= 1 && Util.cardNum(drawCard+2, dealCards) >= 1)
				{
					return true;
				}
			}

			if (drawCard % 100 >= 3)
			{
				if (Util.cardNum(drawCard-1, dealCards) >= 1 && Util.cardNum(drawCard-2, dealCards) >= 1)
				{
					return true;
				}
			}

			if (drawCard % 100 >= 2 && drawCard % 100 <= 8)
			{
				if (Util.cardNum(drawCard-1, dealCards) >= 1 && Util.cardNum(drawCard+1, dealCards) >= 1)
				{
					return true;
				}
			}
		}

		return false;
	}

	// 发牌
	public List<Integer> deal(EXPlayer owner, int num) {
		List<Integer> dealCards = new ArrayList<>();
		shuffle();

		for (int index = 0; index < num ; index++) {
			double rand = Math.random() % 100 * 100;
			if (room.while_list && owner.is_white && rand > room.white_value)
			{
				Global.logger.info("dealcards playerid:"+owner.playerid+ " xingyuhao:"+owner.is_white+" white_value:"+room.white_value);
				ArrayList<Integer> tempCardList = new ArrayList<Integer>();
				int count = 0;
				do {
					int drawCard = room.card.pop();

					if(dealCards.size() > 0 && !IsGoodCard(dealCards, drawCard)) {
						if (room.card.cardList.size() != 0 && count++ <= room.card.cardList.size()) {
							tempCardList.add(drawCard);
							//Global.logger.info("no white to hu:" + drawCard);
							continue;
						}
					}

					dealCards.add(drawCard);
					break;
				} while(true);
				room.card.cardList.addAll(tempCardList);
			}
			else {
				if (owner.black_white_status == 2) {
					double rand1 = Math.random() % 100 * 100;
					if (rand1 <= owner.black_white_rate) {
						Global.logger.info("dealcards playerid:" + owner.playerid + " white player:" + owner.black_white_rate);
						ArrayList<Integer> tempCardList = new ArrayList<Integer>();
						int count = 0;
						do {
							int drawCard = room.card.pop();

							boolean flag = false;
							if (dealCards.size() > 0 && IsGoodCard(dealCards, drawCard)) {
								flag = true;
							}

							if (!flag) {
								if (room.card.cardList.size() > 0 && count++ <= room.card.cardList.size()) {
									tempCardList.add(drawCard);
									//Global.logger.info("not good card:" + drawCard + " for white player");
									continue;
								}
							}

							dealCards.add(drawCard);
							break;
						}
						while (true);
						room.card.cardList.addAll(tempCardList);
					} else {
						dealCards.add(room.card.pop());
					}
				} else if (owner.black_white_status == 1) {
					double rand1 = Math.random() % 100 * 100;
					if (rand1 <= owner.black_white_rate) {
						Global.logger.info("dealcards playerid:" + owner.playerid + " black player:" + owner.black_white_rate);
						ArrayList<Integer> tempCardList = new ArrayList<Integer>();
						int count = 0;
						do {
							int drawCard = room.card.pop();

							boolean flag = false;
							if (dealCards.size() > 0 && IsGoodCard(dealCards, drawCard)) {
								flag = true;
							}

							if (flag) {
								if (room.card.cardList.size() > 0 && count++ <= room.card.cardList.size()) {
									tempCardList.add(drawCard);
									//Global.logger.info("not good card:" + drawCard + " for white player");
									continue;
								}
							}

							dealCards.add(drawCard);
							break;
						}
						while (true);
						room.card.cardList.addAll(tempCardList);
					} else {
						dealCards.add(room.card.pop());
					}
				} else {
					if (room.while_list && !owner.is_white && rand > room.white_value) {
						ArrayList<Integer> tempCardList = new ArrayList<Integer>();
						int count = 0;
						do {
							int drawCard = room.card.pop();

							boolean flag = false;
							if (dealCards.size() > 0 && IsGoodCard(dealCards, drawCard)) {
								flag = true;
							}

							if (flag) {
								if (room.card.cardList.size() > 0 && count++ <= room.card.cardList.size()) {
									tempCardList.add(drawCard);
									//Global.logger.info("not good card:" + drawCard + " for white player");
									continue;
								}
							}

							dealCards.add(drawCard);
							break;
						}
						while (true);
						room.card.cardList.addAll(tempCardList);
					} else {
						dealCards.add(room.card.pop());
					}
				}
			}
		}

		return dealCards;
	}

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

		for (int index = 0; index < num; index++) {
			dealCards.add(this.pop());
		}
		
//		if(num == 14) {
//			dealCards.add(104);
//		}
//		dealCards.add(101);
//		dealCards.add(101);
//		dealCards.add(101);
//		dealCards.add(101);
//		
//		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;

	}
}
