package extend.mj.player.state;

import com.game.Global;
import com.game.state.StateBase;

import com.taurus.core.entity.ITObject;
import extend.mj.*;
import extend.mj.uitl.WinCard;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * ���ץ��״̬
 *
 *
 * 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;
        }
        Global.logger.info("EXPlayerKongDrawState");
        ITObject config = room.config;

        boolean hz_hu = config.getBoolean(Config.ROOM_CONFIG_HZ_HU);
        int laizi = 0;

        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);
                    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);
                    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) {
        owner.stateMachine.changeState(Global.getState(EXPlayerDiscardState.class));
    }
}
