package extend.zp.player.rulestate;

import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.game.Global;
import com.game.player.state.PlayerPauseState;
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.player.state.EXPlayerDiscardState;
import extend.zp.player.state.EXPlayerDrawTipState;
import extend.zp.room.state.EXRoomSetpState;
import extend.zp.uitl.CardUtil;
import extend.zp.uitl.Paixing;


public class PRWeiLiuState extends StateBase<EXPlayer> {
    Logger logger = Logger.getLogger(getClass());
    @Override
    public void enter(EXPlayer owner) {
        owner.kongCount++;
        owner.isDiHu = false;
        int eventCard = owner.drawCard;//jefe wincard

        logger.info("jefe weiliustate :" + eventCard);
        for (int i = 0; i < owner.weiGroup.size(); i++) {
            if (eventCard == owner.weiGroup.get(i)[0] && CardUtil.cardNum(eventCard, owner.cardInhand) == 1) {
           // if ( CardUtil.cardNum(eventCard, owner.cardInhand) == 1) {
                owner.weiGroup.remove(i);
                CardUtil.removeOpcard(owner.opCardList, new OpCard(RuleWeight.TYPE_WEI, eventCard, eventCard, eventCard, 0));
                CardUtil.removeCard(owner.cardInhand, eventCard, 1);

                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;
            }else {

                for (int j = 0; j < owner.cardInhand.size(); j++) {
                    if (owner.cardInhand.get(j) == owner.weiGroup.get(i)[0]) {
                        eventCard = owner.cardInhand.get(j);
                        owner.weiGroup.remove(i);
                        CardUtil.removeOpcard(owner.opCardList, new OpCard(RuleWeight.TYPE_WEI, eventCard, eventCard, eventCard, 0));
                        CardUtil.removeCard(owner.cardInhand, eventCard, 1);

                        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;
                    }
                }
            }
        }

        ITArray opcard = TArray.newInstance();
        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.isTingHu = 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);
    }

    @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

    }
}
