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

/// Funzioni di editing sugli entry.
/// Versione ultra‑ottimizzata, senza album, senza grouping, senza filter_grids.
/// Tutta logica pura, nessun dialog.
class EntryActionEdit {
  // ------------------------------------------------------------
  // ROTATE
  // ------------------------------------------------------------

  static Future<void> rotateCW(
    BuildContext context,
    List<AvesEntry> entries,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.rotate(entry, clockwise: true);
      } catch (e) {
        _show(context, 'Errore rotazione: $e');
      }
    }
  }

  static Future<void> rotateCCW(
    BuildContext context,
    List<AvesEntry> entries,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.rotate(entry, clockwise: false);
      } catch (e) {
        _show(context, 'Errore rotazione: $e');
      }
    }
  }

  // ------------------------------------------------------------
  // FLIP
  // ------------------------------------------------------------

  static Future<void> flip(
    BuildContext context,
    List<AvesEntry> entries,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.flip(entry);
      } catch (e) {
        _show(context, 'Errore flip: $e');
      }
    }
  }

  // ------------------------------------------------------------
  // METADATA (logica pura, i dialog sono nel file 3)
  // ------------------------------------------------------------

  static Future<void> editDate(
    BuildContext context,
    List<AvesEntry> entries,
    int? newDateMillis,
  ) async {
    if (newDateMillis == null) return;

    for (final entry in entries) {
      try {
        await mediaEditService.editDate(entry, newDateMillis);
      } catch (e) {
        _show(context, 'Errore modifica data: $e');
      }
    }
  }

  static Future<void> editLocation(
    BuildContext context,
    List<AvesEntry> entries,
    double? lat,
    double? lon,
  ) async {
    if (lat == null || lon == null) return;

    for (final entry in entries) {
      try {
        await mediaEditService.editLocation(entry, lat, lon);
      } catch (e) {
        _show(context, 'Errore modifica posizione: $e');
      }
    }
  }

  static Future<void> editTitleDescription(
    BuildContext context,
    List<AvesEntry> entries,
    String? title,
    String? description,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.editTitleDescription(
          entry,
          title: title,
          description: description,
        );
      } catch (e) {
        _show(context, 'Errore modifica titolo/descrizione: $e');
      }
    }
  }

  static Future<void> editRating(
    BuildContext context,
    List<AvesEntry> entries,
    int? rating,
  ) async {
    if (rating == null) return;

    for (final entry in entries) {
      try {
        await mediaEditService.editRating(entry, rating);
      } catch (e) {
        _show(context, 'Errore modifica rating: $e');
      }
    }
  }

  static Future<void> editTags(
    BuildContext context,
    List<AvesEntry> entries,
    Set<String> tags,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.editTags(entry, tags);
      } catch (e) {
        _show(context, 'Errore modifica tag: $e');
      }
    }
  }

  static Future<void> removeMetadata(
    BuildContext context,
    List<AvesEntry> entries,
  ) async {
    for (final entry in entries) {
      try {
        await mediaEditService.removeMetadata(entry);
      } catch (e) {
        _show(context, 'Errore rimozione metadata: $e');
      }
    }
  }

  // ------------------------------------------------------------
  // UTILS
  // ------------------------------------------------------------

  static void _show(BuildContext context, String msg) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(content: Text(msg)),
    );
  }
}
