import 'package:flutter/material.dart';
import 'package:aesthetica_wallpaper/models/recipe.dart';

// EditorProvider 持有当前编辑器的所有状态
class EditorProvider with ChangeNotifier {
  // 从一个空白的 Recipe 开始
  Recipe _currentRecipe = Recipe(id: '', baseImagePath: '');

  // 获取当前状态的 getter
  Recipe get currentRecipe => _currentRecipe;

  // 动态模拟器状态
  bool _isTimeAware = false;
  bool get isTimeAware => _isTimeAware;

  bool _isBatteryAware = false;
  get isBatteryAware => _isBatteryAware;

  // 当进入编辑器时，重置或加载一个新的 Recipe
  void startEditing(String baseImagePath) {
    _currentRecipe = Recipe(
      id: DateTime.now().millisecondsSinceEpoch.toString(),
      baseImagePath: baseImagePath,
    );
    // 重置动态开关
    _isTimeAware = false;
    _isBatteryAware = false;
    notifyListeners();
  }

  // 当从"我的配方"加载时
  void loadFromRecipe(Recipe recipe) {
    _currentRecipe = recipe;
    notifyListeners();
  }

  // 更新各种参数的方法
  // 每个方法都会更新值并通知监听器 (UI) 重建

  void setBrightness(double value) {
    _currentRecipe = _currentRecipe.copyWith(brightness: value);
    notifyListeners();
  }

  void setContrast(double value) {
    _currentRecipe = _currentRecipe.copyWith(contrast: value);
    notifyListeners();
  }

  void setSaturation(double value) {
    _currentRecipe = _currentRecipe.copyWith(saturation: value);
    notifyListeners();
  }

  void setBlur(double value) {
    _currentRecipe = _currentRecipe.copyWith(blur: value);
    notifyListeners();
  }

  void setPixelate(double value) {
    _currentRecipe = _currentRecipe.copyWith(pixelate: value);
    notifyListeners();
  }

  void setText(String text) {
    _currentRecipe = _currentRecipe.copyWith(overlayText: text);
    notifyListeners();
  }

  void setFont(String font) {
    _currentRecipe = _currentRecipe.copyWith(fontFamily: font);
    notifyListeners();
  }

  void setTextColor(Color color) {
    _currentRecipe = _currentRecipe.copyWith(textColor: color.toARGB32());
    notifyListeners();
  }

  void setTimeAware(bool value) {
    _isTimeAware = value;
    notifyListeners();
  }

  void setBatteryAware(bool value) {
    _isBatteryAware = value;
    notifyListeners();
  }
}
