package com.genlot.ninglottery.game.qlc.bean;

import android.text.TextUtils;

import com.genlot.ninglottery.game.bet.BetNumber;
import com.genlot.ninglottery.game.qlc.QlcConfig;
import com.genlot.ninglottery.toolkit.GameUtil;
import com.genlot.ninglottery.toolkit.LogTool;
import com.genlot.ninglottery.toolkit.StringTool;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author ZhuYongKang
 * @description 7乐彩胆拖
 * @date 2019/9/11
 */
public class Base7lc extends Bet7lc {
    @Override
    public boolean isFull() {
        return mRedList.size() >= QlcConfig.BASEROUND_TUO_MIN &&
                mDanList.size() >= QlcConfig.BASEROUND_DAN_MIN &&
                (mDanList.size() + mRedList.size()) >= QlcConfig.BASEROUND_RED_MIN;
    }

    /**
     * @return  1|2|3|4|5|-6|7|8|-
     */
    @Override
    public String toBetString() {
        StringBuilder builder = new StringBuilder();
        for (BetNumber number : mDanList) {
            builder.append(number.getNumber()).append("|");
        }
        builder.append("-");
        for (BetNumber number : mRedList) {
            builder.append(number.getNumber()).append("|");
        }
        builder.append("-");
        return builder.toString();
    }


    @Override
    public void parseBetData(String betData) {
        if (!TextUtils.isEmpty(betData)) {
            LogTool.logD("betData = " + betData);
            String[] nums = betData.split("-");
            String[] numsDan = nums[0].split("\\|");
            String[] numsTuo = nums[1].split("\\|");
            for (String dan : numsDan) {
                BetNumber number = new BetNumber(StringTool.format2(Integer.parseInt(dan)));
                number.setType(BetNumber.DAN);
                mDanList.add(number);
            }
            for (String tuo : numsTuo) {
                BetNumber number = new BetNumber(StringTool.format2(Integer.parseInt(tuo)));
                number.setType(BetNumber.CHECKED);
                mRedList.add(number);
            }
        }
    }

    @Override
    public void random(int... args) {
        clear();
        int dan = args[0];
        int tuo = args[1];
        Set<Integer> redSet = GameUtil.randomList(1, 30, dan + tuo);
        List<Integer> tuoList = new ArrayList<>(redSet);
        //胆
        Set<Integer> danSet = GameUtil.randomSplitList(tuoList, dan);
        for (int i : danSet) {
            BetNumber number = new BetNumber(StringTool.format2(i));
            number.setType(BetNumber.DAN);
            mDanList.add(number);
        }
        //拖
        for (int i : tuoList) {
            BetNumber number = new BetNumber(StringTool.format2(i));
            number.setType(BetNumber.CHECKED);
            mRedList.add(number);
        }
    }

    /**
     * 胆拖状态改变
     * @param number  BetNumber
     */
    public void change(BetNumber number) {
        add(number);
        if (number.getType() == BetNumber.DAN) {
            for (BetNumber betNumber : mRedList) {
                if (betNumber.getNumber().equals(number.getNumber())) {
                    mRedList.remove(betNumber);
                    break;
                }
            }
        } else {
            for (BetNumber betNumber : mDanList) {
                if (betNumber.getNumber().equals(number.getNumber())) {
                    mDanList.remove(betNumber);
                    break;
                }
            }
        }
    }

}
