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.GameUtil;
import com.genlot.ninglottery.toolkit.LogTool;

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

/**
 * @author ZhuYongKang
 * @description 猜2D
 * @date 2019/10/14
 */
public class Guess2D implements IBetBean {

    public List<BetNumber> mList1;
    public List<BetNumber> mList2;

    public Guess2D() {
        mList1 = new ArrayList<>();
        mList2 = new ArrayList<>();
    }

    @Override
    public void clear() {
        mList1.clear();
        mList2.clear();
    }

    @Override
    public boolean isFull() {
        return mList1.size() == 1 && mList2.size() == 1;
    }

    @Override
    public String toBetString() {
        StringBuilder builder = new StringBuilder();
        for (BetNumber number : mList1) {
            builder.append(number.getNumber());
        }
        builder.append("|");
        for (BetNumber number : mList2) {
            builder.append(number.getNumber());
        }
        builder.append("|");
        return builder.toString();
    }

    @Override
    public void parseBetData(String betData) {
        if (!TextUtils.isEmpty(betData)) {
            LogTool.logD("betData = " + betData);
            String[] nums = betData.split("\\|");
            BetNumber number1 = new BetNumber(nums[0]);
            number1.setType(BetNumber.CHECKED);
            mList1.add(number1);
            BetNumber number2 = new BetNumber(nums[1]);
            number2.setType(BetNumber.CHECKED);
            mList2.add(number2);
        }
    }

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

    @Override
    public void random(int... args) {
        clear();
        //第一位
        Set<Integer> set1 = GameUtil.randomList(0, 9, 1);
        for (int i : set1) {
            BetNumber number = new BetNumber(String.valueOf(i));
            number.setType(BetNumber.CHECKED);
            mList1.add(number);
        }
        //第二位
        Set<Integer> set2 = GameUtil.randomList(0, 9, 1);
        for (int i : set2) {
            BetNumber number = new BetNumber(String.valueOf(i));
            number.setType(BetNumber.CHECKED);
            mList2.add(number);
        }
    }

    public void add(BetNumber number, int index) {
        BetNumber bet = new BetNumber(number.getNumber());
        bet.setType(number.getType());
        bet.setRed(number.isRed());
        if (index == 0) {
            if (mList1.size() == 0) {
                mList1.add(bet);
            }
        } else if (index == 1) {
            if (mList2.size() == 0) {
                mList2.add(bet);
            }
        }
    }

    public void remove(BetNumber number, int index) {
        if (index == 0) {
            for (BetNumber bet : mList1) {
                if (bet.getNumber().equals(number.getNumber())) {
                    mList1.remove(bet);
                    break;
                }
            }
        } else if (index == 1) {
            for (BetNumber bet : mList2) {
                if (bet.getNumber().equals(number.getNumber()) ) {
                    mList2.remove(bet);
                    break;
                }
            }
        }
    }
}
