package com.xyzshell.myphoneinfo.dialog

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.compose.material3.Text
import com.xyzshell.andinfo.AndInfo
import com.xyzshell.myphoneinfo.R
import com.xyzshell.myphoneinfo.base.BaseDialogFragment
import com.xyzshell.myphoneinfo.databinding.DialogBlueToothBinding

class DialogBlueTooth :BaseDialogFragment<DialogBlueToothBinding>(DialogBlueToothBinding::inflate){

    override fun getTitle(): String  = resources.getString(R.string.bluetooth)

    override fun getIconRes(): Int=5

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val bluetoothInfo = AndInfo.instance.bluetooth
        val  list=ArrayList<Map<String, String>>()
        bluetoothInfo.bondedDevices.forEach { (name, address) ->
            val deviceMap = mapOf(
                "name" to (name ?: "Unknown device"),
                "address" to (address ?: "")
            )
            list.add(deviceMap)
        }
        val container = binding.view as? ViewGroup

        container?.let {
            // 遍历 list，为每个设备创建 item view
            if(list.isEmpty()){
                it.addView(TextView(requireContext()).apply {
                    text = "No devices"
                })
            }else{
                list.forEach { deviceMap ->
                    // 加载 item 布局
                    val itemView = LayoutInflater.from(requireContext())
                        .inflate(R.layout.item_blue_tooth, it, false)

                    // 设置设备信息
                    val tvName = itemView.findViewById<TextView>(R.id.textTitle)
                    val tvAddress = itemView.findViewById<TextView>(R.id.textContent)

                    tvName.text = deviceMap["name"]
                    tvAddress.text = deviceMap["address"]
                    // 添加到容器
                    it.addView(itemView)
                }
            }

        }

    }

    override fun onNegativeClick() {
        super.onNegativeClick()

    }

    override fun onPositiveClick() {
        super.onPositiveClick()
    }

}