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.ITArray;
import com.taurus.core.entity.ITObject;

import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject;
import extend.mj.*;
import extend.mj.player.rule.RuleSelfWin;
import extend.mj.player.rulestate.PRSelfWinState;
import extend.mj.room.state.EXRoomSetpState;
import extend.mj.tip.Tip;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;

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

	@Override
	public void enter(EXPlayer owner) {
		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");
				Global.logger.info("jefe discard:"+discard);
				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;
				}
				if (!Util.checkCard(discard, owner.cardInhand)) {
					ITObject reconParam = new TObject();
					owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
					return;
				}
				owner.outputCardList.add(discard);
				Global.logger.info("logger jefe"+owner.outputCardList);
				if (owner.outputCardList.size()==13||owner.outputCardList.size()==10){
					HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
				//	WinCard win = new WinCard(owner.cardInhand, discard, owner.getRoom().card.laiziCard, owner.getRoom().card.laiziCard2, false, true);
					//if (win.checkWin(map, owner, owner.getRoom(), true)) {
						int tangzihu = WinCard.isTangZiHu(owner.outputCardList);
						Global.logger.info("tangzihu jefe"+tangzihu);
						if (tangzihu>0){
							ITArray opcard = TArray.newInstance();
							opcard.addInt(discard);
							Tip tip = new Tip(discard,opcard, RuleWeight.SELF_WIN,new RuleSelfWin(), RuleWeight.TYPE_WIN);
							tip.winMap = map;
							tip.self = true;
							owner.tipMgr.addTip(tip);
							owner.winMap = tip.winMap;
							if (tangzihu==1){
								WinCard.putWinCardType(map, WinCardType.SHILAOTOU, 1,WinCardType.SHILAOTOU_SCORE, false, "十老头");
							}else if(tangzihu==2){
								WinCard.putWinCardType(map, WinCardType.TANGZISHISANYAO, 1,WinCardType.TANGZISHISANYAO_SCORE, false, "塘子十三ㄠ");
							}else if(tangzihu==3){
								WinCard.putWinCardType(map, WinCardType.TANGZIQIXIAODUI, 1,WinCardType.TANGZIQIXIAODUI_SCORE, false, "塘子小七对");
							}else if(tangzihu==4){
								//塘子胡七星
								WinCard.putWinCardType(map, WinCardType.TANGZIQIXING, 1,WinCardType.TANGZIQIXING_SCORE, false, "塘子胡七星");
							}else if(tangzihu==5){
								WinCard.putWinCardType(map, WinCardType.TANGZIYIBANGAO, 1,WinCardType.TANGZIYIBANGAO_SCORE, false, "塘子一般高");
							}else if(tangzihu==6){
								WinCard.putWinCardType(map, WinCardType.TANGZIQIXINGYIBANGAO, 1,WinCardType.TANGZIQIXINGYIBANGAO_SCORE, false, "塘子七星一般高");
							}else if(tangzihu==7){
								WinCard.putWinCardType(map, WinCardType.TANGZILANPAI, 1,WinCardType.TANGZILANPAI_SCORE, false, "塘子烂牌");
							}
							owner.istangzihu = true;
							if (owner.seat==owner.getRoom().bankerSeat){
								WinCard.putWinCardType(map, WinCardType.ZHUANGJIA, 1,WinCardType.BASE_HU_SCORE, false, "庄家");
							}else{
								WinCard.putWinCardType(map, WinCardType.XIANJIA, 1,WinCardType.BASE_HU_SCORE, false, "闲家");
							}
							owner.winCard = discard;
							owner.drawCard = discard;
							owner.cardInhand = owner.outputCardList;
							WinCard.putWinCardType(map, WinCardType.YING_MO, 1,WinCardType.BASE_HU_SCORE, false, "塘子胡");

							owner.stateMachine.changeState(Global.getState(PRSelfWinState.class));
							break;
						}

					//}
				}

				owner.notPongList.clear();
				owner.louhu = false;
				owner.liangang=0;

				EXMainServer.gameCtr.outCard(owner, discard);


				owner.gangshangpao = false;
				if (owner.getRoom().isLaizi(discard))
				{
					if (owner.getRoom().tipMap.size() != 0) {
						owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
					} else {
						owner.cheng_num++;
						owner.is_cheng = true;
						owner.stateMachine.changeState(Global.getState(EXPlayerChengDrawState.class));
					}
				}
				else {
					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);
				}

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

				if (owner.getRoom().isLaizi(discard))
				{
					if (owner.getRoom().tipMap.size() != 0) {
						owner.stateMachine.changeState(Global.getState(PlayerPauseState.class));
					} else {
						owner.cheng_num++;
						owner.is_cheng = true;
						owner.stateMachine.changeState(Global.getState(EXPlayerChengDrawState.class));
					}
				}
				else {
					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;
		}

	}

}
