package extend.mj.player.state;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map.Entry;

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.Config;
import extend.mj.EXActionEvent;
import extend.mj.EXMainServer;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.room.state.EXRoomSetpState;
import extend.mj.tip.TipManager;
import extend.mj.uitl.Paixing;

/**
 * 
 */
public class EXPlayerDiscardState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		EXMainServer.gameCtr.discardTipEvent(owner);
		if (owner.autoOutCard) {
			owner.startActionTimer(1000);
		} else {
			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();
	}

	private void _action(EXPlayer owner, int discard, int gid) {
		if (owner.autoOutCard && discard != owner.drawCard) {
			ITObject reconParam = new TObject();
			owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
			return;
		}
		if (!Util.checkCard(discard, owner.cardInhand)) {
			ITObject reconParam = new TObject();
			owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
			return;
		}
		owner.louhu = false;
		owner.drawCard = 0;
		EXMainServer.gameCtr.outCard(owner, discard);
		boolean ting = owner.room.config.getBoolean(Config.ROOM_CONFIG_NATIVE_HU);
		if (owner.seat == owner.room.bankerSeat && owner.outCardCount == 0 && ting && Paixing.tingCheck(owner.opCardList, owner.cardInhand)) {
			owner.stateMachine.changeState(Global.getState(EXPlayerTianTingTipState.class));
		} else {
			owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_OTHER_DISCARD, discard);
			EXRoom room = owner.getRoom();
			if (room.tipMap.size() != 0) {
				int win_count = 0;
				for (Entry<Integer, TipManager> entry : room.tipMap.entrySet()) {
					TipManager tipmgr = entry.getValue();
					if (tipmgr.isWin)
						win_count++;
				}
				room.tongpao = 0;
				if (win_count > 1)
					room.tongpao = win_count;
				owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
			} else {
				this.toNextState(owner);
			}
		}
		owner.outCardCount++;
	}

	@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");
			ITArray card_list = netParam.getTArray("card_list");
			ITArray outcard_list = netParam.getTArray("outcard_list");
			if (card_list == null) {
				card_list = TArray.newInstance();
			}
			if (outcard_list == null) {
				outcard_list = TArray.newInstance();
			}

			ArrayList<Integer> tmpCardList = new ArrayList<Integer>();
			for(int i = 0; i < card_list.size(); i++)
			{
				int card = card_list.getInt(i);
				tmpCardList.add(card);
			}

			ArrayList<Integer> out_tmpCardList = new ArrayList<Integer>();
			for(int i = 0; i < outcard_list.size(); i++)
			{
				int card = outcard_list.getInt(i);
				out_tmpCardList.add(card);
			}


			Collections.sort(tmpCardList);
			Collections.sort(out_tmpCardList);

			ArrayList<Integer> tmpCardList2 = new ArrayList<Integer>();
			tmpCardList2.addAll(owner.cardInhand);
			Collections.sort(tmpCardList2);

			ArrayList<Integer> out_tmpCardList2 = new ArrayList<Integer>();
			out_tmpCardList2.addAll(owner.outcardList);
			Collections.sort(out_tmpCardList2);

			if (!tmpCardList.equals(tmpCardList2) || !out_tmpCardList.equals(out_tmpCardList2))
			{
				ITObject reconParam = new TObject();
				owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
				return;
			}
			_action(owner, discard, gid);
			break;
		case ActionEvent.EVENT_TIMER_AUTO:
			discard = owner.drawCard;
			if (owner.drawCard == 0) {
				discard = owner.cardInhand.get(owner.cardInhand.size() - 1);
			}
			_action(owner, discard, gid);
			break;
		case ActionEvent.EVENT_ENTRUST:
			owner.startActionTimer();
			break;
		default:
			break;
		}

	}

}
