package extend.mj.player.rule;

import com.game.Global;
import com.game.Util;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import extend.mj.Config;
import extend.mj.EXPlayer;
import extend.mj.EXRoom;
import extend.mj.RuleWeight;
import extend.mj.player.rulestate.PRChowState;
import extend.mj.tip.IRuleBase;
import extend.mj.tip.Tip;

import java.util.List;

/**
 * ����״̬
 *
 *
 * 2017��11��2��
 * PRChowRule.java
 */
public class RuleChow implements IRuleBase {

    @Override
    public boolean condition(EXPlayer player) {
        EXRoom room = player.getRoom();
        if (player.isEntrust() || !room.config.getBoolean(Config.ROOM_CONFIG_CHUIPAI)) {
            return false;
        }

        if (player.lastSeat != room.activeSeat) {
            return false;
        }

        if (player.is_gang)
        {
            return false;
        }

        EXPlayer activePlayer = (EXPlayer)room.playerMapBySeat.get(room.activeSeat);
        if (activePlayer.gangshangpao)
        {
            return false;
        }

        int eventCard = room.activeCard;
        boolean result = check(player,eventCard);

        return result;
    }

    private boolean check(EXPlayer player,int eventCard){
        if (eventCard % 100 == 4)
        {
            return false;
        }

        boolean result = false;
        List<Integer>	cardInhand = player.cardInhand;
        if (Util.checkCard(eventCard - 1, cardInhand)  && Util.checkCard(eventCard - 2, cardInhand) && (eventCard-1) % 100 != 4 && (eventCard-2) % 100 != 4) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard - 1);
            opcard.addInt(eventCard - 2);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        if (Util.checkCard(eventCard - 1, cardInhand)  && (eventCard-1) % 100 != 4 && Util.checkCard(eventCard / 100 * 100 + 4, cardInhand)) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard - 1);
            opcard.addInt(eventCard / 100 * 100 + 4);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        if (Util.checkCard(eventCard - 2, cardInhand) && (eventCard-2) % 100 != 4 && Util.checkCard(eventCard / 100 * 100 + 4, cardInhand)) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard - 2);
            opcard.addInt(eventCard / 100 * 100 + 4);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }

        if (Util.checkCard(eventCard + 1, cardInhand) && Util.checkCard(eventCard - 1, cardInhand) && (eventCard + 1) % 100 != 4 && (eventCard - 1) % 100 != 4) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard + 1);
            opcard.addInt(eventCard - 1);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        if (Util.checkCard(eventCard + 1, cardInhand) && (eventCard + 1) % 100 != 4 && Util.checkCard(eventCard / 100 * 100 + 4, cardInhand)) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard + 1);
            opcard.addInt(eventCard / 100 * 100 + 4);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        if (Util.checkCard(eventCard + 1, cardInhand) && Util.checkCard(eventCard + 2, cardInhand) && (eventCard + 1) % 100 != 4 && (eventCard + 2) % 100 != 4) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard + 1);
            opcard.addInt(eventCard + 2);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        if (Util.checkCard(eventCard + 2, cardInhand) && (eventCard + 2) % 100 != 4 &&  Util.checkCard(eventCard / 100 * 100 + 4, cardInhand)) {
            ITArray opcard = TArray.newInstance();
            opcard.addInt(eventCard + 2);
            opcard.addInt(eventCard / 100 * 100 + 4);
            Tip tip = new Tip(eventCard,opcard, RuleWeight.CHOW,this, RuleWeight.TYPE_CHOW);
            player.tipMgr.addTip(tip);
            result = result || true;
        }
        return result;
    }

    @Override
    public void action(EXPlayer player,Tip tip) {
        player.getRoom().activeCard = tip.card;
        player.stateMachine.changeState(Global.getState(PRChowState.class));
    }

}
