package extend.mj.room.state;

import java.util.Map.Entry;
import java.util.Random;
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.core.entity.ITObject;
import com.taurus.core.entity.TObject;

import com.taurus.permanent.TPServer;
import extend.mj.*;
import extend.mj.player.state.EXPlayerPiaoNiaoTipState;

/**
 * ���䷢��״̬
 * 
 *
 * 2017��8��30��
 * EXRoomDealState.java
 */
public class EXRoomDealState extends StateBase<EXRoom> {

	@Override
	public void enter(EXRoom owner) {
		owner.card.init();
		if (owner.bankerSeat == 0) {
			//随机定庄
			Random random = new Random();
			owner.bankerSeat = random.nextInt(2) + 1;
		}
//		owner.isplaying = true;
//		owner.round += 1;

		int piao_niao = owner.config.getInt(Config.ROOM_CONFIG_PIAO_NIAO);
		if(piao_niao > 0) {
			if(piao_niao == 1) {
				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);
				}
				this.toNextState(owner);
			}
			else {
			this.toNextState(owner);
			}
		} else {
			this.toNextState(owner);
		}
	}

	public static void timer(EXRoom owner) {

		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() {
						ITObject param = new TObject();
						param.putInt("bank_seat", owner.bankerSeat);
						EXMainServer.gameCtr.dealCard(owner);
						owner.playBackData = new EXPlayBack(owner);
						owner.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
					}
				});
			}
		}, 4000, TimeUnit.MILLISECONDS);
	}

	@Override
	public void toNextState(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);
		}
		else {
			ITObject param = new TObject();
			param.putInt("bank_seat", owner.bankerSeat);
			EXMainServer.gameCtr.dealCard(owner);
			owner.playBackData = new EXPlayBack(owner);
			owner.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
		}
	}
	
	public void execute(EXRoom owner, String cmd, int gid, Object param) {
		if(cmd.equals(EXActionEvent.EVENT_PIAO_NIAO)) {
			if(owner.piaoNiaoCount==0) {
				this.toNextState(owner);
			}
		}
		
	}
}
