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 List<CardObj> cardList;
	public List<CardObj> cardListTemp;
	EXRoom room;
	int num;
	int maxPlayer;
	int selectDiqu;
	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,int selectDiqu) {
		Global.logger.info("RoomCard");
		this.cardList = new ArrayList<CardObj>();
		this.cardListTemp = new ArrayList<CardObj>();
		this.room = table;
		this.num = num;
		this.maxPlayer = maxPlayer;
		this.selectDiqu = selectDiqu;
		Global.logger.info("RoomCard:");
	}

	public void init() {
		Global.logger.info("dealcards init:");
		this.boomNumLimit = rand.nextInt(2) + 2;

		this.sangzhangNumLimit = rand.nextInt(5) + 10;

		this.totalBoomNum = 0;
		this.totalSanZhangNum = 0;
		this.cardList.clear();
		Global.logger.info("dealcards init:"+this.boomNumLimit);
		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 index = 3; index <= 15; index++) {
				// 方片

			if(selectDiqu==0){
				if (index<12){
					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));
				}else{
					this.cardList.add(new CardObj(400 + index));
				}

			}else{
				if (index>4&&index!=15){
					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+"selectDiqu:"+selectDiqu);
	}

	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> deal0(int seat){
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			
			dealCards.add(new CardObj(103));
			dealCards.add(new CardObj(205));
			dealCards.add(new CardObj(305));
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(208));
			
			dealCards.add(new CardObj(308));
			dealCards.add(new CardObj(408));
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(210));
			dealCards.add(new CardObj(311));
		}
		else if(seat == 2) {

			dealCards.add(new CardObj(303));
			dealCards.add(new CardObj(204));
			
			dealCards.add(new CardObj(305));
			dealCards.add(new CardObj(304));
			dealCards.add(new CardObj(107));
			
			dealCards.add(new CardObj(207));
			dealCards.add(new CardObj(307));
			dealCards.add(new CardObj(110));
			dealCards.add(new CardObj(111));
			dealCards.add(new CardObj(315));
		}
		
		return dealCards;
	}

	public List<CardObj> deal1(int seat){
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			dealCards.add(new CardObj(103));
			dealCards.add(new CardObj(203));
			dealCards.add(new CardObj(303));
			
			dealCards.add(new CardObj(104));
			dealCards.add(new CardObj(204));
			dealCards.add(new CardObj(304));
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(108));
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(110));
		}
		else if(seat == 2) {
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(207));
			dealCards.add(new CardObj(307));
			
			dealCards.add(new CardObj(208));
			dealCards.add(new CardObj(209));
		}
		
		return dealCards;
	}
	
	
	public List<CardObj> deal2(int seat){
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			dealCards.add(new CardObj(105));
			dealCards.add(new CardObj(205));
			dealCards.add(new CardObj(305));
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(108));
			
			dealCards.add(new CardObj(110));
		
		}
		else if(seat == 2) {
			
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(209));
			dealCards.add(new CardObj(309));
			dealCards.add(new CardObj(110));
			dealCards.add(new CardObj(210));
			dealCards.add(new CardObj(310));
			
			//dealCards.add(new CardObj(107));
		}
		
		return dealCards;
	}
	
	public List<CardObj> deal3(int seat){
		
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(107));
			
			dealCards.add(new CardObj(108));
			dealCards.add(new CardObj(108));
			dealCards.add(new CardObj(108));
			
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(110));
			dealCards.add(new CardObj(111));
			dealCards.add(new CardObj(112));
			dealCards.add(new CardObj(113));
			dealCards.add(new CardObj(114));
			dealCards.add(new CardObj(215));
		}
		else if(seat == 2) {
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
		}
		
		return dealCards;
	}
	
	public List<CardObj> deal4(int seat){
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(207));
			dealCards.add(new CardObj(307));
			
			dealCards.add(new CardObj(108));
			dealCards.add(new CardObj(208));
			dealCards.add(new CardObj(308));
			
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(209));
			dealCards.add(new CardObj(309));
			
			dealCards.add(new CardObj(110));
			dealCards.add(new CardObj(210));
			dealCards.add(new CardObj(310));
			dealCards.add(new CardObj(410));
		}
		else if(seat == 2) {
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
		}
		
		return dealCards;
	}
	
	public List<CardObj> deal6(int seat){
		List<CardObj> dealCards = new ArrayList<CardObj>();
		
		if(seat == 1) {
			dealCards.add(new CardObj(103));
			dealCards.add(new CardObj(203));
			
			dealCards.add(new CardObj(104));
			dealCards.add(new CardObj(204));
			
			dealCards.add(new CardObj(105));
			dealCards.add(new CardObj(105));
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(106));
			
			dealCards.add(new CardObj(107));
			dealCards.add(new CardObj(107));
			
			dealCards.add(new CardObj(108));
			dealCards.add(new CardObj(108));
			
			dealCards.add(new CardObj(109));
			dealCards.add(new CardObj(109));
			
			dealCards.add(new CardObj(110));
			dealCards.add(new CardObj(110));
		}
		else if(seat == 2) {
			
			dealCards.add(new CardObj(106));
			dealCards.add(new CardObj(206));
			dealCards.add(new CardObj(306));
			
			dealCards.add(new CardObj(107));
		}
		
		return dealCards;
	}
	
	public List<CardObj> dealTest(int seat){
		
		List<CardObj> dealCards = deal2(seat);
		if(dealCards == null || dealCards.size() == 0) {
			
			dealCards = deal();
		}
		
		return dealCards;
	}
	
	// 发牌
	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 maxDanPai, int maxDuizi, int maxSanZhang) {
		List<CardObj> dealCards = new ArrayList<CardObj>();
		Global.logger.info("room_white:"+room_white+"is_white:"+is_white+"white_black_status:"+white_black_status);
		shuffle();

		double rand1 = Math.random() % 100 * 100;
		for (int index = 0; index < this.num ; index++) {
			if (room_white && is_white)
			{

				Global.logger.info("rand1:"+rand1+"room_rate"+room_rate);
			    if (rand1 > room_rate)
				{
					if (index == 0)
					{
					    if (maxDanPai == 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, maxDanPai, maxDuizi, maxSanZhang)) {
							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, 0, 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 == 2) {
				    if (rand1 < black_white_rate)
					{
						if (index == 0)
						{
							if (maxDanPai == 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, maxDanPai, maxDuizi, maxSanZhang)) {
								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, 0, 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 (maxDanPai == 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, maxDanPai, maxDuizi, maxSanZhang)) {
								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, 0, 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 (maxDanPai == 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, maxDanPai, maxDuizi, maxSanZhang)) {
								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 (maxDanPai == 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, maxDanPai, maxDuizi, maxSanZhang)) {
									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, 0, 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, int maxDanPai, int maxDuizi, int maxSanZhang)
	{

		Global.logger.info("card:"+card+"maxDanPai:"+maxDanPai+"maxDuizi:"+maxDuizi+"maxSanZhang:"+maxSanZhang);
		if (isGooodCard2(card, dealCards, black, maxDanPai, maxDuizi, maxSanZhang)) {
			return true;
		}

		if (maxDanPai != 0)
		{
			if (card >= maxDanPai)
			{
				return true;
			}
		}

		if (maxDanPai != 0)
		{
			if (CardUtil.checkGoodCard(card, dealCards, 1)) {
				if (card >= maxDuizi)
				{
					return true;
				}
			}
		}

		if (maxDanPai != 0)
		{
			if (CardUtil.checkGoodCard(card, dealCards, 2)) {
				if (card >= maxSanZhang)
				{
					return true;
				}
			}
		}

		return false;
	}

	boolean isGooodCard2(int card, List<CardObj> dealCards, boolean black, int maxDanPai, int maxDuizi, int maxSanZhang)
	{
		double rand = Math.random() % 100 * 100;
		if (black)
		{
			if (CardUtil.checkGoodCard(card, dealCards, 3) && rand < 70) {
				Global.logger.info("remove zhadan");
				return true;
			}

			if (CardUtil.checkFenJi(card, dealCards) && rand < 70)
			{
				Global.logger.info("remove feiji");
				return true;
			}

			if (CardUtil.checkSevenShunzi(card, dealCards) && rand < 70)
			{
				Global.logger.info("remove sevent shun zi");
				return true;
			}

			if (CardUtil.checkFourDui(card, dealCards) && rand < 70)
			{
				Global.logger.info("remove four dui");
				return true;
			}

			if (CardUtil.checkQPai(card, dealCards) && rand < 70)
			{
				Global.logger.info("remove checkQPai");
				return true;
			}
		}
		else {
			if (CardUtil.checkGoodCard(card, dealCards, 3) && rand < 50) {
				Global.logger.info("remove zhadan");
				return true;
			}

			if (CardUtil.checkFenJi(card, dealCards) && rand < 50)
			{
				Global.logger.info("remove feiji");
				return true;
			}

			if (CardUtil.checkSevenShunzi(card, dealCards) && rand < 50)
			{
				Global.logger.info("remove sevent shun zi");
				return true;
			}

			if (CardUtil.checkFourDui(card, dealCards) && rand < 50)
			{
				Global.logger.info("remove four dui");
				return true;
			}

			if (CardUtil.checkQPai(card, dealCards) && rand < 50)
			{
				Global.logger.info("remove checkQPai");
				return true;
			}
		}


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

}
