import 'package:shared_preferences/shared_preferences.dart';
import '../game/audio/audio_manager.dart';
import 'theme_manager.dart';

class SettingsManager {
  static const String _keyMusicEnabled = 'music_enabled';
  static const String _keyHapticsEnabled = 'haptics_enabled';
  static const String _keyTutorialShown = 'tutorial_shown';
  static const String _keyBgmVolume = 'bgm_volume';
  static const String _keySfxVolume = 'sfx_volume';
  static const String _keySelectedTheme = 'selected_theme';

  static SharedPreferences? _prefs;

  static Future<void> init() async {
    _prefs = await SharedPreferences.getInstance();
    // Initialize theme manager
    await ThemeManager.init();
  }

  // Music Settings
  static bool get isMusicEnabled => _prefs?.getBool(_keyMusicEnabled) ?? true;
  
  static Future<void> setMusicEnabled(bool enabled) async {
    await _prefs?.setBool(_keyMusicEnabled, enabled);
  }

  // Haptics Settings
  static bool get isHapticsEnabled => _prefs?.getBool(_keyHapticsEnabled) ?? true;
  
  static Future<void> setHapticsEnabled(bool enabled) async {
    await _prefs?.setBool(_keyHapticsEnabled, enabled);
  }

  // BGM Volume Settings
  static double get bgmVolume => _prefs?.getDouble(_keyBgmVolume) ?? 0.4;
  
  static Future<void> setBgmVolume(double volume) async {
    await _prefs?.setDouble(_keyBgmVolume, volume);
  }

  // SFX Volume Settings
  static double get sfxVolume => _prefs?.getDouble(_keySfxVolume) ?? 0.6;
  
  static Future<void> setSfxVolume(double volume) async {
    await _prefs?.setDouble(_keySfxVolume, volume);
  }

  // Apply BGM volume to AudioManager
  static void applyBgmVolume() {
    AudioManager().setBgmVolume(bgmVolume);
  }

  // Apply SFX volume to AudioManager
  static void applySfxVolume() {
    AudioManager().setSfxVolume(sfxVolume);
  }

  // Tutorial Settings
  static bool get isTutorialShown => _prefs?.getBool(_keyTutorialShown) ?? false;
  
  static Future<void> setTutorialShown(bool shown) async {
    await _prefs?.setBool(_keyTutorialShown, shown);
  }

  // Theme Settings
  static SeasonalTheme get selectedTheme {
    final themeId = _prefs?.getString(_keySelectedTheme) ?? 'default';
    return SeasonalTheme.fromId(themeId);
  }
  
  static Future<void> setSelectedTheme(SeasonalTheme theme) async {
    await _prefs?.setString(_keySelectedTheme, theme.id);
    await ThemeManager.setTheme(theme);
  }
}