package com.unis.sxzcl.widget;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.unis.sxzcl.databinding.DialogBottomMultipleBinding;
import com.unis.sxzcl.databinding.ListEmptyLayoutBinding;
import com.xyxsbj.mylibrary.tool.ETTool;

import java.util.List;

/**
 * 包名: com.unis.icon.widget
 * 类名: BottomMultipleListDialog
 * 作用描述: 多级选择对话框
 * 创建人: mQ
 * 创建时间: 2020/11/13 10:01
 * 更新人:
 * 更新时间: 2020/11/13 10:01
 * 更新说明:
 * 版本号: 1.0
 */
public class BottomMultipleListDialog {

    private Context mContext;
    private OneCallback mOneCallback;
    private TwoCallback mTwoCallback;
    private ThreeCallback mThreeCallback;
    private EnterCallback mEnterCallback;
    private BottomSheetDialog mBottomDialog;
    private DialogBottomMultipleBinding mBinding;
    private BottomMultipleAdapter mAdapter1, mAdapter2, mAdapter3;

    private View mView;

    public BottomMultipleListDialog(Context context) {
        mContext = context;
        init();
    }

    /**
     * 初始化控件
     */
    private void init() {
        mBottomDialog = new BottomSheetDialog(mContext);
        mBinding = DialogBottomMultipleBinding.inflate(mBottomDialog.getLayoutInflater());
        LinearLayout root = mBinding.getRoot();
        mBottomDialog.setContentView(root);
        try {
            //设置背景颜色为透明
            ViewGroup parent = (ViewGroup) root.getParent();
            parent.setBackgroundResource(android.R.color.transparent);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //初始化列表

        mAdapter1 = new BottomMultipleAdapter(bean -> {
            //点击事件
            if (mOneCallback != null) {
                boolean hideDialog = mOneCallback.onOneClickListener(mView, bean);
                if (hideDialog) mBottomDialog.dismiss();
            }
        });
        mAdapter1.setEmptyView(ListEmptyLayoutBinding.inflate(LayoutInflater.from(mContext)).getRoot());
        mBinding.recyclerView1.setAdapter(mAdapter1);
        mBinding.recyclerView1.setLayoutManager(new LinearLayoutManager(mContext));


        mAdapter2 = new BottomMultipleAdapter(bean -> {
            //点击事件
            if (mTwoCallback != null) {
                boolean hideDialog = mTwoCallback.onTwoClickListener(mView,
                        mAdapter1.getSelBean(),
                        bean);
                if (hideDialog) mBottomDialog.dismiss();
            }
        });
        mAdapter2.setEmptyView(ListEmptyLayoutBinding.inflate(LayoutInflater.from(mContext)).getRoot());
        mBinding.recyclerView2.setAdapter(mAdapter2);
        mBinding.recyclerView2.setLayoutManager(new LinearLayoutManager(mContext));


        mAdapter3 = new BottomMultipleAdapter(bean -> {
            //点击事件
            if (mThreeCallback != null) {
                boolean hideDialog = mThreeCallback.onThreeClickListener(mView,
                        mAdapter1.getSelBean(),
                        mAdapter2.getSelBean(),
                        bean);
                if (hideDialog) mBottomDialog.dismiss();
            }
        });
        mAdapter3.setEmptyView(ListEmptyLayoutBinding.inflate(LayoutInflater.from(mContext)).getRoot());
        mBinding.recyclerView3.setAdapter(mAdapter3);
        mBinding.recyclerView3.setLayoutManager(new LinearLayoutManager(mContext));

        mBinding.btnEnter.setOnClickListener(v -> {
            //点击事件
            if (mEnterCallback != null) {
                boolean hideDialog = mEnterCallback.onEnterListener(mView,
                        mAdapter1.getSelBean(),
                        mAdapter2.getSelBean(),
                        mAdapter3.getSelBean());
                if (hideDialog) mBottomDialog.dismiss();
            }
        });
    }

    public BottomMultipleListDialog setView(View view) {
        mView = view;
        return this;
    }

    public BottomMultipleListDialog setOneCallback(OneCallback oneCallback) {
        mOneCallback = oneCallback;
        return this;
    }

    public BottomMultipleListDialog setTwoCallback(TwoCallback twoCallback) {
        mTwoCallback = twoCallback;
        return this;
    }

    public BottomMultipleListDialog setThreeCallback(ThreeCallback threeCallback) {
        mThreeCallback = threeCallback;
        return this;
    }

    public BottomMultipleListDialog setEnterCallback(EnterCallback enterCallback) {
        mEnterCallback = enterCallback;
        return this;
    }

    /**
     * 设置回调监听
     */


    public BottomMultipleListDialog setTitle(String title) {
        mBinding.tvTitle.setText(title);
        return this;
    }

    public String getTitle() {
        return ETTool.getData(mBinding.tvTitle);
    }

    /**
     * 设置第一列的数据
     *
     * @param list
     */
    public BottomMultipleListDialog setListOne(List<MultipleBean> list) {
        mAdapter1.setList(list);
        mAdapter1.setSelIndex(-1);
        return this;
    }

    /**
     * 设置第二列的数据
     *
     * @param list
     */
    public BottomMultipleListDialog setListTwo(List<MultipleBean> list) {
        mAdapter2.setList(list);
        mAdapter2.setSelIndex(-1);
        return this;
    }

    /**
     * 设置第三列的数据
     *
     * @param list
     */
    public BottomMultipleListDialog setListThree(List<MultipleBean> list) {
        mAdapter3.setList(list);
        mAdapter3.setSelIndex(-1);
        return this;
    }

    /**
     * 设置第一列选中项
     *
     * @param index
     */
    public BottomMultipleListDialog setSelectOne(int index) {
        mAdapter1.setSelIndex(index);
        return this;
    }

    public int getOneSelect() {
        return mAdapter1.getSelIndex();
    }

    /**
     * 设置第二列选中项
     *
     * @param index
     */
    public BottomMultipleListDialog setSelectTwo(int index) {
        mAdapter2.setSelIndex(index);
        return this;
    }

    public int getTwoSelect() {
        return mAdapter2.getSelIndex();
    }

    /**
     * 设置第三列选中项
     *
     * @param index
     */
    public BottomMultipleListDialog setSelectThree(int index) {
        mAdapter3.setSelIndex(index);
        return this;
    }

    public int getThreeSelect() {
        return mAdapter3.getSelIndex();
    }

    public BottomMultipleListDialog hideTwoList() {
        mBinding.recyclerView2.setVisibility(View.GONE);
        return this;
    }

    public BottomMultipleListDialog hideThreeList() {
        mBinding.recyclerView3.setVisibility(View.GONE);
        return this;
    }

    public BottomMultipleListDialog hideRightEnter() {
        mBinding.btnEnter.setVisibility(View.GONE);
        return this;
    }

    /**
     * 显示对话框
     */
    public void show() {
        mBottomDialog.show();
    }

    /**
     * 隐藏对话框
     */
    public void dismiss() {
        mBottomDialog.dismiss();
    }


    public interface OneCallback {
        /**
         * 第一列点击事件
         *
         * @param b1 第一列选择项
         * @return 是否隐藏dialog
         */
        boolean onOneClickListener(View v, MultipleBean b1);
    }

    public interface TwoCallback {
        /**
         * 第二列点击事件
         *
         * @param b1 第一列选择项
         * @param b2 第二列选择项
         * @return 是否隐藏dialog
         */
        boolean onTwoClickListener(View v, MultipleBean b1, MultipleBean b2);
    }

    public interface ThreeCallback {
        /**
         * 第三列点击事件
         *
         * @param b1 第一列选择项
         * @param b2 第二列选择项
         * @param b3 第三列选择项
         * @return 是否隐藏dialog
         */
        boolean onThreeClickListener(View v, MultipleBean b1, MultipleBean b2, MultipleBean b3);
    }

    public interface EnterCallback {
        /**
         * 确定按钮点击事件
         *
         * @param b1 第一列选择项
         * @param b2 第二列选择项
         * @param b3 第三列选择项
         * @return 是否隐藏dialog
         */
        boolean onEnterListener(View v, MultipleBean b1, MultipleBean b2, MultipleBean b3);
    }

}
