// ===========================================================================
// FILE: lib/core/theme_manager.dart
// ===========================================================================

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'app_colors.dart';
import '../services/storage_service.dart';

// --- ENUM DEI TEMI AGGIORNATO ---
const Map<AppThemeType, IconData> themeIcons = {
  AppThemeType.cyberpunk: Icons.electric_bolt,
  AppThemeType.doodle: Icons.brush,
  AppThemeType.music: Icons.headset_mic,
  AppThemeType.arcade: Icons.videogame_asset,
  AppThemeType.grimorio: Icons.auto_stories,
};

const Map<AppThemeType, String> themeNames = {
  AppThemeType.cyberpunk: "Cyberpunk",
  AppThemeType.doodle: "Doodle",
  AppThemeType.music: "Music",
  AppThemeType.arcade: "Arcade",
  AppThemeType.grimorio: "Grimorio",
};

class ThemeManager with ChangeNotifier {
  AppThemeType _currentThemeType = AppThemeType.doodle;
  ThemeColors _currentColors = AppColors.getTheme(AppThemeType.doodle);

  AppThemeType get currentThemeType => _currentThemeType;
  ThemeColors get currentColors => _currentColors;

  ThemeManager() {
    _loadTheme();
  }

  void _loadTheme() async {
    String themeStr = StorageService.instance.getTheme();
    AppThemeType loadedType = AppThemeType.values.firstWhere(
            (e) => e.toString() == themeStr,
        orElse: () => AppThemeType.doodle
    );
    _currentThemeType = loadedType;
    _currentColors = AppColors.getTheme(loadedType);
    _updateSystemUI();
    notifyListeners();
  }

  void setTheme(AppThemeType type) {
    _currentThemeType = type;
    _currentColors = AppColors.getTheme(type);
    StorageService.instance.saveTheme(type.toString());
    _updateSystemUI();
    notifyListeners();
  }

  void _updateSystemUI() {
    SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarIconBrightness: _currentThemeType == AppThemeType.doodle ? Brightness.dark : Brightness.light,
      systemNavigationBarColor: _currentColors.background,
      systemNavigationBarIconBrightness: _currentThemeType == AppThemeType.doodle ? Brightness.dark : Brightness.light,
    ));
  }
}