import 'dart:async';

import 'package:aves/model/settings/settings.dart';
import 'package:aves/services/common/services.dart';
import 'package:aves/theme/colors.dart';
import 'package:aves/theme/icons.dart';
import 'package:aves/widgets/common/extensions/build_context.dart';
import 'package:aves/widgets/settings/common/tile_leading.dart';
import 'package:aves/widgets/settings/common/tiles.dart';
import 'package:aves/widgets/settings/settings_definition.dart';
import 'package:aves/widgets/settings/viewer/entry_background.dart';
import 'package:aves/widgets/settings/viewer/overlay.dart';
import 'package:aves/widgets/settings/viewer/slideshow.dart';
import 'package:aves/widgets/settings/viewer/viewer_actions_editor.dart';
import 'package:aves_model/aves_model.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

// ✅ NEW: enum per la scelta sorgente dei contenuti locali durante il cast
import 'package:aves/model/settings/enums/local_cast_source_mode.dart';

class ViewerSection extends SettingsSection {
  @override
  String get key => 'viewer';

  @override
  Widget icon(BuildContext context) => SettingsTileLeading(
        icon: AIcons.image,
        color: context.select<AvesColorsData, Color>((v) => v.image),
      );

  @override
  String title(BuildContext context) => context.l10n.settingsViewerSectionTitle;

  @override
  Future<List<SettingsTile>> tiles(BuildContext context) async {
    final isCutoutAware = await windowService.isCutoutAware();
    return [
      if (!settings.useTvLayout) SettingsTileViewerQuickActions(),
      SettingsTileViewerOverlay(),
      SettingsTileViewerSlideshow(),

      // ✅ NEW: impostazione solo per contenuti locali (Cast)
      if (!settings.useTvLayout) SettingsTileViewerLocalCastSourceMode(),

      if (!settings.useTvLayout) SettingsTileViewerGestureSideTapNext(),
      if (!settings.useTvLayout && isCutoutAware) SettingsTileViewerUseCutout(),
      SettingsTileViewerMotionPhotoAutoPlay(),
      SettingsTileViewerImageBackground(),
    ];
  }
}

class SettingsTileViewerQuickActions extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsViewerQuickActionsTile;

  @override
  Widget build(BuildContext context) => SettingsSubPageTile(
        title: title(context),
        routeName: ViewerActionEditorPage.routeName,
        builder: (context) => const ViewerActionEditorPage(),
      );
}

class SettingsTileViewerOverlay extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsViewerOverlayTile;

  @override
  Widget build(BuildContext context) => SettingsSubPageTile(
        title: title(context),
        routeName: ViewerOverlayPage.routeName,
        builder: (context) => const ViewerOverlayPage(),
      );
}

class SettingsTileViewerSlideshow extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsViewerSlideshowTile;

  @override
  Widget build(BuildContext context) => SettingsSubPageTile(
        title: title(context),
        routeName: ViewerSlideshowPage.routeName,
        builder: (context) => const ViewerSlideshowPage(),
      );
}

// ✅ NEW TILE: scelta sorgente per i contenuti locali quando si fa Cast
class SettingsTileViewerLocalCastSourceMode extends SettingsTile {
  @override
  String title(BuildContext context) => 'Casting (solo contenuti locali)';

  String _modeLabel(LocalCastSourceMode v) {
    switch (v) {
      case LocalCastSourceMode.automatic:
        return 'Automatico';
      case LocalCastSourceMode.phone:
        return 'Sempre Telefono (LAN)';
      case LocalCastSourceMode.server:
        return 'Sempre Server (Cloud)';
    }
  }

  String _modeSubtitle(LocalCastSourceMode v) {
    switch (v) {
      case LocalCastSourceMode.automatic:
        return 'Se il server è raggiungibile usa il server; altrimenti usa il telefono (LAN).';
      case LocalCastSourceMode.phone:
        return 'I file locali vengono serviti dal telefono tramite Wi‑Fi (funziona anche senza Internet).';
      case LocalCastSourceMode.server:
        return 'I file locali vengono serviti dal server remoto (richiede server raggiungibile).';
    }
  }

  @override
  Widget build(BuildContext context) => Selector<Settings, LocalCastSourceMode>(
        selector: (context, s) => s.localCastSourceMode,
        builder: (context, mode, child) => ListTile(
          title: Text(title(context)),
          subtitle: Text(_modeSubtitle(mode)),
          trailing: DropdownButton<LocalCastSourceMode>(
            value: mode,
            onChanged: (v) {
              if (v != null) settings.localCastSourceMode = v;
            },
            items: LocalCastSourceMode.values
                .map(
                  (v) => DropdownMenuItem<LocalCastSourceMode>(
                    value: v,
                    child: Text(_modeLabel(v)),
                  ),
                )
                .toList(),
          ),
        ),
      );
}

class SettingsTileViewerGestureSideTapNext extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsViewerGestureSideTapNext;

  @override
  Widget build(BuildContext context) => SettingsSwitchListTile(
        selector: (context, s) => s.viewerGestureSideTapNext,
        onChanged: (v) => settings.viewerGestureSideTapNext = v,
        title: title(context),
      );
}

class SettingsTileViewerUseCutout extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsViewerUseCutout;

  @override
  Widget build(BuildContext context) => SettingsSwitchListTile(
        selector: (context, s) => s.viewerUseCutout,
        onChanged: (v) => settings.viewerUseCutout = v,
        title: title(context),
      );
}

class SettingsTileViewerMotionPhotoAutoPlay extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsMotionPhotoAutoPlay;

  @override
  Widget build(BuildContext context) => SettingsSwitchListTile(
        selector: (context, s) => s.enableMotionPhotoAutoPlay,
        onChanged: (v) => settings.enableMotionPhotoAutoPlay = v,
        title: title(context),
      );
}

class SettingsTileViewerImageBackground extends SettingsTile {
  @override
  String title(BuildContext context) => context.l10n.settingsImageBackground;

  @override
  Widget build(BuildContext context) => Selector<Settings, EntryBackground>(
        selector: (context, s) => s.imageBackground,
        builder: (context, current, child) => ListTile(
          title: Text(title(context)),
          trailing: EntryBackgroundSelector(
            getter: () => current,
            setter: (value) => settings.imageBackground = value,
          ),
        ),
      );
}

