package extend.zp.player.state;

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

import com.game.Global;
import com.game.Util;
import com.game.data.Player;
import com.game.player.state.PlayerPauseState;
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.core.util.Utils;
import com.taurus.permanent.TPServer;

import extend.zp.Config;
import extend.zp.EXActionEvent;
import extend.zp.EXGameController;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.EXRoom;
import extend.zp.EXScore;
import extend.zp.PlayerRuleManager;
import extend.zp.room.state.EXRoomSetpState;


public class EXPlayerDrawState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		if (room.card.cardList.size() == 0) {
			room.liuju = true;
			if (room.config.containsKey(Config.ROOM_CONFIG_ZHUANGXIAN)) {
				int xianScore = (room.config.getInt(Config.ROOM_CONFIG_ZHUANGXIAN) + 1)*10;
				for(Entry<Integer, Player> entry : room.playerMapByPlaying.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					room.addDouLiuZiScore(player, player.seat == room.bankerSeat ? xianScore +10 : xianScore, EXScore.DOULIUZISCORE);
				}
			}
			room.bankerSeat = room.playerMapBySeat.get(room.bankerSeat).nextSeat;
			room.endGame();
			return;
		}
		draw(owner);
	}
	
	void draw(EXPlayer owner) {
		EXRoom room = owner.getRoom();

		int drawCard = room.card.pop();
		//drawCard = 103;
		room.showCard = drawCard;
		room.lastPaoSeat = 0;
		room.isAllPass = false;
		room.draw = owner.seat;
		int leftCount = room.card.getCount();
		if (!owner.siShou && Util.cardNum(drawCard, owner.cardInhand) >= 2) {
			ITObject param = new TObject();
			param.putInt("card", drawCard);
			param.putInt("seat", room.activeSeat);
			param.putInt("left_count", leftCount);
			owner.sendEvent(Config.GAME_EVT_DRAW, param);
			ITObject otherParam = new TObject();
			Utils.objectCopy(param, otherParam);
			otherParam.putInt("card", 0);
			room.broadCastToClient(owner.playerid, Config.GAME_EVT_DRAW, otherParam);
			room.getPlayBack().addGetCardCommand(room.activeSeat, drawCard, leftCount);
			//owner.stateMachine.changeState(Global.getState(EXPlayerWeiState.class));
			timer1(owner);
			return;
		}
		((EXGameController) Global.gameCtr).getCard(owner, leftCount);
		this.toNextState(owner);
	}

	@Override
	public void toNextState(EXPlayer owner) {
		if (!owner.getRoom().isAllPass) {
			EXMainServer.playerRuleMgr.conditionEx(PlayerRuleManager.DRAW_RULE, owner);
			owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_OTHER_DRAW, null);
			if (owner.getRoom().tipMap.size() != 0) {
				if (!(owner.stateMachine.curState instanceof EXPlayerDisCardTipState)) {
					owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
				}
				return;
			}
		}
		timer(owner);
	}

	public static void timer1(EXPlayer owner) {
		TPServer.me().getTimerPool().schedule(new Runnable() {
			@Override
			public void run() {
				if (owner.room == null) {
					Global.logger.error("EXPlayerDiscardState room is null");
					return;
				}
				if (owner.room.isDestroy)
					return;
				owner.room.enqueueRunnable(new Runnable() {
					@Override
					public void run() {
						owner.stateMachine.changeState(Global.getState(EXPlayerWeiState.class));
					}
				});
			}
		}, 1500, TimeUnit.MILLISECONDS);
	}
	
	public static void timer(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		if (room.card.getCount() == 0) {
			room.liuju = true;
			if (room.config.containsKey(Config.ROOM_CONFIG_ZHUANGXIAN)) {
				int xianScore = (room.config.getInt(Config.ROOM_CONFIG_ZHUANGXIAN) + 1)*10;
				for(Entry<Integer, Player> entry : room.playerMapByPlaying.entrySet()) {
					EXPlayer player = (EXPlayer) entry.getValue();
					room.addDouLiuZiScore(player, player.seat == room.bankerSeat ? xianScore +10 : xianScore, EXScore.DOULIUZISCORE);
				}
			}
			room.bankerSeat = room.playerMapBySeat.get(room.bankerSeat).nextSeat;
			room.endGame();
			return;
		}
		if (owner.getRoom().isAllPass) {
			ITObject param = new TObject();
			param.putInt("card", room.showCard);
			param.putInt("seat", owner.seat);
			room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, param);
			int showCard = room.showCard;
			owner.outcardList.add(showCard);
			room.showCard = 0;
			room.getPlayBack().addThrowCardCommand(owner.seat, showCard);
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
		} else {
			TPServer.me().getTimerPool().schedule(new Runnable() {
				@Override
				public void run() {
					if (owner.room == null) {
						Global.logger.error("EXPlayerDrawState room is null");
						return;
					}
					if (owner.room.isDestroy)
						return;
					owner.room.enqueueRunnable(new Runnable() {
						@Override
						public void run() {
							ITObject param = new TObject();
							param.putInt("card", room.showCard);
							param.putInt("seat", owner.seat);
							owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, param);
							int showCard = room.showCard;
							owner.outcardList.add(showCard);
							owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, showCard);
							room.showCard = 0;
							owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
							owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
						}
					});
				}
			}, 1500, TimeUnit.MILLISECONDS);
		}
	}
	
}














