package extend.mj.player.state;

import com.game.Global;
import com.game.Util;
import com.game.state.StateBase;

import extend.mj.*;
import extend.mj.uitl.Paixing;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 
 */
public class EXPlayerDrawState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		if (!owner.skipDraw&&room.card.cardList.size() == 0) {
			room.liuju = true;
			room.endGame();
			return;
		}
		draw(owner);
	}
	
	void draw(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		boolean zt_win = false;
		if(!owner.skipDraw) {
			if(owner.seat == room.adminSeat) {
				Global.logger.info("dealcards playerid:"+owner.playerid+ " adminseat");
				double rand = Math.random() % 100 * 100;
				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.popsub();

						boolean no_jiang = room.config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
						boolean jiang = true;
						if(no_jiang&&room.activeHaidi) {
							jiang = false;
						}

						HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
						boolean result = Paixing.checkWin(map, owner.opCardList, owner.cardInhand, drawCard, jiang, room.difen_score);
						if (result) {
							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
				{
					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)) {
							if (room.card.cardList.size() != 0 && count++ <= room.card.cardList.size()) {
								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) {
						Global.logger.info("dealcards playerid:"+owner.playerid+ " white player:" + owner.black_white_rate);
						double rand1 = Math.random() % 100 * 100;
						if (rand1 <= 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)) {
									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;
									}
								}

								owner.drawCard = drawCard;
								break;
							}
							while (true);
							room.card.cardList.addAll(tempCardList);
						} else {
							owner.drawCard = room.card.pop();
						}
					}
					else if(owner.black_white_status == 1) {
						Global.logger.info("dealcards playerid:"+owner.playerid+ " black player:" + owner.black_white_rate);
						double rand1 = Math.random() % 100 * 100;
						if (rand1 <= 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)) {
									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;
									}
								}

								owner.drawCard = drawCard;
								break;
							}
							while (true);
							room.card.cardList.addAll(tempCardList);
						} else {
							owner.drawCard = 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(IsGoodCard(owner, 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;
									}
								}

								owner.drawCard = drawCard;
								break;
							}
							while (true);
							room.card.cardList.addAll(tempCardList);
						}
						else {
							owner.drawCard = room.card.pop();
						}
					}
				}
			}
			//owner.drawCard = room.card.pop();
			zt_win = EXMainServer.playerRuleMgr.condition(PlayerRuleManager.ZT_HU_RULE, owner,false);
			int leftCount = room.card.getCount();
			((EXGameController)Global.gameCtr).getCard(owner, leftCount);
			
		}
		if(room.activeHaidi) {
			room.haidiCard = owner.drawCard;
		}
		owner.skipDraw = false;
		if(!zt_win) {
			EXMainServer.playerRuleMgr.condition(PlayerRuleManager.DRAW_RULE, owner);
		}
	}

	private boolean IsGoodCard(EXPlayer player, int drawCard) {
		boolean no_jiang = player.getRoom().config.getBoolean(Config.ROOM_CONFIG_NO_JIANG);
		boolean jiang = true;
		if(no_jiang&&player.getRoom().activeHaidi) {
			jiang = false;
		}

		HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
		boolean result = Paixing.checkWin(map, player.opCardList, player.cardInhand, drawCard, jiang, player.getRoom().difen_score);
		if (result) {
			return true;
		}

		if (Util.cardNum(drawCard, player.cardInhand) >= 2)
		{
			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));
	}
}
