package extend.zp.player.state;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

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


public class EXPlayerDiscardState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {

		owner.getRoom().showCard = 0;
		List<Integer> sList = new ArrayList<Integer>();
		sList.addAll(owner.suoCardList);
		if(sList.containsAll(owner.cardInhand)) {
			owner.siShou = true;
			ITObject param = new TObject();
			param.putInt("seat", owner.seat);
			owner.getRoom().broadCastToClient(0, Config.GAME_EVT_SISHOU, param);
			toNextSetp(owner);
			return;
		}
		EXMainServer.gameCtr.ting(owner);
		EXMainServer.gameCtr.discardTipEvent(owner);
		owner.startActionTimer();
	}
	
	private void toNextSetp(EXPlayer owner) {
		owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
	}

	@Override
	public void toNextState(EXPlayer owner) {
		timer(owner);
	}
	
	@Override
	public void exit(EXPlayer owner) {
		owner.stopActionTimer();
	}
	
	private void discard(EXPlayer owner, int discard) {
		int cardNum = Util.cardNum(discard, owner.cardInhand);
		if(cardNum == 0 || cardNum == 3) {
			return;
		}
		if(owner.suoCardList.contains(discard)) return;
		owner.chouList.add(discard);
		EXRoom room = owner.getRoom();
		room.showCard = discard;
		room.currenDiscardSeat = 0;
		room.lastPaoSeat = 0;
		room.draw = 0;
		EXMainServer.gameCtr.outCard(owner, discard);
		owner.tingList.clear();
		if (owner.tingMap.containsKey(discard)) {
			owner.tingList.addAll(owner.tingMap.get(discard));
		}
		owner.tingMap.clear();
		if (owner.getRoom().tipMap.size() != 0) {
			owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
		} else {
			this.toNextState(owner);
		}
	}
	
	@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:
				int card=owner.autoDiscard();
				Global.logger.info(owner+" entust auto card :"+card);
				discard(owner,card );
				break;
			default:
				break;
		}

	}

	
	public static void timer(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() {
						ITObject resData = new TObject();
						resData.putInt("card", owner.getRoom().showCard);
						resData.putInt("seat", owner.seat);
						owner.room.broadCastToClient(0, Config.GAME_EVT_THROW_CARD, resData);
						int showCard = owner.getRoom().showCard;
						owner.outcardList.add(showCard);
						System.out.println("laile=================================");
						owner.getRoom().getPlayBack().addThrowCardCommand(owner.seat, showCard);
						owner.getRoom().showCard = 0;
						owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
						owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
					}
				});
			}
		}, 1500, TimeUnit.MILLISECONDS);
	}
	
}
