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

import android.text.TextUtils;

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

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author ZhuYongKang
 * @description K3单式投注实体类
 * @date 2019/8/26
 */
public class SingleK3 implements IBetBean {

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

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

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

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

    public int getPlayTypeID() {
        String num1 = mList1.get(0).getNumber();
        String num2 = mList2.get(0).getNumber();
        String num3 = mList3.get(0).getNumber();
        if (num1.equals(num2) && num1.equals(num3)) {
            //三同号单选
            return PlayTypeID.K3_3NUMBER_SINGLE;
        } else if (num1.equals(num2) || num1.equals(num3) || num2.equals(num3)) {
            if (hasStar()) {
                //俩同号复选
                return PlayTypeID.K3_2NUMBER_MULTI;
            } else {
                //二同号单选
                return PlayTypeID.K3_2NUMBER_SINGLE;
            }
        } else {
            if (hasStar()) {
                //二不同号
                return PlayTypeID.K3_2NUMBER_DIFF;
            } else {
                //三不同号
                return PlayTypeID.K3_3NUMBER_DIFF;
            }
        }
    }

    public boolean hasStar() {
        for (BetNumber num : getAllNumberList()) {
            if ("*".equals(num.getNumber())) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String toBetString() {
        StringBuilder builder = new StringBuilder();
        for (BetNumber number : mList1) {
            builder.append(number.getNumber().equals("*") ? "255" : number.getNumber())
                    .append("|");
        }
        for (BetNumber number : mList2) {
            builder.append(number.getNumber().equals("*")  ? "255" : number.getNumber())
                    .append("|");
        }
        for (BetNumber number : mList3) {
            builder.append(number.getNumber().equals("*")  ? "255" : number.getNumber())
                    .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].equals("255") ? "*" : nums[0]);
            number1.setType(BetNumber.CHECKED);
            mList1.add(number1);
            BetNumber number2 = new BetNumber(nums[1].equals("255") ? "*" : nums[1]);
            number2.setType(BetNumber.CHECKED);
            mList2.add(number2);
            BetNumber number3 = new BetNumber(nums[2].equals("255") ? "*" : nums[2]);
            number3.setType(BetNumber.CHECKED);
            mList3.add(number3);
        }
    }

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

    @Override
    public void random(int... args) {
        clear();
        //第一位
        Random rand = new Random();
        int num1 = rand.nextInt(7) + 1;
        BetNumber number1 = new BetNumber(num1 == 7 ? "*" : String.valueOf(num1));
        number1.setType(BetNumber.CHECKED);
        mList1.add(number1);
        if (num1 == 7) {
            //第二位
            int num2 = rand.nextInt(6) + 1;
            BetNumber number2 = new BetNumber(String.valueOf(num2));
            number2.setType(BetNumber.CHECKED);
            mList2.add(number2);
            //第三位
            int num3 = rand.nextInt(6) + 1;
            BetNumber number3 = new BetNumber(String.valueOf(num3));
            number3.setType(BetNumber.CHECKED);
            mList3.add(number3);
        } else {
            //第二位
            int num2 = rand.nextInt(7) + 1;
            BetNumber number2 = new BetNumber(num2 == 7 ? "*" : String.valueOf(num2));
            number2.setType(BetNumber.CHECKED);
            mList2.add(number2);
            if (num2 == 7) {
                //第三位
                int num3 = rand.nextInt(6) + 1;
                BetNumber number3 = new BetNumber(String.valueOf(num3));
                number3.setType(BetNumber.CHECKED);
                mList3.add(number3);
            } else {
                //第三位
                int num3 = rand.nextInt(7) + 1;
                BetNumber number3 = new BetNumber(num3 == 7 ? "*" : String.valueOf(num3));
                number3.setType(BetNumber.CHECKED);
                mList3.add(number3);
            }
        }

    }

    public void add(BetNumber number, int index) {
        BetNumber bet = new BetNumber(number.getNumber());
        bet.setType(number.getType());
        bet.setRed(number.isRed());
        if (index == 0) {
            mList1.add(bet);
        } else if (index == 1) {
            mList2.add(bet);
        } else if (index == 2) {
            mList3.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;
                }
            }
        } else if (index == 2) {
            for (BetNumber bet : mList3) {
                if (bet.getNumber().equals(number.getNumber())) {
                    mList3.remove(bet);
                    break;
                }
            }
        }
    }

}
