package com.base.scanqrclear.ui.wifi

import android.content.Context
import android.net.wifi.WifiConfiguration
import android.view.ViewGroup
import com.base.scanqrclear.R
import com.base.scanqrclear.bean.WifiUIBean
import com.base.scanqrclear.databinding.ItemWifiBinding
import com.base.scanqrclear.ui.adapter.CommonViewHolder
import com.base.scanqrclear.utils.XmlEx.inflate
import com.chad.library.adapter4.BaseQuickAdapter

class WifiAdapter : BaseQuickAdapter<WifiUIBean, CommonViewHolder>() {

    var itemClick: ((wifi: WifiUIBean) -> Unit)? = null

    override fun onBindViewHolder(holder: CommonViewHolder, position: Int, item: WifiUIBean?) {
        item ?: return
        val binding = ItemWifiBinding.bind(holder.itemView)
        binding.tvSsid.text = item.ssid
        binding.tvDesc.text = ""
        if (item.securityTypes.contains(WifiConfiguration.SECURITY_TYPE_PSK)) {
            binding.tvDesc.text = context.getString(R.string.wpa_wpa2)
        }
        if (item.securityTypes.contains(WifiConfiguration.SECURITY_TYPE_WEP)) {
            binding.tvDesc.text = context.getString(R.string.wep)
        }
        if (item.securityTypes.contains(WifiConfiguration.SECURITY_TYPE_OPEN)) {
            binding.tvDesc.text = context.getString(R.string.open)
        }
        if (binding.tvDesc.text.equals("")) {
            binding.tvDesc.text = item.bssid
        }
        binding.root.setOnClickListener {
            itemClick?.invoke(item)
        }
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): CommonViewHolder {
        return CommonViewHolder(R.layout.item_wifi.inflate(parent))
    }
}