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

import '../domain/enums/app_mode.dart';

abstract class SettingsLocalDataSource {
  Locale? getLocale();
  Future<void> setLocale(Locale locale);
  ThemeMode getThemeMode();
  Future<void> setThemeMode(ThemeMode mode);
  AppMode? getAppMode();
  Future<void> setAppMode(AppMode mode);
  bool isSetupCompleted();
  Future<void> setSetupCompleted(bool completed);
  bool isOnboardingShown();
  Future<void> setOnboardingShown(bool shown);
}

class SettingsLocalDataSourceImpl implements SettingsLocalDataSource {
  final SharedPreferences sharedPreferences;

  static const String _localeKey = 'locale';
  static const String _themeModeKey = 'theme_mode';
  static const String _appModeKey = 'app_mode';
  static const String _setupCompletedKey = 'setup_completed';
  static const String _onboardingShownKey = 'onboarding_shown';

  SettingsLocalDataSourceImpl({required this.sharedPreferences});

  @override
  Locale? getLocale() {
    final localeCode = sharedPreferences.getString(_localeKey);
    if (localeCode == null) return null;
    return Locale(localeCode);
  }

  @override
  Future<void> setLocale(Locale locale) async {
    await sharedPreferences.setString(_localeKey, locale.languageCode);
  }

  @override
  ThemeMode getThemeMode() {
    final index = sharedPreferences.getInt(_themeModeKey);
    if (index == null) return ThemeMode.system;
    return ThemeMode.values[index];
  }

  @override
  Future<void> setThemeMode(ThemeMode mode) async {
    await sharedPreferences.setInt(_themeModeKey, mode.index);
  }

  @override
  AppMode? getAppMode() {
    final index = sharedPreferences.getInt(_appModeKey);
    if (index == null) return null;
    return AppMode.values[index];
  }

  @override
  Future<void> setAppMode(AppMode mode) async {
    await sharedPreferences.setInt(_appModeKey, mode.index);
  }

  @override
  bool isSetupCompleted() {
    return sharedPreferences.getBool(_setupCompletedKey) ?? false;
  }

  @override
  Future<void> setSetupCompleted(bool completed) async {
    await sharedPreferences.setBool(_setupCompletedKey, completed);
  }

  @override
  bool isOnboardingShown() {
    return sharedPreferences.getBool(_onboardingShownKey) ?? false;
  }

  @override
  Future<void> setOnboardingShown(bool shown) async {
    await sharedPreferences.setBool(_onboardingShownKey, shown);
  }
}
