// lib/widgets/viewer/fullscreen_cast_viewer_dlna.dart
//
// Viewer FULLSCREEN per DLNA (solo locale).
// Il backend DLNA gestisce:
// - invio entry
// - next/prev
// - stop

import 'package:flutter/material.dart';
import 'package:aves/model/entry/entry.dart';

import 'package:aves/widgets/viewer/controls/controller.dart';
import 'package:aves/widgets/viewer/entry_viewer_page_dlna.dart';

import 'package:aves/widgets/cast/cast_controller.dart';

class FullscreenCastViewerDlna extends StatefulWidget {
  final List<AvesEntry> entries;
  final int startIndex;

  const FullscreenCastViewerDlna({
    super.key,
    required this.entries,
    required this.startIndex,
  });

  @override
  State<FullscreenCastViewerDlna> createState() =>
      _FullscreenCastViewerDlnaState();
}

class _FullscreenCastViewerDlnaState extends State<FullscreenCastViewerDlna> {
  late int _currentIndex;
  late ViewerController _viewerController;

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

    _currentIndex = widget.startIndex;
    _viewerController = ViewerController();

    // ⭐ invia la prima entry tramite backend DLNA
    CastController.instance.startManualQueue(widget.entries, _currentIndex);
  }

  @override
  Widget build(BuildContext context) {
    final controller = CastController.instance;

    return Stack(
      children: [
        // ------------------------------------------------------------
        // VIEWER DLNA (solo locale)
        // ------------------------------------------------------------
        EntryViewerPageDlna(
          collection: null,
          initialEntry: widget.entries[_currentIndex],
          viewerController: _viewerController,

          // ⭐ quando cambia entry → notifica il backend DLNA
          onEntryChanged: (entry) {
            final newIndex = widget.entries.indexOf(entry);
            if (newIndex != -1) {
              setState(() => _currentIndex = newIndex);
              controller.startManualQueue(widget.entries, newIndex);
            }
          },
        ),

        // ------------------------------------------------------------
        // OVERLAY CONTROLLI DLNA
        // ------------------------------------------------------------
        Positioned(
          bottom: 20,
          left: 0,
          right: 0,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              // PREV
              IconButton(
                iconSize: 40,
                color: Colors.white,
                icon: const Icon(Icons.skip_previous),
                onPressed: () async {
                  await controller.prevManual();
                },
              ),

              const SizedBox(width: 20),

              // STOP
              IconButton(
                iconSize: 40,
                color: Colors.white,
                icon: const Icon(Icons.stop),
                onPressed: () async {
                  await controller.stopCast();
                  if (mounted) Navigator.of(context).maybePop();
                },
              ),

              const SizedBox(width: 20),

              // NEXT
              IconButton(
                iconSize: 40,
                color: Colors.white,
                icon: const Icon(Icons.skip_next),
                onPressed: () async {
                  await controller.nextManual();
                },
              ),
            ],
          ),
        ),
      ],
    );
  }
}
