import 'package:flutter/material.dart';
import 'theme_manager.dart';

/// A notifier that updates when the theme changes
class ThemeNotifier extends ChangeNotifier {
  static final ThemeNotifier _instance = ThemeNotifier._internal();
  factory ThemeNotifier() => _instance;
  ThemeNotifier._internal();

  SeasonalTheme _currentTheme = SeasonalTheme.default_;

  SeasonalTheme get currentTheme => _currentTheme;

  void setTheme(SeasonalTheme theme) {
    if (_currentTheme != theme) {
      _currentTheme = theme;
      notifyListeners();
    }
  }

  void initFromManager() {
    _currentTheme = ThemeManager.currentTheme;
  }
}

/// A widget that rebuilds when the theme changes
class ThemeAwareBuilder extends StatelessWidget {
  final Widget Function(BuildContext context, SeasonalTheme theme) builder;

  const ThemeAwareBuilder({
    super.key,
    required this.builder,
  });

  @override
  Widget build(BuildContext context) {
    return ListenableBuilder(
      listenable: ThemeNotifier(),
      builder: (context, child) {
        return builder(context, ThemeNotifier().currentTheme);
      },
    );
  }
}