// lib/widgets/cast/backends/google_cast_backend.dart
//
// Backend Google Cast moderno (CAF)
// - Nessuna logica slideshow interna
// - Nessun timer Dart
// - Nessun playerStateNotifier
// - Tutto delegato a GoogleCastController (CAF)

import 'package:aves/model/entry/entry.dart';
import '../cast_controller.dart';
import '../google_cast_controller.dart';

class GoogleCastBackend extends CastBackendBase {
  GoogleCastBackend(super.controller) {
    _bindGoogleNotifiers();
  }

  final google = GoogleCastController.instance;

  // ------------------------------------------------------------
  // SYNC GOOGLE → CastController
  // ------------------------------------------------------------
  void _bindGoogleNotifiers() {
    google.castIconState.addListener(_sync);
    google.isCastingNotifier.addListener(_sync);
    google.currentIndexNotifier.addListener(_sync);
    google.isPlayingNotifier.addListener(_sync);
    google.isBufferingNotifier.addListener(_sync);
    google.positionNotifier.addListener(_sync);
    google.durationNotifier.addListener(_sync);
    google.volumeNotifier.addListener(_sync);
    google.isMutedNotifier.addListener(_sync);

    _sync();
  }

  void _sync() {
    controller.castIconState.value = google.castIconState.value;
    controller.isCastingNotifier.value = google.isCastingNotifier.value;
    controller.currentIndexNotifier.value = google.currentIndexNotifier.value;
    controller.isPlayingNotifier.value = google.isPlayingNotifier.value;
    controller.isBufferingNotifier.value = google.isBufferingNotifier.value;
    controller.positionNotifier.value = google.positionNotifier.value;
    controller.durationNotifier.value = google.durationNotifier.value;
    controller.volumeNotifier.value = google.volumeNotifier.value;
    controller.isMutedNotifier.value = google.isMutedNotifier.value;
  }

  // ------------------------------------------------------------
  // START CAST (CAF queue)
  // ------------------------------------------------------------
  @override
  Future<void> startCast(List<AvesEntry> entries, int index) async {
    controller.castIconState.value = CastIconState.connecting;
    controller.isCastingNotifier.value = true;

    await google.startManualQueue(
      entries: entries,
      startIndex: index,
    );

    controller.castIconState.value = CastIconState.connected;
  }

  @override
  Future<void> startFullscreen(List<AvesEntry> entries, int index) =>
      startCast(entries, index);

  // ------------------------------------------------------------
  // SLIDESHOW (CAF)
  // ------------------------------------------------------------
  @override
  Future<void> startSlideshow(List<AvesEntry> entries, int index) async {
    controller.castIconState.value = CastIconState.connecting;
    controller.isCastingNotifier.value = true;

    await google.startSlideshow(
      entries: entries,
      startIndex: index,
    );

    controller.castIconState.value = CastIconState.connected;
  }

  // ------------------------------------------------------------
  // QUEUE MANUALE (CAF)
  // ------------------------------------------------------------
  @override
  Future<void> startManualQueue(List<AvesEntry> entries, int index) async {
    controller.castIconState.value = CastIconState.connecting;
    controller.isCastingNotifier.value = true;

    await google.startManualQueue(
      entries: entries,
      startIndex: index,
    );

    controller.castIconState.value = CastIconState.connected;
  }

  // ------------------------------------------------------------
  // NEXT / PREV
  // ------------------------------------------------------------
  @override
  Future<void> nextManual() => google.nextManual();

  @override
  Future<void> prevManual() => google.prevManual();

  // ------------------------------------------------------------
  // STOP
  // ------------------------------------------------------------
  @override
  Future<void> stopCast() async {
    await google.stopGoogleCast();
    controller.castIconState.value = CastIconState.idle;
    controller.isCastingNotifier.value = false;
  }

  // ------------------------------------------------------------
  // CAF gestisce tutto → nessun timer Dart
  // ------------------------------------------------------------
  @override
  Duration? getNextDelay(AvesEntry entry) => null;
}
