package com.xty.common.picker

import android.content.Context
import android.graphics.Color
import android.view.View
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.view.OptionsPickerView
import com.contrarywind.interfaces.IPickerViewData
import com.xty.common.R


/**
 * 多级联动选择器
 *
 * 可支持三级联动
 *
 */
class CustomPickerSelect(
    var context: Context,
    var select: () -> Unit
) {

    private var index:Int = 0
    private var mView: View? = null
    private var list1: MutableList<PickerViewData>? = null
    private var list2: MutableList<MutableList<PickerViewData>>? = null
    private var list3: MutableList<MutableList<MutableList<PickerViewData>>>? = null

    //选择器
    private var pickerView
            : OptionsPickerView<PickerViewData>? = null

    init {
        pickerView = OptionsPickerBuilder(
            context
        ) { options1, options2, options3, v -> //返回的分别是三个级别的选中位置
            var strBuilder = StringBuilder()
            var tagBuilder = StringBuilder()
            if (!list1.isNullOrEmpty()) {
                strBuilder.append(list1!![options1].name)
                tagBuilder.append(options1)
            }
            if (!list2.isNullOrEmpty()) {
                strBuilder.append(list2!![options1][options2].name)
                tagBuilder.append(",$options2")
            }
            if (!list3.isNullOrEmpty()) {
                strBuilder.append(list3!![options1][options2][options3].name)
                tagBuilder.append(",$options3")
            }
            if (null != mView) {
                if (mView is TextView) {
                    (mView as TextView).text = strBuilder.toString()
                    (mView as TextView).tag = tagBuilder.toString()
                }
            }

            select.invoke()
        }
            .setCancelText(context!!.getString(R.string.cancel))//取消按钮文字
            .setSubmitText(context!!.getString(R.string.confirm))//确认按钮文字
            .setTitleSize(18)//标题文字大小
            .setContentTextSize(15) //设置文字大小
            .setTitleText("")//标题文字
            .setOutSideCancelable(true)//点击屏幕，点在控件外部范围时，是否取消显示
            .setTitleColor(ContextCompat.getColor(context!!, R.color.col_455)!!)//标题文字颜色
            .setSubmitColor(ContextCompat.getColor(context!!, R.color.start_col))!!//确定按钮文字颜色
            .setCancelColor(ContextCompat.getColor(context!!, R.color.col_c64)!!)//取消按钮文字颜色
            .setTitleBgColor(Color.WHITE)//标题背景颜色 Night mode
            .setBgColor(Color.WHITE)//滚轮背景颜色 Night mode
            .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
            .isDialog(false)//是否显示为对话框样式
            .setSelectOptions(0) //设置选择的值
            .build()
    }

    fun showView(view: View?, list1: MutableList<PickerViewData>? = null){
        show(0, view, list1)
    }

    fun show(
        index: Int,
        list1: MutableList<PickerViewData>? = null,
        list2: MutableList<MutableList<PickerViewData>>? = null,
        list3: MutableList<MutableList<MutableList<PickerViewData>>>? = null
    ) {
        show(index, null, list1, list2, list3)
    }

    fun show(
        index: Int,
        view: View?,
        list1: MutableList<PickerViewData>? = null,
        list2: MutableList<MutableList<PickerViewData>>? = null,
        list3: MutableList<MutableList<MutableList<PickerViewData>>>? = null
    ) {
        this.mView = view
        this.index = index
        if (list1 != null) {
            this.list1 = list1
        }
        if (list2 != null) {
            this.list2 = list2
        }
        if (list3 != null) {
            this.list3 = list3
        }
        pickerView?.setPicker(
            this.list1, this.list2, this.list3
        )
        pickerView?.show()
    }

    //用于显示PickerView显示
    data class PickerViewData(
        val name: String,
    ) : IPickerViewData {
        override fun getPickerViewText(): String = name
    }

}