package extend.mj.room.state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.data.Player;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;

import com.taurus.permanent.TPServer;
import extend.mj.Config;
import extend.mj.EXActionEvent;
import extend.mj.EXMainServer;
import extend.mj.EXPlayBack;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.PlayerRuleManager;
import extend.mj.player.state.EXPlayerPiaoNiaoTipState;
import extend.mj.player.state.EXPlayerTianTingTipState;
import extend.mj.uitl.Paixing;

/**
 * 
 */
public class EXRoomDealState extends StateBase<EXRoom> {

	@Override
	public void enter(EXRoom owner) {
		owner.card.init();
		if (owner.bankerSeat == 0) {
			owner.bankerSeat = 1;
			ArrayList<Integer> randPlayer = new ArrayList<>();
			for (Entry<Integer, Player> entry : owner.playerMapBySeat.entrySet()) {
				Integer seat = entry.getKey();
				randPlayer.add(seat);
			}

			Collections.shuffle(randPlayer);
			if (randPlayer.size() > 0)
			{
				owner.bankerSeat = randPlayer.get(0);
			}
		}
//		owner.round += 1;
//		owner.isplaying = true;

		int piao_niao = owner.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		boolean piao_niao_auto = owner.config.getBoolean(Config.ROOM_CONFIG_PIAO_NIAO_AUTO);
		if(piao_niao > 0) {
			if(piao_niao == 1 || piao_niao == 2 && piao_niao_auto) {
				owner.piaoNiaoCount = owner.maxPlayers;
				for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
					Player player = entry.getValue();
					player.stateMachine.changeState(Global.getState(EXPlayerPiaoNiaoTipState.class));
				}
			}
			else if (piao_niao == 2)
			{
				owner.piaoNiaoCount = 0;
				int opt = owner.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO_OPT);
				int piao_fen = 1;
				if (opt == 0)
				{
					piao_fen = 1;
				}
				else if (opt == 1)
				{
					piao_fen = 2;
				}
				else if (opt == 2)
				{
					piao_fen = 3;
				}

				for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					player.piaoNiao = piao_fen;
					player.tipMgr.clean();
					player.stateMachine.changeState(Global.getState(PlayerWaitState.class));
					player.getRoom().piaoNiaoCount --;
					EXMainServer.gameCtr.piaoNiaoEvent(player);
				}
				checkXiPai(owner);
			} else {
				checkXiPai(owner);
			}
		}else {
			checkXiPai(owner);
		}
	}

	public static void timer(EXRoom owner, EXRoomDealState state) {

		TPServer.me().getTimerPool().schedule(new Runnable() {

			@Override
			public void run() {

				if (owner == null) {
					Global.logger.error("room is null");
					return;
				}

				if (owner.isDestroy)
					return;

				owner.enqueueRunnable(new Runnable() {

					@Override
					public void run() {
						state.checkQSWin(owner);
					}
				});
			}
		}, 4000, TimeUnit.MILLISECONDS);
	}

	private void checkXiPai(EXRoom owner) {
		boolean donghua = false;
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.xi_pai)
			{
				donghua = true;
				break;
			}
		}

		if (donghua)
		{
			owner.notifyXiPai();
			//启动定时器
			timer(owner, this);
		}
		else {
			checkQSWin(owner);
		}
	}
	
	private void checkQSWin(EXRoom owner) {
		EXMainServer.gameCtr.dealCard(owner);
		owner.playBackData = new EXPlayBack(owner);
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			Player player = entry.getValue();
			EXMainServer.playerRuleMgr.condition(PlayerRuleManager.QS_HU_RULE, (EXPlayer)player);
		}
		
		if(owner.qsWinCount ==0) {
			checkTianting(owner);
		}
	}
	
	private void checkTianting(EXRoom owner) {
		boolean ting = owner.config.getBoolean(Config.ROOM_CONFIG_NATIVE_HU);
		if(ting) {
			for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
				EXPlayer player = (EXPlayer) entry.getValue();
				if(player.seat != owner.bankerSeat) {
					if(Paixing.tingCheck(player.opCardList, player.cardInhand)) {
						owner.tianTingCount ++;
						player.stateMachine.changeState(Global.getState(EXPlayerTianTingTipState.class));
					}
				}
			}
			if (owner.tianTingCount == 0) {
				toNextState(owner);
			}
		}else {
			toNextState(owner);
		}
	}

	public void execute(EXRoom owner, String cmd, int gid, Object param) {
		if(cmd.equals(EXActionEvent.EVENT_PIAO_NIAO)) {
			if(owner.piaoNiaoCount==0) {
				checkXiPai(owner);
			}
		}else if(cmd.equals(EXActionEvent.EVENT_QSWIN)) {
			if(owner.qsWinCount ==0) {
				checkTianting(owner);
			}
		}else if(cmd.equals(EXActionEvent.EVENT_TIANTING)) {
			if(owner.tianTingCount ==0) {
				toNextState(owner);
			}
		}
	}

	@Override
	public void toNextState(EXRoom owner) {
		owner.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
	}

}
