package extend.mj.player.state;

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.*;

import extend.mj.*;
import extend.mj.room.state.EXRoomSetpState;

/**
 * �ȴ���ҳ���״̬
 * 
 */
public class EXPlayerDiscardState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		if (owner.is_gang)
		{
			owner.louhu = false;
			int discard = owner.drawCard;
			if(owner.drawCard == 0) {
				discard = owner.cardInhand.get(owner.cardInhand.size() - 1);
			}

			EXMainServer.gameCtr.outCard(owner, discard);
			owner.gangshangpao = false;

			if (owner.getRoom().tipMap.size() != 0) {
				owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
			} else {
				this.toNextState(owner);
			}
		}
		else {
			EXMainServer.gameCtr.discardTipEvent(owner);
			owner.startActionTimer();
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
	}
	
	

	@Override
	public void exit(EXPlayer owner) {
		owner.stopActionTimer();
	}
	
	public void reload(EXPlayer owner) {
		owner.startActionTimer();
	}
	
	
	@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");

				if (!Util.checkCard(discard, owner.cardInhand)) {
					ITObject reconParam = new TObject();
					owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
					return;
				}
				owner.louhu = false;
				EXMainServer.gameCtr.outCard(owner, discard);
				owner.gangshangpao = false;

				if (owner.getRoom().tipMap.size() != 0) {
					owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
				} else {
					this.toNextState(owner);
				}
				break;
			case ActionEvent.EVENT_TIMER_AUTO:
				owner.louhu = false;
				discard = owner.drawCard;
				if(owner.drawCard == 0) {
					discard = owner.cardInhand.get(owner.cardInhand.size() - 1);
				}

				EXMainServer.gameCtr.outCard(owner, discard);
				owner.gangshangpao = false;

				if (owner.getRoom().tipMap.size() != 0) {
					owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
				} else {
					this.toNextState(owner);
				}
				break;	
			case ActionEvent.EVENT_ENTRUST:
				owner.startActionTimer();
				break;
			default:
				break;
		}

	}

}
