import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:pdf/pdf.dart'; // PdfPageFormat

// 1. Menu Mode Toggle
final menuModeProvider = NotifierProvider<MenuModeNotifier, bool>(MenuModeNotifier.new);

class MenuModeNotifier extends Notifier<bool> {
  @override
  bool build() => false;
  
  void set(bool value) => state = value;
  void toggle() => state = !state;
}

// 2. Selected Sake IDs
class SelectedMenuSakeNotifier extends Notifier<Set<String>> {
  @override
  Set<String> build() => {};

  void toggle(String id) {
    if (state.contains(id)) {
      state = {...state}..remove(id);
    } else {
      state = {...state, id};
    }
  }

  void clear() => state = {};
  
  bool isSelected(String id) => state.contains(id);
}

final selectedMenuSakeIdsProvider = NotifierProvider<SelectedMenuSakeNotifier, Set<String>>(SelectedMenuSakeNotifier.new);


// 3. Menu Order
final menuOrderedIdsProvider = NotifierProvider<MenuOrderedIdsNotifier, List<String>>(MenuOrderedIdsNotifier.new);

class MenuOrderedIdsNotifier extends Notifier<List<String>> {
  @override
  List<String> build() => [];
  
  void initialize(List<String> currentSelection) {
    state = List.from(currentSelection);
  }

  void reorder(int oldIndex, int newIndex) {
    if (oldIndex < newIndex) {
      newIndex -= 1;
    }
    final item = state.removeAt(oldIndex);
    state.insert(newIndex, item);
    state = [...state]; // Trigger notify
  }
}

// 4. PDF Settings Providers
// Note: We use Notifiers for complex logic, but simple StateProviders (Riverpod 2.0 style) are fine here.
// Actually, Riverpod recommended is Notifier, but StateProvider is still available.
// Let's use simple class-based Notifiers for 2.0 strictness if needed, or simple State for brevity.
// We will use StateProvider for now as they are declared in the previous snippets as "Notifiers" but let's stick to simple Notifiers or StateProvider if imported.
// In the viewed file, I only see NotifierProvider. Let's add simple Notifiers.

final pdfPageFormatProvider = NotifierProvider<PdfPageFormatNotifier, PdfPageFormat>(PdfPageFormatNotifier.new);
class PdfPageFormatNotifier extends Notifier<PdfPageFormat> {
  @override
  PdfPageFormat build() => PdfPageFormat.a4; // Default Portrait A4. 
  // Wait, A4 is actually "Portrait" by default in pdf package? Yes.
  void set(PdfPageFormat format) => state = format;
}

final pdfIsPortraitProvider = NotifierProvider<PdfIsPortraitNotifier, bool>(PdfIsPortraitNotifier.new);
class PdfIsPortraitNotifier extends Notifier<bool> {
  @override
  bool build() => true; // Default Portrait
  void set(bool value) => state = value;
}

final pdfIsMonochromeProvider = NotifierProvider<PdfIsMonochromeNotifier, bool>(PdfIsMonochromeNotifier.new);
class PdfIsMonochromeNotifier extends Notifier<bool> {
  @override
  bool build() => false;
  void set(bool value) => state = value;
}

final pdfIncludePriceProvider = NotifierProvider<PdfIncludePriceNotifier, bool>(PdfIncludePriceNotifier.new);
class PdfIncludePriceNotifier extends Notifier<bool> {
  @override
  bool build() => true;
  void set(bool value) => state = value;
}

final pdfDensityProvider = NotifierProvider<PdfDensityNotifier, double>(PdfDensityNotifier.new);
class PdfDensityNotifier extends Notifier<double> {
  @override
  double build() => 1.8; // Default 1.8 "High Density"
  void set(double value) => state = value;
}

// 4. Show Selected Only Toggle
final menuShowSelectedOnlyProvider = NotifierProvider<MenuShowSelectedNotifier, bool>(MenuShowSelectedNotifier.new);

class MenuShowSelectedNotifier extends Notifier<bool> {
  @override
  bool build() => false;
  void toggle() => state = !state;
  void set(bool value) => state = value;
}


// Logic to initialize menu order when entering mode or selecting items?
// Maybe easier:
// HomeScreen filters `rawList` by `selectedIds`.
// Result is a list.
// If the user reorders this list, we need to store the new order.
// Let's use `menuOrderedIdsProvider` to store the authoritative order of the MENU.

// When generating PDF, we read `menuOrderedIdsProvider` (or the current list in UI).

