import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sigap/src/features/personalization/presentasion/controllers/settings/base_settings_controller.dart';

class DisplayModeController extends BaseSettingsController {
  // Available theme modes
  final List<String> themeOptions = [
    'System default',
    'Light mode',
    'Dark mode',
  ];

  // Current selected theme mode
  final RxString selectedThemeMode = 'System default'.obs;

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

  // Change theme mode
  void changeThemeMode(String mode) {
    if (!themeOptions.contains(mode)) return;

    selectedThemeMode.value = mode;
    _applyTheme(mode);
    saveSettings();
  }

  // Apply the theme based on selected mode
  void _applyTheme(String mode) {
    ThemeMode themeMode;

    switch (mode) {
      case 'Light mode':
        themeMode = ThemeMode.light;
        break;
      case 'Dark mode':
        themeMode = ThemeMode.dark;
        break;
      default:
        themeMode = ThemeMode.system;
    }

    Get.changeThemeMode(themeMode);
  }

  @override
  Future<bool> saveSettings() async {
    try {
      setLoading(true);

      // TODO: Save the theme setting to persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // await prefs.setString('theme_mode', selectedThemeMode.value);

      return true;
    } catch (e) {
      setError('Failed to save theme settings: ${e.toString()}');
      return false;
    } finally {
      setLoading(false);
    }
  }

  @override
  Future<void> loadSettings() async {
    try {
      setLoading(true);

      // TODO: Load the theme setting from persistent storage
      final prefs = Get.find<dynamic>(); // Replace with your storage solution
      // final savedTheme = await prefs.getString('theme_mode') ?? 'System default';
      // selectedThemeMode.value = savedTheme;

      // Apply the loaded theme
      _applyTheme(selectedThemeMode.value);
    } catch (e) {
      setError('Failed to load theme settings: ${e.toString()}');
    } finally {
      setLoading(false);
    }
  }
}
