package com.dahe.gldriver.mypop

import android.content.Context
import android.graphics.Color
import android.widget.Button
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter4.BaseQuickAdapter
import com.dahe.gldriver.R
import com.dahe.gldriver.adapter.SelectCarAdapter
import com.dahe.gldriver.bean.CarBean
import com.dahe.mylibrary.adapter.GridImageAdapter
import com.dahe.mylibrary.callback.OnPicResultListener
import com.dahe.mylibrary.recycleviewswipe.RecycleViewDivider
import com.dahe.mylibrary.utils.ConvertUtils
import com.luck.picture.lib.entity.LocalMedia
import com.lxj.xpopup.core.BottomPopupView


/**
 * @ClassName PopBottomPic
 * @Author john
 * @Date 2024/1/31 11:11
 * @Description TODO
 */
class PopBottomCar(
    context: Context,
    cars: MutableList<CarBean>,
    onCarSelectListener: OnCarSelectListener
) : BottomPopupView(context) {

    private var cars: MutableList<CarBean>
    private var listener: OnCarSelectListener
    private var currCar: CarBean? = null

    init {
        this.listener = onCarSelectListener
        this.cars = cars
    }


    override fun getImplLayoutId() = R.layout.choice_car

    override fun onCreate() {
        super.onCreate()
        var recycler = findViewById<RecyclerView>(R.id.recycler)
        var btnOk = findViewById<Button>(R.id.btnOk)
        initRecycle(recycler)

        btnOk.setOnClickListener {
            if (currCar == null) {
                dismiss()
                return@setOnClickListener
            }

            if (listener != null) {
                listener.onResult(currCar!!)
                dismiss()
            }
        }

    }

    private fun initRecycle(recycler: RecyclerView) {
        recycler.run {
            layoutManager = LinearLayoutManager(context, RecyclerView.VERTICAL, false)
            setHasFixedSize(true)
            addItemDecoration(
                RecycleViewDivider(
                    LinearLayout.VERTICAL,
                    ConvertUtils.dp2px(10.0f),
                    Color.TRANSPARENT
                )
            )
            adapter = SelectCarAdapter()
            (adapter as SelectCarAdapter).submitList(cars)
            (adapter as SelectCarAdapter)
        }.run {
            setOnItemClickListener() { adapter, view, position ->
                currCar = items[position]
                items.find { it.isSelect }?.isSelect = false
                items[position].isSelect = true
                notifyDataSetChanged()
            }
        }
    }

}

fun interface OnCarSelectListener {
    fun onResult(result: CarBean)
}