package extend.zp.player.rule;

import com.game.Global;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;

import com.taurus.core.util.Logger;
import extend.zp.EXPlayer;
import extend.zp.RuleWeight;
import extend.zp.player.rulestate.*;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.tip.IRuleBase;
import extend.zp.tip.Tip;
import extend.zp.uitl.CardUtil;


public class RuleWeiLiu implements IRuleBase {
    Logger logger = Logger.getLogger(getClass());
    public boolean qlflag = false;

    @Override
    public boolean condition(EXPlayer player) {
        if (player.isDeadPut)
        {
            return false;
        }
        //player.tipMgr.clean();
        int eventCard = player.drawCard;
        logger.info("jefe weiliu wincard:" + player.winCard);
        logger.info("jefe weiliu rreventCard:" + eventCard);
        logger.info("jefe weiliu player.weiGroup:" + player.weiGroup.size());
        logger.info("jefe weiliu player.KAN:" + player.kanGroup.size());
        logger.info("jefe weiliu CardUtil.cardNum:" + CardUtil.cardNum(eventCard, player.cardInhand));
        logger.info("jefe weiliu player.cardInhand:" + player.cardInhand);

        logger.info("jefe weiliu player.it:" + player.tiGroup);
        if (!(player.stateMachine.curState instanceof PRWeiState)) {
            for (int i = 0; i < player.weiGroup.size(); i++) {
                logger.info("jefe weiliu player.weiGroup:" + player.weiGroup.get(0)[0]);
                if (eventCard == player.weiGroup.get(i)[0]) {
                    //if (CardUtil.cardNum(eventCard, player.cardInhand) == 1) {
                    ITArray opcard = TArray.newInstance();
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    Tip tip = new Tip(eventCard, opcard, RuleWeight.TI, this, RuleWeight.TYPE_TI);
                    player.tipMgr.addTip(tip);
                    return true;
                }
            }
        }
        boolean flag = false;

        if((player.stateMachine.curState instanceof PRChiState)||(player.stateMachine.curState instanceof PRPongState)||(player.stateMachine.curState instanceof PRWeiState)||(player.stateMachine.curState instanceof PRDrawPongState)||(player.stateMachine.curState instanceof PRDrawChiState)){

            if (player.tiGroup.size()>0){
                for (int i = 0; i < player.tiGroup.size(); i++) {
                    eventCard = player.tiGroup.get(i)[0];
                    logger.info("jefe weitiliuaaaeventCard:" + eventCard);
                    ITArray opcard = TArray.newInstance();
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    Tip tip = new Tip(eventCard, opcard, RuleWeight.TI, this, RuleWeight.TYPE_TI);
                    player.tipMgr.addTip(tip);
                    this.qlflag = true;
                    flag = true;
                   // return true;
                }
            }
            for (int i = 0; i < player.weiGroup.size(); i++) {
               // if (eventCard == player.weiGroup.get(i)[0] && CardUtil.cardNum(eventCard, player.cardInhand) == 1) {
                if ( CardUtil.cardNum(eventCard, player.cardInhand) == 1) {
                    ITArray opcard = TArray.newInstance();
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    opcard.addInt(eventCard);
                    Tip tip = new Tip(eventCard, opcard, RuleWeight.TI, this, RuleWeight.TYPE_TI);
                    player.tipMgr.addTip(tip);
                    return true;
                }else {

                    for (int j = 0; j < player.cardInhand.size(); j++) {
                        if (player.cardInhand.get(j) == player.weiGroup.get(i)[0]) {
                            eventCard = player.cardInhand.get(j);
                            logger.info("jefe weiliuaaaeventCard:" + eventCard);
                            ITArray opcard = TArray.newInstance();
                            opcard.addInt(eventCard);
                            opcard.addInt(eventCard);
                            opcard.addInt(eventCard);
                            Tip tip = new Tip(eventCard, opcard, RuleWeight.TI, this, RuleWeight.TYPE_TI);
                            player.tipMgr.addTip(tip);
                            return true;
                        }
                    }
                }
            }
        //

        }
        logger.info("jefe weiliu end" + player.tiGroup);
        return flag;
    }

    @Override
    public void action(EXPlayer player) {
        logger.info("jefe weiliu action" );
        if (this.qlflag){
            this.qlflag = false;
            player.stateMachine.changeState(Global.getState(PRDealLiuState.class));
        }else{
            player.stateMachine.changeState(Global.getState(PRWeiLiuState.class));
        }


    }
}
