package com.genlot.ninglottery.game.k3;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.RadioButton;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.genlot.nativesdk.model.GameTicket;
import com.genlot.ninglottery.R;
import com.genlot.ninglottery.game.bet.BetManager;
import com.genlot.ninglottery.game.bet.BetNumber;
import com.genlot.ninglottery.game.bet.IBetBean;
import com.genlot.ninglottery.game.common.CommonFragment;
import com.genlot.ninglottery.game.common.NumberAdapter;
import com.genlot.ninglottery.game.common.SingleView;
import com.genlot.ninglottery.game.k3.bean.Diff2Number;
import com.genlot.ninglottery.game.k3.bean.Diff3Number;
import com.genlot.ninglottery.game.k3.bean.Multi2Same;
import com.genlot.ninglottery.game.k3.bean.Select3Same;
import com.genlot.ninglottery.game.k3.bean.Single2Same;
import com.genlot.ninglottery.game.k3.bean.Single3Same;
import com.genlot.ninglottery.game.k3.bean.SingleK3;
import com.genlot.ninglottery.game.k3.bean.SumK3;
import com.genlot.ninglottery.game.k3.betview.GeneralView;
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 com.genlot.ninglottery.widget.KeyboardEditText;
import com.genlot.ninglottery.widget.decoration.GridItemDecoration;

import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;
import butterknife.OnCheckedChanged;

/**
 * @author ZhuYongKang
 * @description 快3游戏界面
 * @date 2019/8/22
 */
public class K3Fragment extends CommonFragment {

    private List<BetNumber> mList1, mList2, mList3;
    private NumberAdapter mAdapter1, mAdapter2, mAdapter3;

    @OnCheckedChanged({R.id.radio1, R.id.radio2, R.id.radio3, R.id.radio4, R.id.radio5,
            R.id.radio6, R.id.radio7, R.id.radio8, R.id.radio9})
    public void playTypeChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            frameBet.removeAllViews();
            switch (buttonView.getId()) {
                case R.id.radio1:
                    mPlayType = PlayTypeID.K3_SINGLE;
                    showMultiNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, SingleK3.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio2:
                    mPlayType = PlayTypeID.K3_SUM;
                    showSumNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, SumK3.class);
                    break;
                case R.id.radio3:
                    mPlayType = PlayTypeID.K3_3NUMBER_ALL;
                    showNoNumber();
                    btRandom.setVisibility(View.GONE);
                    btRandom5.setVisibility(View.GONE);
                    mBetView = new GeneralView(activity, Select3Same.class);
                    break;
                case R.id.radio4:
                    mPlayType = PlayTypeID.K3_3NUMBER_SINGLE;
                    showSingleNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, Single3Same.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio5:
                    mPlayType = PlayTypeID.K3_2NUMBER_MULTI;
                    showMultiNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, Multi2Same.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio6:
                    mPlayType = PlayTypeID.K3_2NUMBER_SINGLE;
                    showSingleNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, Single2Same.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio7:
                    mPlayType = PlayTypeID.K3_3NUMBER_DIFF;
                    showSingleNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, Diff3Number.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio8:
                    mPlayType = PlayTypeID.K3_2NUMBER_DIFF;
                    showMultiNumber();
                    btRandom.setVisibility(View.VISIBLE);
                    btRandom5.setVisibility(View.VISIBLE);
                    mBetView = new SingleView(activity, Diff2Number.class);
                    ((SingleView) mBetView).changeBetSize(3, 90, 10);
                    break;
                case R.id.radio9:
                    mPlayType = PlayTypeID.K3_3NUMBER_SERIAL;
                    showNoNumber();
                    btRandom.setVisibility(View.GONE);
                    btRandom5.setVisibility(View.GONE);
                    mBetView = new GeneralView(activity, Select3Same.class);
                    break;
                default:
                    break;
            }
            BetManager.getInstance().setPlayType(mPlayType);
            frameBet.addView(mBetView);
            clearUI();
            if (isRandom) {
                clearRandom();
            }
        }
    }

    @Override
    public void changePlayType(int playTypeId) {
        switch (playTypeId) {
            case PlayTypeID.K3_SINGLE:
                radio1.setChecked(true);
                break;
            case PlayTypeID.K3_SUM:
                radio2.setChecked(true);
                break;
            case PlayTypeID.K3_3NUMBER_ALL:
                radio3.setChecked(true);
                break;
            case PlayTypeID.K3_3NUMBER_SINGLE:
                radio4.setChecked(true);
                break;
            case PlayTypeID.K3_2NUMBER_MULTI:
                radio5.setChecked(true);
                break;
            case PlayTypeID.K3_2NUMBER_SINGLE:
                radio6.setChecked(true);
                break;
            case PlayTypeID.K3_3NUMBER_DIFF:
                radio7.setChecked(true);
                break;
            case PlayTypeID.K3_2NUMBER_DIFF:
                radio8.setChecked(true);
                break;
            case PlayTypeID.K3_3NUMBER_SERIAL:
                radio9.setChecked(true);
                break;
            default:
                break;
        }
    }

    private void showMultiNumber() {
        rvNumber2.setVisibility(View.VISIBLE);
        rvNumber3.setVisibility(View.VISIBLE);
        mList1.clear();
        mList2.clear();
        mList3.clear();
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList1.add(number);
        }
        mList1.add(new BetNumber("*"));
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList2.add(number);
        }
        mList2.add(new BetNumber("*"));
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList3.add(number);
        }
        mList3.add(new BetNumber("*"));
        mAdapter1.notifyDataSetChanged();
        mAdapter2.notifyDataSetChanged();
        mAdapter3.notifyDataSetChanged();
    }

    private void showSingleNumber() {
        rvNumber2.setVisibility(View.VISIBLE);
        rvNumber3.setVisibility(View.VISIBLE);
        mList1.clear();
        mList2.clear();
        mList3.clear();
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList1.add(number);
        }
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList2.add(number);
        }
        for (int i = 1; i <= 6; i++) {
            BetNumber number = new BetNumber(String.valueOf(i));
            mList3.add(number);
        }
        mAdapter1.notifyDataSetChanged();
        mAdapter2.notifyDataSetChanged();
        mAdapter3.notifyDataSetChanged();
    }

    private void showSumNumber() {
        rvNumber2.setVisibility(View.GONE);
        rvNumber3.setVisibility(View.GONE);
        mList1.clear();
        for (int i = 4; i <= 17; i++) {
            BetNumber number = new BetNumber(StringTool.format2(i));
            mList1.add(number);
        }
        mAdapter1.notifyDataSetChanged();
    }

    /**
     * 三同号 三联号
     */
    private void showNoNumber() {
        rvNumber2.setVisibility(View.GONE);
        rvNumber3.setVisibility(View.GONE);
        mList1.clear();
        mAdapter1.notifyDataSetChanged();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_k3;
    }

    @Override
    public void initData() {
        mList1 = new ArrayList<>();
        mList2 = new ArrayList<>();
        mList3 = new ArrayList<>();
    }

    @Override
    public void initAdapter() {
        rvNumber1.setLayoutManager(new GridLayoutManager(activity, 8));
        rvNumber1.addItemDecoration(new GridItemDecoration(20, 10));
        rvNumber2.setLayoutManager(new GridLayoutManager(activity, 8));
        rvNumber2.addItemDecoration(new GridItemDecoration(20, 10));
        rvNumber3.setLayoutManager(new GridLayoutManager(activity, 8));
        rvNumber3.addItemDecoration(new GridItemDecoration(20, 10));
        mAdapter1 = new NumberAdapter(R.layout.item_number, mList1);
        mAdapter2 = new NumberAdapter(R.layout.item_number, mList2);
        mAdapter3 = new NumberAdapter(R.layout.item_number, mList3);
        rvNumber1.setAdapter(mAdapter1);
        rvNumber2.setAdapter(mAdapter2);
        rvNumber3.setAdapter(mAdapter3);
        rvNumber1.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                BetNumber betNumber = mList1.get(position);
                onNumber1Click(betNumber);
            }
        });
        rvNumber2.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                BetNumber betNumber = mList2.get(position);
                onNumber2Click(betNumber);
            }
        });
        rvNumber3.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                BetNumber betNumber = mList3.get(position);
                onNumber3Click(betNumber);
            }
        });
    }

    private void singleClick(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            if ("*".equals(number.getNumber()) && bean.hasStar()) {
                showTips("该玩法最多有一个*");
                return;
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number, index);
        }else {
            bean.remove(number, index);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void single3Same(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            number.setType(BetNumber.CHECKED);
            BetNumber number2 = mAdapter2.getData().get(Integer.parseInt(number.getNumber()) - 1);
            BetNumber number3 = mAdapter3.getData().get(Integer.parseInt(number.getNumber()) - 1);
            number2.setType(BetNumber.CHECKED);
            number3.setType(BetNumber.CHECKED);
            bean.add(number, 0);
            bean.add(number, 1);
            bean.add(number, 2);
        }else {
            bean.remove(number, 0);
            bean.remove(number, 1);
            bean.remove(number, 2);
            number.setType(BetNumber.NORMAL);
            BetNumber number2 = mAdapter2.getData().get(Integer.parseInt(number.getNumber()) - 1);
            BetNumber number3 = mAdapter3.getData().get(Integer.parseInt(number.getNumber()) - 1);
            number2.setType(BetNumber.NORMAL);
            number3.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
        mAdapter1.notifyDataSetChanged();
        mAdapter2.notifyDataSetChanged();
        mAdapter3.notifyDataSetChanged();
    }

    private void multi2Same(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            if ("*".equals(number.getNumber()) && bean.hasStar()) {
                showTips("该玩法最多有一个*");
                return;
            }
            if (!"*".equals(number.getNumber()) && bean.getAllNumberList().size() == 1 && !bean.hasStar()
                    && (!number.getNumber().equals(bean.getAllNumberList().get(0).getNumber()))) {
                showTips("该玩法俩个号码必须相同");
                return;
            }
            if (bean.getAllNumberList().size() == 2) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                String num2 = bean.getAllNumberList().get(1).getNumber();
                if (!bean.hasStar() && !number.getNumber().equals("*")) {
                    showTips("该玩法必须有一个*");
                    return;
                }
                if (("*".equals(num1) && !num2.equals(number.getNumber())) ||
                        ("*".equals(num2) && !num1.equals(number.getNumber()))) {
                    showTips("该玩法俩个号码必须相同");
                    return;
                }
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number, index);
        }else {
            bean.remove(number, index);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void single2Same(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            if (bean.getAllNumberList().size() == 2) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                String num2 = bean.getAllNumberList().get(1).getNumber();
                if (num1.equals(num2)) {
                    if (number.getNumber().equals(num1)) {
                        showTips("该玩法必须俩个号码相同");
                        return;
                    }
                } else {
                    if (!num1.equals(number.getNumber()) && !num2.equals(number.getNumber())) {
                        showTips("该玩法必须俩个号码相同");
                        return;
                    }
                }
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number, index);
        }else {
            bean.remove(number, index);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void diff3Number(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            if (bean.getAllNumberList().size() == 1) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                if (num1.equals(number.getNumber())) {
                    showTips("该玩法三个号码不能相同");
                    return;
                }
            }
            if (bean.getAllNumberList().size() == 2) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                String num2 = bean.getAllNumberList().get(1).getNumber();
                if (num1.equals(number.getNumber()) || num2.equals(number.getNumber())) {
                    showTips("该玩法三个号码不能相同");
                    return;
                }
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number, index);
        }else {
            bean.remove(number, index);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void diff2Number(BetNumber number, int index) {
        int type = number.getType();
        SingleK3 bean = (SingleK3) ((SingleView) mBetView).getCurrentBet();
        if (type == BetNumber.NORMAL) {
            if (index == 0 && bean.mList1.size() == 1) {
                return;
            }
            if (index == 1 && bean.mList2.size() == 1) {
                return;
            }
            if (index == 2 && bean.mList3.size() == 1) {
                return;
            }
            if (bean.getAllNumberList().size() == 1) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                if (num1.equals(number.getNumber())) {
                    showTips("该玩法两个号码不能相同");
                    return;
                }
            }
            if (bean.getAllNumberList().size() == 2) {
                String num1 = bean.getAllNumberList().get(0).getNumber();
                String num2 = bean.getAllNumberList().get(1).getNumber();
                if (bean.hasStar()) {
                    if(("*".equals(num1) && num2.equals(number.getNumber())) ||
                            ("*".equals(num2) && num1.equals(number.getNumber()))){
                        showTips("该玩法两个号码不能相同");
                        return;
                    }
                    if ("*".equals(number.getNumber())) {
                        showTips("该玩法只能有一个*");
                        return;
                    }
                }else {
                    if (!number.getNumber().equals("*")) {
                        showTips("该玩法必须有一个*");
                        return;
                    }
                }
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number, index);
        }else {
            bean.remove(number, index);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void sumClick(BetNumber number) {
        SumK3 bean = (SumK3) ((SingleView) mBetView).getCurrentBet();
        if (number.getType() == BetNumber.NORMAL) {
            if (bean.isFull()) {
                return;
            }
            number.setType(BetNumber.CHECKED);
            bean.add(number);
        } else {
            bean.remove(number);
            number.setType(BetNumber.NORMAL);
        }
        ((SingleView) mBetView).refreshRow(mBetView.mCurrentRow);
        if (bean.isFull() && mBetView.mCurrentRow < 4) {
            mBetView.rowChange(mBetView.mCurrentRow + 1);
            refreshAdapter(((SingleView) mBetView).getCurrentBet());
        }
    }

    private void onNumber1Click(BetNumber number) {
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
                singleClick(number, 0);
                break;
            case PlayTypeID.K3_SUM:
                sumClick(number);
                break;
            case PlayTypeID.K3_3NUMBER_SINGLE:
                single3Same(number, 0);
                break;
            case PlayTypeID.K3_2NUMBER_MULTI:
                multi2Same(number, 0);
                break;
            case PlayTypeID.K3_2NUMBER_SINGLE:
                single2Same(number, 0);
                break;
            case PlayTypeID.K3_3NUMBER_DIFF:
                diff3Number(number, 0);
                break;
            case PlayTypeID.K3_2NUMBER_DIFF:
                diff2Number(number, 0);
                break;
            default:break;
        }
        mAdapter1.notifyDataSetChanged();
        refreshMoney();
    }

    private void onNumber2Click(BetNumber number) {
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
                singleClick(number, 1);
                break;
            case PlayTypeID.K3_3NUMBER_SINGLE:
                single3Same(number, 1);
                break;
            case PlayTypeID.K3_2NUMBER_MULTI:
                multi2Same(number, 1);
                break;
            case PlayTypeID.K3_2NUMBER_SINGLE:
                single2Same(number, 1);
                break;
            case PlayTypeID.K3_3NUMBER_DIFF:
                diff3Number(number, 1);
                break;
            case PlayTypeID.K3_2NUMBER_DIFF:
                diff2Number(number, 1);
                break;
            default:break;
        }
        mAdapter2.notifyDataSetChanged();
        refreshMoney();
    }

    private void onNumber3Click(BetNumber number) {
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
                singleClick(number, 2);
                break;
            case PlayTypeID.K3_3NUMBER_SINGLE:
                single3Same(number, 2);
                break;
            case PlayTypeID.K3_2NUMBER_MULTI:
                multi2Same(number, 2);
                break;
            case PlayTypeID.K3_2NUMBER_SINGLE:
                single2Same(number, 2);
                break;
            case PlayTypeID.K3_3NUMBER_DIFF:
                diff3Number(number, 2);
                break;
            case PlayTypeID.K3_2NUMBER_DIFF:
                diff2Number(number, 2);
                break;
            default:break;
        }
        mAdapter3.notifyDataSetChanged();
        refreshMoney();
    }

    @Override
    public void initViews() {
        if (!isFromCopy) {
            radio1.setChecked(true);
        }
        etIssues.setOnTextFinishListener(value -> {
            if (TextUtils.isEmpty(value)) {
                etIssues.setText("1");
            }
            etIssues.clearFocus();
            int issues = Integer.parseInt(etIssues.getText().toString().trim());
            if (issues == 0) {
                ToastUtils.showShort("期数最小为1");
                etIssues.setText("1");
            }
            if (issues > 99) {
                ToastUtils.showShort("最大期数为99");
                etIssues.setText("99");
            }
            BetManager.getInstance().setIssues(Integer.parseInt(etIssues.getText().toString().trim()));
            refreshMoney();
        });
    }

    /**
     * 清空界面
     */
    @Override
    public void clearUI(){
        tvMulti.setText("1");
        BetManager.getInstance().setTimes(1);
        etIssues.setText("1");
        BetManager.getInstance().setIssues(1);
        clearAdapter();
        if (mBetView != null) {
            mBetView.clear();
        }
        refreshMoney();
    }

    @Override
    public void onCopySuccess(GameTicket ticket) {
        LogTool.logD("onCopySuccess");
        isFromCopy = true;
        changePlayType(GameUtil.getPlayType(ticket.mPlayType, ticket.mBets));
        mBetView.parseBetData(ticket.gameBetDataString);
        refreshAdapter(mBetView.mBetList.get(0));
        tvMulti.setText(String.valueOf(ticket.mMultiTimes[0]));
        BetManager.getInstance().setTimes(ticket.mMultiTimes[0]);
        etIssues.setText(String.valueOf(ticket.mMultiIssue));
        BetManager.getInstance().setIssues(ticket.mMultiIssue);
        refreshMoney();
        EventBus.getDefault().removeStickyEvent(ticket);
    }

    @Override
    public int getMaxTimes() {
        return K3Config.MAX_TIMES;
    }

    @Override
    public int getOneBetMoney() {
        return K3Config.MONEY_OF_BET;
    }

    @Override
    public long getValidBets() {
        long num = 0;
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
            case PlayTypeID.K3_SUM:
            case PlayTypeID.K3_3NUMBER_SINGLE:
            case PlayTypeID.K3_2NUMBER_MULTI:
            case PlayTypeID.K3_2NUMBER_SINGLE:
            case PlayTypeID.K3_3NUMBER_DIFF:
            case PlayTypeID.K3_2NUMBER_DIFF:
            case PlayTypeID.K3_3NUMBER_ALL:
            case PlayTypeID.K3_3NUMBER_SERIAL:
                for (IBetBean bean : mBetView.mBetList) {
                    if (bean.isFull()) {
                        num++;
                    }
                }
                break;
        }
        return num;
    }

    @Override
    public void refreshAdapter(IBetBean betBean) {
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
            case PlayTypeID.K3_3NUMBER_SINGLE:
            case PlayTypeID.K3_2NUMBER_MULTI:
            case PlayTypeID.K3_2NUMBER_SINGLE:
            case PlayTypeID.K3_3NUMBER_DIFF:
            case PlayTypeID.K3_2NUMBER_DIFF:
                mAdapter1.changeK3Bet(((SingleK3) betBean).mList1, 1);
                mAdapter2.changeK3Bet(((SingleK3) betBean).mList2, 1);
                mAdapter3.changeK3Bet(((SingleK3) betBean).mList3, 1);
                break;
            case PlayTypeID.K3_SUM:
                mAdapter1.changeK3Bet(((SumK3) betBean).mList, 4);
                break;
        }
    }

    @Override
    public void clearAdapter() {
        mAdapter1.clear();
        mAdapter2.clear();
        mAdapter3.clear();
    }

    @Override
    public void random() {
        switch (mPlayType) {
            case PlayTypeID.K3_SINGLE:
            case PlayTypeID.K3_SUM:
            case PlayTypeID.K3_3NUMBER_SINGLE:
            case PlayTypeID.K3_2NUMBER_MULTI:
            case PlayTypeID.K3_2NUMBER_SINGLE:
            case PlayTypeID.K3_3NUMBER_DIFF:
            case PlayTypeID.K3_2NUMBER_DIFF:
                if (isRandom) {
                    randomNum(mRandomNum);
                } else {
                    showSingleRandomDialog();
                }
                break;
        }
    }

    @BindView(R.id.rv_number1)
    RecyclerView rvNumber1;
    @BindView(R.id.rv_number2)
    RecyclerView rvNumber2;
    @BindView(R.id.rv_number3)
    RecyclerView rvNumber3;
    @BindView(R.id.radio1)
    RadioButton radio1;
    @BindView(R.id.radio2)
    RadioButton radio2;
    @BindView(R.id.radio3)
    RadioButton radio3;
    @BindView(R.id.radio4)
    RadioButton radio4;
    @BindView(R.id.radio5)
    RadioButton radio5;
    @BindView(R.id.radio6)
    RadioButton radio6;
    @BindView(R.id.radio7)
    RadioButton radio7;
    @BindView(R.id.radio8)
    RadioButton radio8;
    @BindView(R.id.radio9)
    RadioButton radio9;
    @BindView(R.id.et_issues)
    KeyboardEditText etIssues;
}
