package extend.zp.room.state;

import java.util.Map.Entry;

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

import extend.zp.Config;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.PlayBack;
import extend.zp.PlayerRuleManager;
import extend.zp.player.state.EXPlayerPreDrawState;


public class EXRoomDealState extends StateBase<EXRoom> {

	@Override
	public void enter(EXRoom owner) {
		owner.card.init();
		if (owner.bankerSeat == 0) {
			//
			owner.bankerSeat = 1;
		}
		EXMainServer.gameCtr.dealCard(owner);
		Boolean qupaiV = owner.config.getBoolean(Config.ROOM_CONFIG_QUPAI);
		boolean qupai = (qupaiV == null) ? false : qupaiV;
		if (owner.maxPlayers == 2 && qupai) {
			for (int index = 0; index < 20; index++) {
				owner.throwCardsList.add(owner.card.pop());
			}
		}
		// owner.isplaying = true;
		// owner.round += 1;
		owner.playBackData = new PlayBack(owner);
		for (Entry<Integer, Player> entry : owner.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			if (player.seat != owner.bankerSeat) {
				EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DEAL_RULE, player);
			}
		}
		this.toNextState(owner);
	}

	@Override
	public void toNextState(EXRoom owner) {
		EXPlayer player = (EXPlayer) owner.playerMapBySeat.get(owner.bankerSeat);
		EXMainServer.gameCtr.changeActiveSeat(owner, player.seat);
		player.stateMachine.changeState(Global.getState(EXPlayerPreDrawState.class));
	}

}
