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

class FastCastBackend extends CastBackendBase {
  FastCastBackend(super.controller);

  static const _channel = MethodChannel('google_cast');

  // FAST non usa timer Dart → tutto gestito da CAF
  @override
  Duration? getNextDelay(AvesEntry entry) => null;

  // ------------------------------------------------------------
  // START CAST (slideshow veloce)
  // ------------------------------------------------------------
  @override
  Future<void> startCast(List<AvesEntry> entries, int index) =>
      startSlideshow(entries, index);

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

    final payload = _buildQueuePayload(
      entries: entries,
      startIndex: index,
      mode: "slideshow",
    );

    await _channel.invokeMethod("fast.loadQueue", payload);

    controller.castIconState.value = CastIconState.connected;
  }

  // ------------------------------------------------------------
  // FULLSCREEN = slideshow veloce
  // ------------------------------------------------------------
  @override
  Future<void> startFullscreen(List<AvesEntry> entries, int index) =>
      startSlideshow(entries, index);

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

    final payload = _buildQueuePayload(
      entries: entries,
      startIndex: index,
      mode: "manual",
    );

    await _channel.invokeMethod("fast.loadQueue", payload);

    controller.castIconState.value = CastIconState.connected;
  }

  // ------------------------------------------------------------
  // NEXT / PREV
  // ------------------------------------------------------------
  @override
  Future<void> nextManual() =>
      _channel.invokeMethod("fast.next");

  @override
  Future<void> prevManual() =>
      _channel.invokeMethod("fast.prev");

  // ------------------------------------------------------------
  // STOP
  // ------------------------------------------------------------
  @override
  Future<void> stopCast() async {
    await _channel.invokeMethod("fast.stop");
    controller.castIconState.value = CastIconState.idle;
    controller.isCastingNotifier.value = false;
  }

  // ------------------------------------------------------------
  // COSTRUZIONE PAYLOAD PER CAF
  // ------------------------------------------------------------
  Map<String, dynamic> _buildQueuePayload({
    required List<AvesEntry> entries,
    required int startIndex,
    required String mode, // "slideshow" / "manual"
  }) {
    return {
      "mode": mode,
      "startIndex": startIndex,
      "items": entries.map((e) {
        final isVideo = e.mimeType?.startsWith('video') ?? false;

        return {
          "id": e.id.toString(),
          "url": e.uri, // corretto per AVES
          "isVideo": isVideo,
          "title": e.bestTitle ?? "",
        };
      }).toList(),
    };
  }
}
