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

/// A service that stores and retrieves user settings.
///
/// By default, this class does not persist user settings. If you'd like to
/// persist the user settings locally, use the shared_preferences package. If
/// you'd like to store settings on a web server, use the http package.
class SettingsService {
  /// Loads the User's preferred ThemeMode from local or remote storage.
  Future<ThemeMode> themeMode() async {
    var prefs = await SharedPreferences.getInstance();
    var index = prefs.getInt("theme");
    if (index != null) {
      return ThemeMode.values[index];
    }
    return ThemeMode.system;
  }

  Future<bool> useRegex() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool("useRegex") ?? false;
  }

  Future<bool> caseSensitive() async {
    var prefs = await SharedPreferences.getInstance();
    return prefs.getBool("caseSensitive") ?? false;
  }

  /// Persists the user's preferred ThemeMode to local or remote storage.
  Future<void> updateThemeMode(ThemeMode theme) async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setInt("theme", theme.index);
  }

  /// Persists the user's preferred ThemeMode to local or remote storage.
  Future<void> updateUseRegex(bool useRegex) async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setBool("useRegex", useRegex);
  }

  Future<void> updateCaseSensitive(bool caseSensitive) async {
    var prefs = await SharedPreferences.getInstance();
    prefs.setBool("caseSensitive", caseSensitive);
  }
}
