package extend.zp.player.rulestate;

import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.player.state.PlayerWaitState;
import com.game.state.StateBase;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.TArray;
import com.taurus.core.util.Logger;
import com.taurus.permanent.TPServer;

import extend.zp.EXMainServer;
import extend.zp.EXPlayer;
import extend.zp.OpCard;
import extend.zp.PlayerRuleManager;
import extend.zp.RuleWeight;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.tip.Tip;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;


public class PRDealLiuState extends StateBase<EXPlayer> {
    Logger logger = Logger.getLogger(getClass());
    @Override
    public void enter(EXPlayer owner) {
        owner.kongCount++;
        Tip actionTip = owner.actionTip;
        int eventCard = actionTip.card;
        logger.info("jefe PRDealLiuState :" + eventCard);
        for (int i = 0; i < owner.kanGroup.size(); i++) {
            if (eventCard == owner.kanGroup.get(i)[0]) {
                owner.winCard = owner.drawCard;
                owner.drawCard = 0;
                owner.kongCount++;
                owner.kanGroup.remove(i);
                CardUtil.removeOpcard(owner.opCardList, new OpCard(RuleWeight.TYPE_KAN, eventCard, eventCard, eventCard, 0));

                int[] tiGroup = new int[4];
                tiGroup[0] = eventCard;
                tiGroup[1] = eventCard;
                tiGroup[2] = eventCard;
                tiGroup[3] = eventCard;

                owner.liuGroup.add(tiGroup);

                //内毫
                owner.opCardList.add(new OpCard(RuleWeight.TYPE_TI, eventCard, eventCard, eventCard, Paixing.caclHuxi(RuleWeight.TYPE_TI, eventCard), false, RuleWeight.HAO_NUI, eventCard));

                break;
            }
        }

        for (int i = 0; i < owner.tiGroup.size(); i++) {
            if (eventCard == owner.tiGroup.get(i)[0]) {
                owner.kongCount++;
                owner.tiGroup.remove(i);
                CardUtil.removeOpcard(owner.opCardList, new OpCard(RuleWeight.TYPE_TI, eventCard, eventCard, eventCard, 0));

                int[] tiGroup = new int[4];
                tiGroup[0] = eventCard;
                tiGroup[1] = eventCard;
                tiGroup[2] = eventCard;
                tiGroup[3] = eventCard;

                owner.liuGroup.add(tiGroup);

                owner.opCardList.add(new OpCard(RuleWeight.TYPE_TI, eventCard, eventCard, eventCard, Paixing.caclHuxi(RuleWeight.TYPE_TI, eventCard), false, RuleWeight.HAO_QING, eventCard));

                break;
            }
        }

        ITArray opcard = TArray.newInstance();
        opcard.addInt(eventCard);
        opcard.addInt(eventCard);
        opcard.addInt(eventCard);
        opcard.addInt(eventCard);

        EXMainServer.gameCtr.actionCard(owner, eventCard, eventCard, RuleWeight.TYPE_TI, owner.seat, opcard);
        EXMainServer.playerRuleMgr.condition(PlayerRuleManager.HU_RULE, owner);

        owner.handleDeadPut();
    }

    @Override
    public void toNextState(EXPlayer owner) {
        owner.getRoom().isTianHu = false;
        if (owner.getRoom().card.getCount() == 0) {
            owner.getRoom().liuju = true;
            owner.getRoom().liujuNum++;
            owner.getRoom().endGame();
            return;
        }
        timer(owner);
    }

    public static void timer(EXPlayer owner) {
        TPServer.me().getTimerPool().schedule(new Runnable() {
            @Override
            public void run() {
                if (owner.room.isDestroy)
                    return;
                owner.room.enqueueRunnable(new Runnable() {
                    @Override
                    public void run() {
                        owner.stateMachine.changeState(Global.getState(PlayerWaitState.class));
                        owner.room.stateMachine.changeState(Global.getState(EXRoomSetpState.class));
                    }
                });
            }
        }, 1, TimeUnit.SECONDS);
    }
}
