package extend.mj.player.rulestate;

import com.game.Global;
import com.game.Util;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
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;

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

	@Override
	public void enter(EXPlayer owner) {
		// TODO Auto-generated method stub
		owner.is_fang_jian_gang = false;
		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++;
		
		EXMainServer.gameCtr.changeActiveSeat(room,owner.seat);
		owner.cardInhand.add(owner.getRoom().activeCard);
		int card = room.activeCard;

		owner.is_fang_jian_gang = false;
		if (owner.getRoom().isFanPai(card))
		{
			int [] kongGroup = new int [4];
			kongGroup[0] = card;
			kongGroup[1] = card;
			kongGroup[2] = card;

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

			ITArray opcard = TArray.newInstance();
			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);
			owner.fang_jian_num++;
			if(!EXMainServer.playerRuleMgr.condition(PlayerRuleManager.CHOW_PONG_DISCARD_RULE, owner,false)){
				owner.is_fang_jian_gang = false;
				owner.drawCard = 0;
				owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
			}
			else {
				owner.is_fang_jian_gang = true;
			}
		}
		else {
			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 = card;
			boolean qiangkong = room.config.getBoolean(Config.ROOM_CONFIG_QIANGKONG_MING);
			if(qiangkong) {
				owner.room.broadCastToServer(owner.playerid, EXActionEvent.EVENT_KONG_WIN, null);
				if(room.tipMap.size() == 0){
					room.activeCard = 0;
					room.lastDiscardSeat = 0;
					owner.settleLog.add(Config.SETTLE_MING_KONG);
					toNextState(owner);
				}else{
					owner.stateMachine.changeState(Global.getState(EXPlayerWaitKongWinState.class));
				}
			}else {
				room.activeCard = 0;
				room.lastDiscardSeat = 0;
				owner.settleLog.add(Config.SETTLE_MING_KONG);
				toNextState(owner);
			}
		}
	}

	@Override
	public void toNextState(EXPlayer owner) {
		if (owner.is_fang_jian_gang)
		{
			owner.is_fang_jian_gang = false;
			owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
		}
		else {
			owner.is_fang_jian_gang = false;
			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
		
	}

}
