package extend.mj.player.rulestate;

import com.game.Global;
import com.game.Util;
import com.game.data.Player;
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 extend.mj.*;
import extend.mj.player.state.EXPlayerDiscardState;
import extend.mj.player.state.EXPlayerKongDrawState;
import extend.mj.player.state.EXPlayerWaitKongWinState;
import extend.mj.uitl.WinCard;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * ��Ӧ���ܴ���״̬
 * 
 *
 * 2017��8��30��
 * PROtherKongState.java
 */
public class PROtherKongState extends StateBase<EXPlayer> {

	@Override
	public void enter(EXPlayer owner) {
		// TODO Auto-generated method stub
		EXRoom room = owner.getRoom();
		EXPlayer activePlayer =  (EXPlayer)room.playerMapBySeat.get(owner.room.activeSeat);
		activePlayer.outcardList.remove(activePlayer.outcardList.size()-1);
		activePlayer.stateMachine.changeState(Global.getState(PlayerWaitState.class));
		int fromseat  = activePlayer.seat;
		room.addScore(owner,activePlayer, Config.MING_GANG_SCORE,EXScore.KONG);
		owner.ming_gang_num++;
		//activePlayer.dian_gang_num++;
		owner.liangang++;
		
		EXMainServer.gameCtr.changeActiveSeat(room,owner.seat);
		owner.cardInhand.add(owner.getRoom().activeCard);

		int card = room.activeCard;

			int [] kongGroup = new int [4];
			kongGroup[0] = card;
			kongGroup[1] = card;
			kongGroup[2] = card;
			kongGroup[3] = card;

			owner.kongGroup.add(kongGroup);
			owner.opCardList.add( new OpCard(RuleWeight.TYPE_KONG, card, activePlayer.seat, kongGroup));
			Util.removeCard(owner.cardInhand, card, 4);
			owner.gangshangpao = true;

			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			opcard.addInt(card);
			opcard.addInt(card);
			EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_KONG, fromseat,opcard);


			room.activeCard = 0;
			room.lastDiscardSeat = 0;
			owner.settleLog.add(Config.SETTLE_MING_KONG);
			//杠之后选择两个牌，未能下一轮

			room.gangnum = room.gangnum+1;
			EXMainServer.gameCtr.kaiGangEvent(owner);

			//toNextState(owner);


	}

	@Override
	public void toNextState(EXPlayer owner) {

		owner.stateMachine.changeState(Global.getState(EXPlayerKongDrawState.class));

	}

	@Override
	public void exit(EXPlayer owner) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void execute(EXPlayer owner, String cmd, int gid, Object param) {
		// TODO Auto-generated method stub
		switch (cmd) {

			case EXActionEvent.EVENT_DOGANG:
				ITObject netParam2 = (ITObject) param;
				int discard2 = netParam2.getInt("card");
				EXRoom room = owner.getRoom();
				RoomCard cards = room.card;
				Global.logger.info("cards list1:"+cards.cardList);
				cards.cardList.add(0,discard2);
				if (room.gangzi1 == discard2){
					//最后一张
					cards.cardList.remove(cards.cardList.size()-1);
					room.gangzi1 = cards.cardList.get(cards.cardList.size()-2);
					//room.gangzi2 = cards.cardList.get(cards.cardList.size()-1);
				}
				if (room.gangzi2 == discard2){
					//最后第二张
					cards.cardList.remove(cards.cardList.size()-2);
					//room.gangzi1 = cards.cardList.get(cards.cardList.size()-1);
					room.gangzi2 = cards.cardList.get(cards.cardList.size()-2);
				}

				EXMainServer.gameCtr.sendGangZiEvent(room);
				//}
				Global.logger.info("cards list3:"+cards.cardList);
				Global.logger.info("PROtherKongState:"+discard2);
				toNextState(owner);
				break;
			default:
				break;
		}
	}

}
