package extend.pk.room.state;


import com.game.Global;
import com.game.data.Player;
import com.game.state.StateBase;
import com.taurus.permanent.TPServer;
import extend.pk.*;
import extend.pk.player.state.EXPlayerBaoChunState;
import extend.pk.EXPlayer;
import extend.pk.EXRoom;
import extend.pk.player.state.EXPlayerPassState;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 房间发牌状态
 * 
 * 
 */
public class EXRoomDealState extends StateBase<EXRoom> {
	@Override
	public void enter(EXRoom owner) {
		boolean donghua = false;
		for (Map.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 {
			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() {
						owner.stateMachine.toNextState();
					}
				});
			}
		}, 3000, TimeUnit.MILLISECONDS);
	}

	@Override
	public void toNextState(EXRoom owner) {
		//owner.card.init();
		if(owner.bankerSeat!=0&&!owner.playerMapBySeat.containsKey(owner.bankerSeat)) {
			owner.bankerSeat = 0;
		}
		EXMainServer.gameCtr.dealCard(owner);

		owner.playBackData = new EXPlayBack(owner);

		Global.logger.error("deal room tonextstate");
		//定庄家

		if(owner.baochunstate<0){
			owner.stateMachine.changeState(Global.getState(EXRoomBaoChunState.class));
		}
	//	owner.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
	}
}
