import 'package:flutter/material.dart';
import 'dlna_manager.dart';
import 'package:aves/widgets/cast/dlna.dart';   // ⭐ IMPORT NECESSARIO

class CastDialogDlna extends StatefulWidget {
  const CastDialogDlna({super.key});

  @override
  State<CastDialogDlna> createState() => _CastDialogDlnaState();
}

class _CastDialogDlnaState extends State<CastDialogDlna> {
  final manager = DLNAManager.instance;
  List<DLNADevice> devices = [];
  bool loading = true;

  @override
  void initState() {
    super.initState();
    _discover();
  }

  Future<void> _discover() async {
    final found = await manager.discover();
    setState(() {
      devices = found;
      loading = false;
    });
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text("Dispositivi DLNA"),
      content: SizedBox(
        width: 300,
        height: 300,
        child: loading
            ? const Center(child: CircularProgressIndicator())
            : devices.isEmpty
                ? const Center(child: Text("Nessun dispositivo trovato"))
                : ListView.builder(
                    itemCount: devices.length,
                    itemBuilder: (_, i) {
                      final d = devices[i];
                      return ListTile(
                        title: Text(d.info.friendlyName),
                        subtitle: Text(d.avTransportUrl.host),
                        onTap: () => Navigator.pop(context, d),
                      );
                    },
                  ),
      ),
    );
  }
}
