import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class ThemeController extends GetxController {
  static ThemeController get to => Get.find();

  final _isDarkMode = false.obs;
  bool get isDarkMode => _isDarkMode.value;

  final _themeMode = ThemeMode.light.obs;
  ThemeMode get themeMode => _themeMode.value;

  static const String _themeKey = 'theme_mode';

  @override
  void onInit() {
    super.onInit();
    _loadThemeFromPrefs();
  }

  /// Load theme preference from local storage
  Future<void> _loadThemeFromPrefs() async {
    try {
      final prefs = await SharedPreferences.getInstance();
      final savedTheme = prefs.getString(_themeKey);
      
      if (savedTheme != null) {
        switch (savedTheme) {
          case 'dark':
            _themeMode.value = ThemeMode.dark;
            _isDarkMode.value = true;
            break;
          case 'light':
            _themeMode.value = ThemeMode.light;
            _isDarkMode.value = false;
            break;
          case 'system':
            _themeMode.value = ThemeMode.system;
            _isDarkMode.value = _isSystemDark();
            break;
        }
      } else {
        // Default to system theme
        _themeMode.value = ThemeMode.system;
        _isDarkMode.value = _isSystemDark();
      }
    } catch (e) {
      debugPrint('Error loading theme: $e');
    }
  }

  /// Save theme preference to local storage
  Future<void> _saveThemeToPrefs(String theme) async {
    try {
      final prefs = await SharedPreferences.getInstance();
      await prefs.setString(_themeKey, theme);
    } catch (e) {
      debugPrint('Error saving theme: $e');
    }
  }

  /// Check if system is in dark mode
  bool _isSystemDark() {
    return WidgetsBinding.instance.platformDispatcher.platformBrightness == 
           Brightness.dark;
  }

  /// Toggle between light and dark theme
  void toggleTheme() {
    if (_isDarkMode.value) {
      setLightTheme();
    } else {
      setDarkTheme();
    }
  }

  /// Set light theme
  void setLightTheme() {
    _themeMode.value = ThemeMode.light;
    _isDarkMode.value = false;
    _saveThemeToPrefs('light');
    Get.changeThemeMode(ThemeMode.light);
  }

  /// Set dark theme
  void setDarkTheme() {
    _themeMode.value = ThemeMode.dark;
    _isDarkMode.value = true;
    _saveThemeToPrefs('dark');
    Get.changeThemeMode(ThemeMode.dark);
  }

  /// Set system theme
  void setSystemTheme() {
    _themeMode.value = ThemeMode.system;
    _isDarkMode.value = _isSystemDark();
    _saveThemeToPrefs('system');
    Get.changeThemeMode(ThemeMode.system);
  }

  /// Get theme mode as string
  String getThemeModeString() {
    switch (_themeMode.value) {
      case ThemeMode.light:
        return 'Light';
      case ThemeMode.dark:
        return 'Dark';
      case ThemeMode.system:
        return 'System';
    }
  }

  /// Get theme icon
  IconData getThemeIcon() {
    if (_isDarkMode.value) {
      return Icons.dark_mode_rounded;
    } else {
      return Icons.light_mode_rounded;
    }
  }
}

