package extend.mj.player.state;

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.ITObject;
import com.taurus.permanent.TPServer;
import extend.mj.*;
import extend.mj.room.state.EXRoomSetpState;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * ���ץ��״̬
 *
 *
 * 2017��8��30��
 * EXPlayerDrawState.java
 */
public class EXPlayerKongDrawState extends StateBase<EXPlayer> {

    @Override
    public void enter(EXPlayer owner) {
        EXRoom room = owner.getRoom();
        if (room.isLiuJu()) {
            room.endGameByLiuJu();
            return;
        }

        ITObject config = room.config;

        boolean hz_hu = config.getBoolean(Config.ROOM_CONFIG_HZ_HU);
        int laizi = room.config.getInt(Config.ROOM_CONFIG_LAIZI);

        if(owner.seat == room.adminSeat) {
            double rand = Math.random() % 100;
            if (room.while_list && !owner.is_white && rand > room.white_value)
            {
                ArrayList<Integer> tempCardList = new ArrayList<Integer>();
                int count = 0;
                do {
                    int drawCard = owner.drawCard = room.card.popsub();
                    HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
                    WinCard win = new WinCard(owner.cardInhand, drawCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, true, true);
                    if(!(hz_hu && win.laizi_count >0)) {
                        if (win.checkWin(map, owner, room, true)) {
                            if (room.card.cardList.size() != 0 && count++ <= 5) {
                                tempCardList.add(drawCard);
                                continue;
                            }
                        }
                    }

                    owner.drawCard = drawCard;
                    break;
                } while(true);

                room.card.cardList.addAll(tempCardList);
            }
            else
            {
                owner.drawCard = room.card.popsub();
            }
        }else {
            double rand = Math.random() % 100 * 100;
            if (room.while_list && !owner.is_white && rand > room.white_value)
            {
                ArrayList<Integer> tempCardList = new ArrayList<Integer>();
                int count = 0;
                do {
                    int drawCard = room.card.pop();
                    HashMap<Integer, WinCardType> map = new HashMap<Integer, WinCardType>();
                    WinCard win = new WinCard(owner.cardInhand, drawCard, room.card.laiziCard, room.card.laiziCard2, laizi > 0 ? true : false, true, true);
                    if(!(hz_hu && win.laizi_count >0)) {
                        if (win.checkWin(map, owner, room, true)) {
                            if (room.card.cardList.size() != 0 && count++ <= 5) {
                                tempCardList.add(drawCard);
                                Global.logger.info("no white to hu:" + drawCard);
                                continue;
                            }
                        }
                    }

                    owner.drawCard = drawCard;
                    break;
                } while(true);
                room.card.cardList.addAll(tempCardList);
            }
            else
            {
                owner.drawCard = room.card.pop();
            }
        }

        int leftCount = room.card.getCount();
        ((EXGameController)Global.gameCtr).getCard(owner, leftCount);

        EXMainServer.playerRuleMgr.condition(PlayerRuleManager.KONG_DRAW_RULE, owner);
    }

    @Override
    public void toNextState(EXPlayer owner) {
        if (owner.is_gang)
        {
            timer(owner);
        }
        else {
            owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
        }
    }

    public static void timer(EXPlayer owner) {

        TPServer.me().getTimerPool().schedule(new Runnable() {

            @Override
            public void run() {

                if (owner == null || owner.getRoom() == null) {
                    Global.logger.error("room is null");
                    return;
                }

                if (owner.getRoom().isDestroy)
                    return;

                owner.getRoom().enqueueRunnable(new Runnable() {

                    @Override
                    public void run() {
                        owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
                    }
                });
            }
        }, 1000, TimeUnit.MILLISECONDS);
    }
}
