package extend.zp.player.state;

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

import com.game.ActionEvent;
import com.game.Global;
import com.game.Router;
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.Logger;
import com.taurus.permanent.TPServer;

import extend.zp.Config;
import extend.zp.EXActionEvent;
import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.room.state.EXRoomSetpState;


public class EXPlayerDiscardState extends StateBase<EXPlayer> {
	
	Logger logger = Logger.getLogger(getClass());

	@Override
	public void enter(EXPlayer owner) {

		for (Map.Entry<Integer, Player> entry : owner.room.playerMapById.entrySet()) {
			EXPlayer player = (EXPlayer) entry.getValue();
			player.notChiSet.clear();
		}

		owner.getRoom().tipMap.clear();
		logger.info("jefe EXPlayerDiscardState:" +owner.cardInhand);
		owner.isGuoliu = false;
		if (owner.cardInhand.isEmpty()) {
			
			owner.isLaoYe = true;
			owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
			owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
			
		} else {
			boolean flag = true;
			for(int card : owner.cardInhand)
			{
				if (!owner.notPutSet.contains(card))
				{
				    flag = false;
				    break;
				}
			}

			if (flag)
			{

			    if (owner.kanGroup.size() > 0 || owner.tiGroup.size() > 0)
				{
					flag = false;
				}
			}

			if (flag)
			{
				owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
				owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
			}
			else
			{
				EXMainServer.gameCtr.discardTipEvent(owner);
				owner.startActionTimer();
			}
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		logger.info("jefe EXPlayerDiscardState toNextState:" +owner.isGuoliu);
		owner.isGuoliu = false;

		timer(owner);
	}
	@Override
	public void exit(EXPlayer owner) {
		owner.stopActionTimer();
	}

	@Override
	public void execute(EXPlayer owner, String cmd, int gid, Object param) {
		switch (cmd) {
		case EXActionEvent.EVENT_DISCARD:
			ITObject netParam = (ITObject) param;
			int discard = netParam.getInt("card");
			discard(owner, discard);
			break;
		case ActionEvent.EVENT_ENTRUST:
			owner.startActionTimer();
			break;
		case ActionEvent.EVENT_TIMER_AUTO:
			discard(owner, owner.autoDiscard());
			break;
		default:
			break;
		}
	}

	private void discard(EXPlayer owner, int discard) {
		
		if (!Util.checkCard(discard, owner.cardInhand)) {
			boolean flag = false;
			for (int i = 0; i < owner.kanGroup.size(); i++) {
				if (discard == owner.kanGroup.get(i)[0]) {
				    flag = true;
				    break;
				}
			}
			for (int i = 0; i < owner.tiGroup.size(); i++) {
				if (discard == owner.tiGroup.get(i)[0]) {
					flag = true;
					break;
				}
			}
			if (!flag)
			{
				ITObject param = new TObject();
				owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, param);
				return;
			}
		}
		logger.info("discard,player:" + owner + ",card:" + discard);
		if (owner.seat == owner.room.bankerSeat) {
			
			if (owner.outcardExList.size() > 0) {
				owner.isTingHu = false;
			}
		} else {
			owner.isTingHu = false;
		}

		if (owner.notPutSet.contains(discard)) {
			ITObject param = new TObject();
			owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, param);
			return;
		}

		owner.outcardExList.add(discard);
		exit(owner);
		EXMainServer.gameCtr.outCard(owner, discard);

		owner.getRoom().isDiHu = false;
		
		if (owner.getRoom().tipMap.size() != 0) {
			 owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
		} else {
			
			if (owner.getRoom().activeCard != 0) {
				this.toNextState(owner);
			}
		}
	}

	public static void timer(EXPlayer owner) {
		Global.logger.info("timer"+owner.getRoom().isAllPass);
		if (owner.getRoom().isAllPass) {
			
			ITObject resData = new TObject();
			resData.putInt("card", owner.getRoom().activeCard);
			resData.putInt("seat", owner.seat);
			owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, resData);
			owner.outcardList.add(owner.getRoom().activeCard);
			owner.getRoom().throwCardsSet.add(owner.getRoom().activeCard);
			owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, owner.getRoom().activeCard);

			owner.getRoom().activeCard = 0;
			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("EXPlayerDiscardState room is null");
						return;
					}
					
					if (owner.room.isDestroy)
						return;
					
					owner.room.enqueueRunnable(new Runnable() {
						
						@Override
						public void run() {
							
							ITObject resData = new TObject();
							resData.putInt("card", owner.getRoom().activeCard);
							resData.putInt("seat", owner.seat);
							owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, resData);
							
							owner.outcardList.add(owner.getRoom().activeCard);
							
							owner.getRoom().throwCardsSet.add(owner.getRoom().activeCard);
							owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, owner.getRoom().activeCard);

							owner.getRoom().activeCard = 0;
							
							owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
							owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
						}
					});
				}
			}, 1, TimeUnit.SECONDS);
		}
	}
}
