package extend.mj.player.state;

import com.data.cache.GroupMemberCache;
import com.game.Global;
import com.game.Util;
import com.game.state.StateBase;

import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.StringUtil;
import extend.mj.*;
import extend.mj.tip.Tip;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;

/**
 * ���ץ��״̬
 * 
 *
 * 2017��8��30��
 * EXPlayerDrawState.java
 */
public class EXPlayerDrawState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		if (room.card.cardList.size() == 0) {
			room.liuju = true;
			room.endGame();
			return;

		}

		ITObject config = room.config;
		boolean qidui = config.getBoolean(Config.ROOM_CONFIG_QIDUI);
		boolean hz_hu = config.getBoolean(Config.ROOM_CONFIG_HZ_HU);
		boolean laizi = room.config.getBoolean(Config.ROOM_CONFIG_HONGZHONG);

		if(owner.seat == room.adminSeat) {
			double rand = Math.random() % 100;
			if (room.while_list && !owner.is_white && rand > room.white_value)
			{
				ArrayList<Integer> tempCardList = new ArrayList<Integer>();
				int count = 0;
				do {
					int drawCard = owner.drawCard = room.card.popsub();

					WinCard win = new WinCard(owner.cardInhand, drawCard, Config.HONGZHONG, laizi,qidui);
					if(!(hz_hu && win.zhong_count >0)) {
						if (win.checkQidui() || win.tryWin(room)) {
							if (room.card.cardList.size() != 0 && count++ <= 5) {
								tempCardList.add(drawCard);
								continue;
							}
						}
					}

					owner.drawCard = drawCard;
					break;
				} while(true);

				room.card.cardList.addAll(tempCardList);
			}
			else
			{
				owner.drawCard = room.card.popsub();
			}
		}else {
			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(!IsGoodCard(owner, drawCard, qidui, hz_hu, laizi)) {
						if (room.card.cardList.size() != 0 && count++ <= 5) {
							tempCardList.add(drawCard);
							Global.logger.info("no white to hu:" + drawCard);
							continue;
						}
					}

					owner.drawCard = 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_value:"+owner.black_white_rate);
						ArrayList<Integer> tempCardList = new ArrayList<Integer>();
						int count = 0;
						do {
							int drawCard = room.card.pop();

							boolean flag = false;
							if(IsGoodCard(owner, drawCard, qidui, hz_hu, laizi)) {
								flag = true;
							}

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

							owner.drawCard = drawCard;
							break;
						}
						while (true);
						room.card.cardList.addAll(tempCardList);
					} else {
						owner.drawCard = 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_value:"+owner.black_white_rate);
						ArrayList<Integer> tempCardList = new ArrayList<Integer>();
						int count = 0;
						do {
							int drawCard = room.card.pop();

							boolean flag = false;
							if(IsGoodCard(owner, drawCard, qidui, hz_hu, laizi)) {
								flag = true;
							}

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

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

		int leftCount = room.card.getCount();
		((EXGameController)Global.gameCtr).getCard(owner, leftCount);

		EXMainServer.playerRuleMgr.condition(PlayerRuleManager.DRAW_RULE, owner);
	}

	private boolean IsGoodCard(EXPlayer player, int drawCard, boolean qidui, boolean hz_hu, boolean laizi) {
	    if (drawCard == Config.HONGZHONG)
		{
			return true;
		}

		if (Util.cardNum(drawCard, player.cardInhand) >= 2)
		{
				return true;
		}

		for (int[] cardGroup : player.pongGroup) {
			if (drawCard == cardGroup[0]) {
				return true;
			}
		}

		WinCard win = new WinCard(player.cardInhand, drawCard, Config.HONGZHONG, laizi,qidui);
		if(!(hz_hu && win.zhong_count >0)) {
			if (win.checkQidui() || win.tryWin(player.getRoom())) {
				return true;
			}
		}

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

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

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

		return false;
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));

	}


}
