package extend.zp.player.state;

import java.util.concurrent.TimeUnit;

import com.game.ActionEvent;
import com.game.Global;
import com.game.Router;
import com.game.Util;
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.*;
import extend.zp.room.state.EXRoomSetpState;


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

	@Override
	public void enter(EXPlayer owner) {
		owner.getRoom().tipMap.clear();
		if (owner.cardInhand.isEmpty()) {
			owner.isLaoYe = true;
			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) {
		////System.out.println("tonextstate:"+owner.getRoom().isFanPao);
		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) {
		//System.outln("discard");
		if (!Util.checkCard(discard, owner.cardInhand)) {
			ITObject reconParam = new TObject();
			owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
			return;
		}
		logger.info("discard,player:" + owner + ",card:" + discard);

		////System.out.println("discard:");
		////System.out.println(owner.playerid);
		if (owner.seat == owner.room.bankerSeat) {
			if (owner.outcardExList.size() > 0) {
				owner.isTingHu = false;
			}
		} else {
			owner.isTingHu = false;
		}
		owner.outcardExList.add(discard);
		exit(owner);
		////System.out.println("discards:");
		owner.getRoom().roomoutcardlist.add(discard);
		owner.getRoom().paochucardlist.add(discard);
		EXMainServer.gameCtr.outCard(owner, discard);

		owner.getRoom().isDiHu = false;


		if (owner.getRoom().tipMap.size() != 0) {
			////System.out.println("dddssssssss11111");
			if (!(owner.stateMachine.curState instanceof EXPlayerDrawTipState)) {
				owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
            }
		} else {
			////System.out.println("dddssssssss1111122");
			if (owner.getRoom().activeCard != 0) {
				this.toNextState(owner);
			}
		}
	}

	public static void timer(EXPlayer owner) {
		////System.out.println("dddssssssss1111122222");
		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 {
			////System.out.println("dddssssssss11111444444");
			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));
						}
					});
				}
			}, 100, TimeUnit.MILLISECONDS);
		}
	}
}
