package com.hongyegroup.cpt_auths.widget

import android.annotation.SuppressLint
import android.app.Activity
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.hongyegroup.cpt_auths.R
import com.lxj.easyadapter.EasyAdapter
import com.lxj.easyadapter.MultiItemTypeAdapter
import com.lxj.easyadapter.ViewHolder
import com.lxj.xpopup.core.AttachPopupView
import com.lxj.xpopup.interfaces.OnSelectListener
import com.lxj.xpopup.widget.VerticalRecyclerView

/**
 * 依附下拉选-选择国家区号
 */
@SuppressLint("ViewConstructor")
class CountyCodePopup(
    activity: Activity, private val view: View, private val rvHeight: Int,
    private val list: List<String>, private val selectListener: OnSelectListener?
) : AttachPopupView(activity) {

    private lateinit var recyclerView: RecyclerView

    override fun getImplLayoutId(): Int {
        return R.layout.popup_county_code_list
    }

    override fun onCreate() {
        super.onCreate()

        recyclerView = findViewById(R.id.recyclerView)
        val layoutParams = recyclerView.layoutParams
        layoutParams.width = view.width
        layoutParams.height = rvHeight
        recyclerView.layoutParams = layoutParams

        val adapter: EasyAdapter<String> = object : EasyAdapter<String>(list, R.layout.item_popup_count_code) {
            override fun bind(holder: ViewHolder, t: String, position: Int) {
                holder.setText(R.id.tv_text, t)
            }
        }
        adapter.setOnItemClickListener(object : MultiItemTypeAdapter.SimpleOnItemClickListener() {
            override fun onItemClick(view: View, holder: RecyclerView.ViewHolder, position: Int) {

                selectListener?.onSelect(position, adapter.data[position])

                if (popupInfo.autoDismiss) {
                    dismiss()
                }
            }
        })

        recyclerView.adapter = adapter
        (recyclerView as VerticalRecyclerView).setupDivider(false)
    }

    override fun getPopupWidth(): Int {
        return view.width
    }

}