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

import android.text.TextUtils;

import com.genlot.ninglottery.game.bet.BetNumber;
import com.genlot.ninglottery.model.PlayTypeID;
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 色球胆拖投注实体类
 * @date 2019/8/29
 */
public class BaseSSQ extends BetSSQ {

    public int getMinDan() {
        return 1;
    }

    public int getMaxDan() {
        return 5;
    }

    public int getMinTuo() {
        return 2;
    }

    public int getMaxTuo() {
        return 30;
    }

    public int getMinRed() {
        return 7;
    }

    public int getMaxRed() {
        return 33;
    }

    public int getMinBlue() {
        return 1;
    }

    public int getMaxBlue() {
        return 16;
    }

    /**
     * 根据蓝球数确定游戏ID
     * @return  游戏ID
     */
    public int getPlayTypeID() {
        return mBlueList.size() == 1 ? PlayTypeID.SSQ_BASE : PlayTypeID.SSQ_BASE_MULTI;
    }

    @Override
    public boolean isFull() {
        return mDanList.size() >= getMinDan() && mRedList.size() >= getMinTuo() &&
                (mDanList.size() + mRedList.size()) >= getMinRed() &&
                mBlueList.size() >= getMinBlue();
    }

    /**
     * 1|2|3|4|5|-6|7|8|-15|16|
     * @return  String
     */
    @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("-");
        for (BetNumber number : mBlueList) {
            builder.append(number.getNumber()).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("\\|");
            String[] numsBlue = nums[2].split("\\|");
            for (String dan : numsDan) {
                BetNumber number = new BetNumber(StringTool.format2(Integer.parseInt(dan)));
                number.setType(BetNumber.DAN);
                mDanList.add(number);
            }
            for (String red : numsTuo) {
                BetNumber number = new BetNumber(StringTool.format2(Integer.parseInt(red)));
                number.setType(BetNumber.CHECKED);
                mRedList.add(number);
            }
            for (String blue : numsBlue) {
                BetNumber number = new BetNumber(StringTool.format2(Integer.parseInt(blue)));
                number.setType(BetNumber.CHECKED);
                number.setRed(false);
                mBlueList.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;
                }
            }
        }
    }

    @Override
    public void random(int... args) {
        int dan = args[0];
        int tuo = args[1];
        int blue = args[2];
        clear();
        Set<Integer> redSet = GameUtil.randomList(1, 33, 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);
        }
        //蓝球
        Set<Integer> blueSet = GameUtil.randomList(1, 16, blue);
        for (int i : blueSet) {
            BetNumber number = new BetNumber(StringTool.format2(i));
            number.setType(BetNumber.CHECKED);
            number.setRed(false);
            mBlueList.add(number);
        }
    }

}
