package com.base.scanqr.ui.widget

import android.app.AlertDialog
import android.content.Context
import android.view.Gravity
import android.view.LayoutInflater
import com.base.scanqr.R
import com.base.scanqr.bean.WifiUIBean
import com.base.scanqr.databinding.DialogWifiSelectorBinding
import com.base.scanqr.ui.wifi.WifiAdapter

object WifiSelectDialog {

    fun Context.showWifiSelectDialog(wifiList: List<WifiUIBean>, selectWifi: (wifi: WifiUIBean) -> Unit) {

        val dialog = AlertDialog.Builder(this).create()
        val binding = DialogWifiSelectorBinding.inflate(LayoutInflater.from(this))
        dialog.setView(binding.root)
        dialog.setCanceledOnTouchOutside(true)

        dialog.show()

        val params = dialog.window?.attributes
//        params?.width = resources.getDimensionPixelOffset(R.dimen.dp_320)
        params?.height = resources.getDimensionPixelOffset(R.dimen.dp_400)
        params?.gravity = Gravity.CENTER
//        params?.y = 50

        dialog.window?.attributes = params
        dialog.window?.setBackgroundDrawableResource(android.R.color.transparent)

        val adapter = WifiAdapter()
        adapter.itemClick = {
            dialog.dismiss()
            selectWifi.invoke(it)
        }
        binding.rv.adapter = adapter
        adapter.submitList(wifiList)
    }
}