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

import android.text.TextUtils;

import com.genlot.ninglottery.game.bet.BetNumber;
import com.genlot.ninglottery.game.bet.IBetBean;
import com.genlot.ninglottery.toolkit.LogTool;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author ZhuYongKang
 * @description 3D胆拖玩法
 * @date 2019/10/16
 */
public class BaseD3 implements IBetBean {

    public List<BetNumber> mDanList;
    public List<BetNumber> mTuoList;

    public BaseD3() {
        mTuoList = new ArrayList<>();
        mDanList = new ArrayList<>();
    }

    @Override
    public void clear() {
        mDanList.clear();
        mTuoList.clear();
    }

    public boolean isSameDan() {
        if (mDanList.size() == 2) {
            BetNumber betNumber1 = mDanList.get(0);
            BetNumber betNumber2 = mDanList.get(1);
            return betNumber1.getNumber().equals(betNumber2.getNumber());
        }
        return false;
    }

    public int getMinDanSize() {
        return 1;
    }

    public int getMaxDanSize() {
        return 2;
    }

    public int getMinTuoSize() {
        return 2;
    }

    public int getMaxTuoSize() {
        return 9;
    }

    public int getMinSize() {
        return 3;
    }

    public int getMaxSize() {
        return 10;
    }

    @Override
    public boolean isFull() {
        int danSize = mDanList.size();
        int tuoSize = mTuoList.size();
        return danSize > 0 && danSize <= 2 && tuoSize >= 2 && tuoSize <= 9
                && danSize + tuoSize <= 10;
    }

    @Override
    public String toBetString() {
        StringBuilder builder = new StringBuilder();
        for (BetNumber number : mDanList) {
            builder.append(number.getNumber()).append("|");
        }
        builder.append("-");
        for (BetNumber number : mTuoList) {
            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(String.valueOf(dan));
                number.setType(BetNumber.DAN);
                mDanList.add(number);
            }
            for (String tuo : numsTuo) {
                BetNumber number = new BetNumber(String.valueOf(tuo));
                number.setType(BetNumber.CHECKED);
                mTuoList.add(number);
            }
        }
    }

    @Override
    public List<BetNumber> getAllNumberList() {
        List<BetNumber> list = new ArrayList<>();
        list.addAll(mDanList);
        list.addAll(mTuoList);
        return list;
    }

    @Override
    public void random(int... args) {
        clear();
        int dan = args[0];
        int tuo = args[1];
        //胆 可以相同
        Random rand = new Random();
        while (mDanList.size() < dan) {
            int anInt = rand.nextInt(10);
            BetNumber number = new BetNumber(String.valueOf(anInt));
            number.setType(BetNumber.DAN);
            mDanList.add(number);
            Collections.sort(mDanList, (o1, o2) -> Integer.parseInt(o1.getNumber()) - Integer.parseInt(o2.getNumber()));
        }
        //拖
        Set<Integer> tuoSet = new TreeSet<>();
        while (tuoSet.size() < tuo) {
            int num = rand.nextInt(10);
            if (!contains(mDanList, String.valueOf(num))) {
                tuoSet.add(num);
            }
        }
        for (int i : tuoSet) {
            BetNumber number = new BetNumber(String.valueOf(i));
            number.setType(BetNumber.CHECKED);
            mTuoList.add(number);
        }
    }

    boolean contains(List<BetNumber> list, String num) {
        for (BetNumber betNumber : list) {
            if (betNumber.getNumber().equals(num)) {
                return true;
            }
        }
        return false;
    }

    public void add(BetNumber number) {
        BetNumber bet = new BetNumber(number.getNumber());
        bet.setType(number.getType());
        bet.setRed(number.isRed());
        if (number.getType() == BetNumber.DAN) {
            mDanList.add(bet);
            Collections.sort(mDanList, (o1, o2) -> Integer.parseInt(o1.getNumber()) - Integer.parseInt(o2.getNumber()));
        } else {
            mTuoList.add(bet);
            Collections.sort(mTuoList, (o1, o2) -> Integer.parseInt(o1.getNumber()) - Integer.parseInt(o2.getNumber()));
        }
    }

    public void remove(BetNumber number) {
        if (number.getType() == BetNumber.DAN) {
            for (BetNumber betNumber : mDanList) {
                if (betNumber.getNumber().equals(number.getNumber())) {
                    mDanList.remove(betNumber);
                    break;
                }
            }
        } else {
            for (BetNumber betNumber : mTuoList) {
                if (betNumber.getNumber().equals(number.getNumber())) {
                    mTuoList.remove(betNumber);
                    break;
                }
            }
        }
    }

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

}
