package extend.pk;

import com.game.Global;
import extend.pk.uitl.CardUtil;

import java.util.*;

public class RoomCard {
	
	public static final int HEART_TEN = 310;
	public static final int CARD_A = 14;
	public static final int CARD_2 = 15;
	public static final int CARD_3 = 3;
	public static final int MAX_510K_SCORE = 1000;
	public static final int CUR_5_CARD = 8;
	public List<CardObj> cardList;
	public List<CardObj> cardListTemp;
	public List<CardObj> jiangliCardList;
	EXRoom room;
	int num;
	int maxPlayer;
	Random rand = new Random();
	
	int boomNumLimit = rand.nextInt(2) + 2;
	int sangzhangNumLimit = rand.nextInt(5) + 10;
	
	int totalBoomNum = 0;
	int totalSanZhangNum = 0;
	
	public RoomCard(EXRoom table,int num,int maxPlayer) {
		this.cardList = new ArrayList<CardObj>();
		this.cardListTemp = new ArrayList<CardObj>();
		this.jiangliCardList = new ArrayList<CardObj>();
		this.room = table;
		this.num = num;
		this.maxPlayer = maxPlayer;
	}

	public void init() {
		this.boomNumLimit = rand.nextInt(2) + 5;
		this.sangzhangNumLimit = rand.nextInt(5) + 10;

		this.totalBoomNum = 0;
		this.totalSanZhangNum = 0;
		this.cardList.clear();
		this.jiangliCardList.clear();
		this.initCard();
		int currentBoomNum = 0;
		int currentSanZhangNum = 0;
		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		do {
			currentBoomNum = 0;
			currentSanZhangNum = 0;
			this.shuffle();
			for(int i = 0; i <this.maxPlayer; i++) {
				this.cardListTemp.clear();
				for(int j = 0; j < num; j++) {
					this.cardListTemp.add(this.cardList.get(i * num + j));
					CardUtil.getCardNumMap(result, this.cardListTemp);
				}
			}
			for(Map.Entry<Integer, Integer> entry : result.entrySet()) {
				if(entry.getValue() >= 4) {
					currentBoomNum++;
				}
				else if(entry.getValue() == 3) {
					currentSanZhangNum++;
				}
			}
		}while((currentBoomNum + totalBoomNum > boomNumLimit) || (currentSanZhangNum + totalSanZhangNum > sangzhangNumLimit));
	}

	private void initCard() {
		for(int i = 0; i < 3; i++)
		{
			for (int index = 8; index <= 15; index++) {
				// 方片
				this.cardList.add(new CardObj(100 + index));
				// 梅花
				this.cardList.add(new CardObj(200 + index));
				// 红桃
				this.cardList.add(new CardObj(300 + index));
				// 黑桃
				this.cardList.add(new CardObj(400 + index));
			}
		}

		rand.setSeed(System.currentTimeMillis());
		int len = this.cardList.size();
		for(int i = 0; i < len; i++) {
			
			int randpos = rand.nextInt(1000000000) % len;
			CardObj cotemp = this.cardList.get(i);
			
			this.cardList.set(i, this.cardList.get(randpos));
			this.cardList.set(randpos,cotemp);
		}

		Global.logger.info("RoomCard  initCard(): "+this.cardList);
	}

	private void shuffle() {
		for(int i = 0; i < 100; i++)
		{
			Collections.shuffle(this.cardList);
		}
		Random rand = new Random();
		int len = this.cardList.size();

		for(int i = 0; i < 10000; i++) {
			rand.setSeed(System.currentTimeMillis());
			int start = rand.nextInt(len);
			int end = rand.nextInt(len);

			CardObj co = this.cardList.get(start);
			this.cardList.set(start, this.cardList.get(end));
			this.cardList.set(end, co);
		}
	}

	public CardObj pop() {
		CardObj card = this.cardList.remove(0);
		return card;
	}

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

	// 发牌
	public List<CardObj> deal(boolean room_white, boolean is_white, double room_rate, int white_black_status, double black_white_rate, double black_black_rate, int maxZhaDan, int cardNum) {
		List<CardObj> dealCards = new ArrayList<CardObj>();
		//shuffle();

		double rand1 = Math.random() % 100 * 100;
		for (int index = 0; index < this.num ; index++) {
			if (room_white && is_white)
			{
			    if (rand1 > room_rate)
				{
					if (index == 0)
					{
					    if (maxZhaDan == 0)
						{
							Global.logger.info("dealcards rand card");
						}
					    else
						{
							Global.logger.info("dealcards xingyun card");
						}
					}
					ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
					int count = 0;
					do {
						CardObj tempCard = this.pop();

						if (isGooodCard(tempCard.cardMod, dealCards, false, true, maxZhaDan, cardNum)) {
							if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
								tempCardList.add(tempCard);
								continue;
							}
						}
						else {
							double rand111 = Math.random() % 100 * 100;
							if (tempCard.cardMod <= 11 && rand111 < 10)
							{
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}
						}

						dealCards.add(tempCard);
						break;
					} while (true);
					this.cardList.addAll(tempCardList);
				}
				else {

					if (index == 0)
						Global.logger.info("dealcards rand card");
					ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
					int count = 0;
					do {
						CardObj tempCard = this.pop();

						if (isGooodCard2(tempCard.cardMod, dealCards, false, true, 0, 0)) {
							if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
								tempCardList.add(tempCard);
								continue;
							}
						}
						else {
							double rand111 = Math.random() % 100 * 100;
							if (tempCard.cardMod <= 11 && rand111 <= 10)
							{
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}
						}

						dealCards.add(tempCard);
						break;
					} while (true);
					this.cardList.addAll(tempCardList);
				}
			}
			else {
				if (white_black_status == 2) {
				    if (rand1 < black_white_rate)
					{
						if (index == 0)
						{
							if (maxZhaDan == 0)
							{
								Global.logger.info("dealcards rand card");
							}
							else
							{
								Global.logger.info("dealcards white card");
							}
						}

						ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
						int count = 0;
						do {
							CardObj tempCard = this.pop();

							if (isGooodCard(tempCard.cardMod, dealCards, false, true, maxZhaDan, cardNum)) {
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}

							dealCards.add(tempCard);
							break;
						} while (true);
						this.cardList.addAll(tempCardList);
					}
					else {
						if (index == 0)
							Global.logger.info("dealcards rand card");
						ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
						int count = 0;
						do {
							CardObj tempCard = this.pop();

							if (isGooodCard2(tempCard.cardMod, dealCards, false, true, 0, 0)) {
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}

							dealCards.add(tempCard);
							break;
						} while (true);
						this.cardList.addAll(tempCardList);
					}
				}
				else if (white_black_status == 1) {
				    if (rand1 < black_white_rate)
					{
						if (index == 0)
						{
							if (maxZhaDan == 0)
							{
								Global.logger.info("dealcards rand card");
							}
							else
							{
								Global.logger.info("dealcards black card");
							}
						}
;
						ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
						int count = 0;
						do {
							CardObj tempCard = this.pop();

							if (isGooodCard(tempCard.cardMod, dealCards, true, false, maxZhaDan, cardNum)) {
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}

							dealCards.add(tempCard);
							break;
						} while (true);
						this.cardList.addAll(tempCardList);
					}
					else {
						if (index == 0)
							Global.logger.info("dealcards rand card");
						ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
						int count = 0;
						do {
							CardObj tempCard = this.pop();

							if (isGooodCard2(tempCard.cardMod, dealCards, true, false, 0, 0)) {
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}

							dealCards.add(tempCard);
							break;
						} while (true);
						this.cardList.addAll(tempCardList);
					}
				}
				else {
					if (room_white && rand1 > room_rate)
					{
						if (index == 0)
						{
							if (maxZhaDan == 0)
							{
								Global.logger.info("dealcards rand card");
							}
							else
							{
								Global.logger.info("dealcards general cha card");
							}
						}

						ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
						int count = 0;
						do {
							CardObj tempCard = this.pop();

							if (isGooodCard(tempCard.cardMod, dealCards, false, false, maxZhaDan, cardNum)) {
								if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
									tempCardList.add(tempCard);
									continue;
								}
							}

							dealCards.add(tempCard);
							break;
						} while (true);
						this.cardList.addAll(tempCardList);
					}
					else {
						if (rand1 < black_white_rate)
						{
							if (index == 0)
							{
								if (maxZhaDan == 0)
								{
									Global.logger.info("dealcards rand card");
								}
								else
								{
									Global.logger.info("dealcards general cha card");
								}
							}
							ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
							int count = 0;
							do {
								CardObj tempCard = this.pop();

								if (isGooodCard(tempCard.cardMod, dealCards, false, false, maxZhaDan, cardNum)) {
									if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
										tempCardList.add(tempCard);
										continue;
									}
								}

								dealCards.add(tempCard);
								break;
							} while (true);
							this.cardList.addAll(tempCardList);
						}
						else {
							if (index == 0)
								Global.logger.info("dealcards rand card");
							ArrayList<CardObj> tempCardList = new ArrayList<CardObj>();
							int count = 0;
							do {
								CardObj tempCard = this.pop();

								if (isGooodCard2(tempCard.cardMod, dealCards, false, true, 0, 0)) {
									if (this.cardList.size() > 0 && count++ <= this.cardList.size()) {
										tempCardList.add(tempCard);
										continue;
									}
								}

								dealCards.add(tempCard);
								break;
							} while (true);
							this.cardList.addAll(tempCardList);
						}
					}
				}
			}
		}
		//shuffle();

		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		CardUtil.getCardNumMap(result, dealCards);
		for(Map.Entry<Integer, Integer> entry : result.entrySet()) {
			if(entry.getValue() >= 4) {
				this.totalBoomNum++;
			}
			else if(entry.getValue() == 3) {
				this.totalSanZhangNum++;
			}
		}

		return dealCards;
	}

	boolean isGooodCard(int card, List<CardObj> dealCards, boolean black, boolean white, int maxZhaDan, int cardNum)
	{
		if (isGooodCard2(card, dealCards, black, white, maxZhaDan, cardNum)) {
			return true;
		}

		if (maxZhaDan > 0)
		{
		    if (cardNum > 0)
			{
				if (CardUtil.checkCard(card, dealCards, cardNum-1)) {
					if (card >= maxZhaDan)
					{
						return true;
					}
				}
				else {
					if (CardUtil.checkGoodCard(card, dealCards, cardNum-1)) {
						return true;
					}
				}
			}
			else {
				if (CardUtil.checkGoodCard(card, dealCards, 6)) {
					if (card >= maxZhaDan)
					{
						return true;
					}
				}
			}
		}

		return false;
	}

	boolean isGooodCard2(int card, List<CardObj> dealCards, boolean black, boolean white, int maxZhaDan, int cardNum)
	{
		double rand = Math.random() % 100 * 100;
		if (black)
		{
			if (CardUtil.checkQPai(card, dealCards) && rand < 70)
			{
				Global.logger.info("remove checkQPai");
				return true;
			}
		}
		else {
			if (!white)
			{
				if (CardUtil.checkQPai(card, dealCards) && rand < 50)
				{
					Global.logger.info("remove checkQPai");
					return true;
				}

				if (CardUtil.checkGoodCard(card, dealCards, 6)) {
					if (rand < 80)
					{
						Global.logger.info("remove seven zha dan");
						return true;
					}
					else {
						Global.logger.info("remove seven zha dan fail");
					}
				}
			}
		}

		if (CardUtil.checkGoodCard(card, dealCards, 6)) {
			if (rand < 60)
			{
				Global.logger.info("remove seven zha dan");
				return true;
			}
			else {
				Global.logger.info("remove seven zha dan fail");
			}
		}

		return false;
	}

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

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

		Map<Integer, Integer> result = new HashMap<Integer, Integer>();
		CardUtil.getCardNumMap(result, dealCards);
		for(Map.Entry<Integer, Integer> entry : result.entrySet()) {
			if(entry.getValue() >= 4) {
				this.totalBoomNum++;
			}
			else if(entry.getValue() == 3) {
				this.totalSanZhangNum++;
			}
		}

		return dealCards;
	}

}
