import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingsProvider with ChangeNotifier {
  static const String _keyEnemyAnim = 'settings_enemy_anim';
  static const String _keyAttackAnimScale = 'settings_attack_anim_scale';

  bool _enableEnemyAnimations = true; // Default: Enabled
  double _attackAnimScale = 5.0; // Default: 5.0
  double _shakeOffset = 30.0; // Default: 30.0
  double _shakeCount =
      3.0; // Default: 3.0 (using double for slider convenience, cast to int where needed)

  bool get enableEnemyAnimations => _enableEnemyAnimations;
  double get attackAnimScale => _attackAnimScale;
  double get shakeOffset => _shakeOffset;
  double get shakeCount => _shakeCount;

  SettingsProvider() {
    _loadSettings();
  }

  Future<void> _loadSettings() async {
    final prefs = await SharedPreferences.getInstance();
    _enableEnemyAnimations = prefs.getBool(_keyEnemyAnim) ?? true;
    _attackAnimScale = prefs.getDouble(_keyAttackAnimScale) ?? 5.0;
    _shakeOffset = prefs.getDouble('settings_shake_offset') ?? 30.0;
    _shakeCount = prefs.getDouble('settings_shake_count') ?? 3.0;
    notifyListeners();
  }

  Future<void> toggleEnemyAnimations(bool value) async {
    _enableEnemyAnimations = value;
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    await prefs.setBool(_keyEnemyAnim, value);
  }

  Future<void> setAttackAnimScale(double value) async {
    _attackAnimScale = value;
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble(_keyAttackAnimScale, value);
  }

  Future<void> setShakeOffset(double value) async {
    _shakeOffset = value;
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble('settings_shake_offset', value);
  }

  Future<void> setShakeCount(double value) async {
    _shakeCount = value;
    notifyListeners();
    final prefs = await SharedPreferences.getInstance();
    await prefs.setDouble('settings_shake_count', value);
  }
}
