package com.example.cmake_project_test

import android.app.Dialog
import android.bluetooth.BluetoothDevice
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 * 蓝牙设备选择对话框
 */
class BluetoothDeviceDialog : DialogFragment() {
    
    private var devices: List<BluetoothDevice> = emptyList()
    private var onDeviceSelectedListener: ((BluetoothDevice) -> Unit)? = null
    
    // 设备适配器
    private inner class DeviceAdapter : RecyclerView.Adapter<DeviceAdapter.DeviceViewHolder>() {
        
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DeviceViewHolder {
            val view = LayoutInflater.from(parent.context)
                .inflate(android.R.layout.simple_list_item_1, parent, false)
            return DeviceViewHolder(view)
        }
        
        override fun onBindViewHolder(holder: DeviceViewHolder, position: Int) {
            val device = devices[position]
            val deviceName = device.name ?: "未知设备"
            val deviceAddress = device.address
            holder.textView.text = "$deviceName\n$deviceAddress"
            
            holder.itemView.setOnClickListener {
                onDeviceSelectedListener?.invoke(device)
                dismiss()
            }
        }
        
        override fun getItemCount(): Int = devices.size
        
        inner class DeviceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val textView: TextView = itemView.findViewById(android.R.id.text1)
        }
    }
    
    companion object {
        fun newInstance(devices: List<BluetoothDevice>): BluetoothDeviceDialog {
            return BluetoothDeviceDialog().apply {
                this.devices = devices
            }
        }
    }
    
    fun setOnDeviceSelectedListener(listener: (BluetoothDevice) -> Unit) {
        onDeviceSelectedListener = listener
    }
    
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = AlertDialog.Builder(requireContext())
        builder.setTitle("选择蓝牙设备 (找到 ${devices.size} 个设备)")
        
        if (devices.isEmpty()) {
            builder.setMessage("未找到蓝牙设备")
            builder.setPositiveButton("重新扫描") { _, _ ->
                dismiss()
            }
            builder.setNegativeButton("取消") { _, _ ->
                dismiss()
            }
        } else {
            // 创建RecyclerView显示所有设备
            val recyclerView = RecyclerView(requireContext()).apply {
                layoutManager = LinearLayoutManager(requireContext())
                adapter = DeviceAdapter()
                
                // 设置固定高度，避免对话框过大
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    600 // 固定高度600dp
                )
            }
            
            builder.setView(recyclerView)
            builder.setNegativeButton("取消") { _, _ ->
                dismiss()
            }
        }
        
        return builder.create()
    }
}
