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 com.taurus.core.util.Logger;

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> {
	Logger logger = Logger.getLogger(EXRoomDealState.class);
	@Override
	public void enter(EXRoom owner) {
		
		//洗牌
		owner.card.init();
		logger.info("bankerseat===>"+owner.bankerSeat);
		for(Entry<Integer,Player> en : owner.playerMapBySeat.entrySet()) {
			logger.info("en-===>"+en.getKey());
		}
		// 如果没有庄家，确定一个庄家
		if (owner.bankerSeat == 0) {
			//随机一个庄家
			int a=(int)(Math.random()*(owner.playerMapBySeat.size()))+1;
			owner.bankerSeat = a;
		}
		
		//发牌
		EXMainServer.gameCtr.dealCard(owner);
		
		// 去底牌
		if (owner.maxPlayers == 2 ) {
			int qupaiV = owner.config.getInt(Config.ROOM_CONFIG_QUPAI);
			if(qupaiV > 0) {
				
				int num = qupaiV * 5 + 5;
				for (int index = 0; index < num; index++) {
					owner.throwCardsList.add(owner.card.pop());
				}
			}
		}
		
		// 生成游戏记录对象
		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));
	}
	
	public static void main(String[] args) {
		for(int i = 0;i < 100;i++) {
			
			int a=(int)(Math.random()*(2))+1;
			System.out.println(a);
		}
		
	}

}
