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.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
		
		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 [] kongGroup = new int [4];
		int card = room.activeCard;
		if (Util.checkCard(card, owner.cardInhand, 4)) {
			kongGroup[0] = card;
			kongGroup[1] = card;
			kongGroup[2] = card;
			kongGroup[3] = card;

			owner.kongGroup.add(kongGroup);
			if (owner.is_gang)
			{
				OpCard pongCard = new OpCard(RuleWeight.TYPE_KONG, card, activePlayer.seat);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				owner.opCardList.add(pongCard);
				owner.gangshangpao = true;
			}
			else {
				OpCard pongCard = new OpCard(RuleWeight.TYPE_ZHAO, card, activePlayer.seat);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				owner.opCardList.add(pongCard);
			}

			Util.removeCard(owner.cardInhand, card, 4);

			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			opcard.addInt(card);
			opcard.addInt(card);
			if (owner.is_gang)
			{
				EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_KONG, fromseat,opcard);
			}
			else {
				EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_ZHAO, fromseat,opcard);
			}
		}
		else if (Util.checkCard(card, owner.cardInhand, 3) && Util.checkCard(card /100 * 100 + 4, owner.cardInhand, 1)) {
			kongGroup[0] = card;
			kongGroup[1] = card;
			kongGroup[2] = card;
			kongGroup[3] = card /100 * 100 + 4;

			owner.kongGroup.add(kongGroup);
			if (owner.is_gang)
			{
				OpCard pongCard = new OpCard(RuleWeight.TYPE_KONG, card, activePlayer.seat);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card /100 * 100 + 4);
				owner.opCardList.add(pongCard);
				owner.gangshangpao = true;
			}
			else {
				OpCard pongCard = new OpCard(RuleWeight.TYPE_ZHAO, card, activePlayer.seat);
				pongCard.opCard = new TArray();
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card);
				pongCard.opCard.addInt(card /100 * 100 + 4);
				owner.opCardList.add(pongCard);
			}

			Util.removeCard(owner.cardInhand, card, 3);
			Util.removeCard(owner.cardInhand, card /100 * 100 + 4, 1);

			ITArray opcard = TArray.newInstance();
			opcard.addInt(card);
			opcard.addInt(card);
			opcard.addInt(card /100 * 100 + 4);
			if (owner.is_gang)
			{
				EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_KONG, fromseat,opcard);
			}
			else {
				EXMainServer.gameCtr.actionCard(owner, card, RuleWeight.TYPE_ZHAO, 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) {
		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
		
	}

}
