package com.zj365.health.weight

import android.content.Context
import android.view.Gravity
import android.view.View
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
import com.xty.base.dialog.BaseDialog
import com.zj365.health.databinding.DialogEatTypeBinding
import com.xty.network.model.EatTypeBean
import com.xty.network.model.Nation
import kotlinx.coroutines.selects.select

class EatTypeDialog(
    context: Context,
    private var title: String = "选择用餐类型",
    private var leftBtnText: String? = "取消",
    private var rightBtnText: String? = "确定",
    var confirm: (selectValue: String, selectKey: String) -> Unit
) : BaseDialog(context) {

    private val binding by lazy { DialogEatTypeBinding.inflate(layoutInflater) }
    override fun setLayout(): View = binding.root

    private var wheelViewList: MutableList<String> = mutableListOf()

    private var selectValue: String = ""
    private var selectKey: String = ""

    override fun initView() {
        super.initView()
        window?.setGravity(Gravity.BOTTOM)
        binding.tvLeftBtn.text = leftBtnText
        binding.tvRightBtn.text = rightBtnText
        binding.tvTitleText.text = title
        binding.tvRightBtn.setOnClickListener {
            confirm.invoke(selectValue, selectKey)
            dismiss()
        }
        binding.tvLeftBtn.setOnClickListener {
            dismiss()
        }
    }

    fun setWheelViewData(listData: MutableList<EatTypeBean>) {
        if (listData.size <= 0) {
            return
        }
        selectValue = listData[0].value
        selectKey = listData[0].key
        if (wheelViewList.size > 0) {
            wheelViewList.clear()
        }
        listData.forEach { data ->
            wheelViewList.add(data.key)
        }
        binding.wheelView.apply {
            setCyclic(false)
            adapter = ArrayWheelAdapter(wheelViewList)
            setOnItemSelectedListener {
                selectValue = listData[it].value
                selectKey = listData[it].key
            }
        }
    }

    fun setNationWheelViewData(listData: MutableList<Nation>,selectData:String?) {
        if (listData.size <= 0) {
            return
        }
        selectValue = listData[0].dictTypeId
        selectKey = listData[0].dictName
        if (wheelViewList.size > 0) {
            wheelViewList.clear()
        }
        listData.forEach { data ->
            wheelViewList.add(data.dictName)
        }
        binding.wheelView.apply {
            setCyclic(false)
            adapter = ArrayWheelAdapter(wheelViewList)
            setOnItemSelectedListener {
                selectValue = listData[it].dictTypeId
                selectKey = listData[it].dictName
            }
            currentItem = if(!selectData.isNullOrEmpty()){
                wheelViewList.indexOf(selectData)
            }else{
                0
            }
        }
    }

    fun setGravity(gravity: Int) {
        window?.setGravity(gravity)
    }
}