package extend.pk.player.state;

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

import com.game.Global;
import com.game.Router;
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.TObject;
import extend.pk.CardGroup;
import extend.pk.CardObj;
import extend.pk.Config;
import extend.pk.EXActionEvent;
import extend.pk.EXMainServer;
import extend.pk.EXPlayer;
import extend.pk.EXRoom;
import extend.pk.room.state.EXRoomSetpState;
import extend.pk.uitl.CardCheck;
import extend.pk.uitl.CardConfig;
import extend.pk.uitl.CardUtil;

/**
 * 等待玩家出牌状态
 * 
 * 
 */
public class EXPlayerDiscardState extends StateBase<EXPlayer> {
	
	@Override
	public void enter(EXPlayer owner) {
		
		// 设置防作弊场开牌的标志
		owner.open = 1;
		
		EXRoom room = owner.getRoom();
		
		//如果最后一次的打牌的座位不等于零并且最后一次打牌的座位不等于自己
		if(room.lastDiscardSeat != 0 &&room.lastDiscardSeat != owner.seat) {
			if (owner.cardInhand.size() == 0) {
				EXPlayer lastPlayer = (EXPlayer)room.playerMapBySeat.get(room.lastDiscardSeat);
				if (lastPlayer != null)
				{
					room.add510kScore(lastPlayer);
				}
				this.over(owner);
				return;
			}

 			CardGroup big_ct = CardCheck.genOutCard(owner.cardInhand, room.discard,room.config, false);
			if(big_ct==null) {
				owner.stateMachine.changeState(Global.getState(EXPlayerPassState.class));
				return;
			}
		}
		else {
			room.curPaiFen = 0;
			if (owner.cardInhand.size() == 0) {
				this.over(owner);
				return;
			}
		}
		EXMainServer.gameCtr.discardTipEvent(owner);
		
		owner.startActionTimer();
	}

	@Override
	public void reload(EXPlayer owner) {
		EXMainServer.gameCtr.discardTipEvent(owner);
	}

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

	public void over(EXPlayer owner) {
		EXRoom room = owner.getRoom();
		room.bankerSeat = owner.seat;
		room.win = owner;
		room.endGame();
	}

	@Override
	public void execute(EXPlayer owner, String cmd, int gid, Object param) {
		switch (cmd) {
		case EXActionEvent.EVENT_DISCARD:
			
			ITObject netParam = (ITObject) param;
			ITArray card_list = netParam.getTArray("card");
			ITArray all_card = netParam.getTArray("all_card");

			if (all_card != null)
			{
				ArrayList<Integer> temCardList = new ArrayList<Integer>();
				for(int i=0;i< all_card.size();++i) {
					temCardList.add(all_card.getInt(i));
				}
				Collections.sort(temCardList);

				ArrayList<Integer> temCardList2 = new ArrayList<Integer>();
				for(int i=0;i< owner.cardInhand.size();++i) {
					temCardList2.add(owner.cardInhand.get(i).card);
				}
				Collections.sort(temCardList2);

				if (!temCardList.equals(temCardList2))
				{
					ITObject reconParam = new TObject();
					owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
					return;
				}
			}
			
			// 如果出的牌的个数为0 或者大于手牌的数目 则是非法的
			if (card_list.size() == 0 || card_list.size() > owner.cardInhand.size()) {
				EXMainServer.gameCtr.sendPutError(owner,Config.PUT_ERROR_INVALID_TYPE);
				ITObject reconParam = new TObject();
				owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
				return;
			}
			
			List<CardObj> list = CardUtil.toList(card_list);

			for(CardObj  card : list) {
				
				boolean bNotExisted = false;
				
				for(CardObj handc : owner.cardInhand) {
					if(card.card == handc.card) {
						bNotExisted = true;
						break;
					}
				}
				
				if(bNotExisted == false) {
					ITObject reconParam = new TObject();
					owner.sendEvent(Router.GAME_EVT__UPDATE_RECONECT, reconParam);
					return;
				}
			}
			
			CardGroup ct = CardCheck.getType(list, owner.room.config);
			if(ct == null) {
				EXMainServer.gameCtr.sendPutError(owner,Config.PUT_ERROR_INVALID_TYPE);
				return;
			}

			//验证客户端发过来的类型
			if(!CardCheck.tryType(ct,owner.cardInhand.size(),owner.room.config)) {
				EXMainServer.gameCtr.sendPutError(owner,Config.PUT_ERROR_INVALID_TYPE);
				return;
			}
			
			doAction(owner,ct,false);
			break;
			
		case EXActionEvent.EVENT_PASS:
			
			pass(owner);
			
			break;
		case EXActionEvent.EVENT_OFFLINE:
			owner.startActionTimer();
			break;
		case EXActionEvent.EVENT_TIMER_AUTO:
		case EXActionEvent.EVENT_ENTRUST:
			
			AutoOut(owner);
			break;
		}
	}
	
	private void AutoOut(EXPlayer owner) {
		
		EXRoom room = owner.getRoom();
		
		if(room.lastDiscardSeat != 0 &&room.lastDiscardSeat != owner.seat) {
			
			CardGroup big_ct = null;
			EXPlayer playerNext = (EXPlayer)room.playerMapBySeat.get(owner.nextSeat);
			if(playerNext != null && room.discard.config.type ==Config.TYPE_DANPAI && playerNext.cardInhand.size() == 1) {
				
				Map<Integer, List<CardObj>> cardMap =CardUtil.getCardListMap(owner.cardInhand);
				big_ct = CardCheck.selectDanpai(cardMap,room.discard,room.config,true);
			}else {
				big_ct = CardCheck.tryBig(owner.cardInhand, room.discard);
			}

			if (big_ct != null) {
				big_ct.card_list_mp = CardUtil.toTArray(big_ct.card_list);
				doAction(owner,big_ct,true);
			} else {
				Global.logger.info("big_ct == null pass !!!!!!!!!!!!");
				pass(owner);
			}

		}else {
		
			// 在托管或者自动出牌的时候  系统先出炸弹，如果炸弹没有 再出单张
			// 将来可以把这个函数做的能智能一些，不光可以出炸弹 也可以出顺子飞机等等
			CardGroup out_ct = CardCheck.autoOut(owner.cardInhand, owner.room.config);
			if(out_ct == null) {
				
				out_ct = new CardGroup();
				List<CardObj> out_list = new ArrayList<>();
				out_ct.card_list = out_list;
				out_ct.config = CardConfig.DAN;
				out_ct.len = 1;
				
				EXPlayer next = (EXPlayer) owner.room.playerMapBySeat.get(owner.nextSeat);
				if(next.cardInhand.size() == 1) {
					out_list.add(owner.cardInhand.get(owner.cardInhand.size() -1));
				}else {
					out_list.add(owner.cardInhand.get(0));
				}
			}
			
			out_ct.card_list_mp = CardUtil.toTArray(out_ct.card_list);
			out_ct.min_card = out_ct.card_list.get(0).cardMod;
			
			doAction(owner,out_ct,true);
		}
	}

	private void pass(EXPlayer owner) {
		EXMainServer.gameCtr.pass(owner);//打不起过
		this.toNextState(owner);//下一位大哥
	}
    
	private void doAction(EXPlayer owner,CardGroup ct,boolean skip) {
		
		EXRoom room = owner.getRoom();
		boolean outcard = true;
		if(!skip) {
			if(room.lastDiscardSeat != 0 &&room.lastDiscardSeat != owner.seat) {
				outcard = CardCheck.tryCompete(room.discard, ct,room.config);
			}
		}
		if(!outcard) {
			EXMainServer.gameCtr.sendPutError(owner,Config.PUT_ERROR_INVALID_TYPE);
			return;
		}

		EXMainServer.gameCtr.outCard(owner,ct);

		this.toNextState(owner);
	}


}
