import 'dart:convert';

import 'package:mohem_flutter_app/models/config_model.dart';

import 'package:shared_preferences/shared_preferences.dart'
as SharedPrefsPlugin;

///
/// Taken from AlarmGuide Project
///

abstract class ISharedPreferences {
  Future<int?> get authState;

  Future<void> setAuthState(int authState);

  Future<int?> get configState;

  Future<void> setConfigState(int confState);

  Future<ConfigModel?> get config;

  Future<void> setConfig(ConfigModel config);

  Future<bool?> get promotionNotificationsEnabled;

  Future<void> setPromotionNotificationEnabled(bool newSetting);

  Future<bool?> get helpAlreadyShown;

  Future<void> setHelpAlreadyShown();

  Future<int?> get useS3;

  Future<void> setUseS3(int value);
}

class SharedPreferences implements ISharedPreferences {
  static const String _AUTH_STATE_KEY = "auth_key";
  static const String _CONFIG_KEY = "config";
  static const String _CONFIG_STATE_KEY = "config_key";
  static const String _PROMOTION_NOTIFICATION_KEY = "promotion";
  static const String _HELP_ALREADY_SHOWN = "help_shown";
  static const String _USE_S3 = "s3";

  @override
  Future<int?> get authState async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    return sharedPrefs.getInt(_AUTH_STATE_KEY);
  }

  @override
  Future<void> setAuthState(int authState) async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setInt(_AUTH_STATE_KEY, authState);
  }

  @override
  Future<ConfigModel?> get config async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    final configAsJson = sharedPrefs.getString(_CONFIG_KEY);
    return ConfigModel.fromJson(jsonDecode(configAsJson!));
  }

  @override
  Future<void> setConfig(ConfigModel config) async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setString(_CONFIG_KEY, jsonEncode(config));
    setConfigState(1);
  }

  @override
  Future<bool?> get promotionNotificationsEnabled async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    return sharedPrefs.getBool(_PROMOTION_NOTIFICATION_KEY);
  }

  @override
  Future<void> setPromotionNotificationEnabled(bool newSetting) async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setBool(_PROMOTION_NOTIFICATION_KEY, newSetting);
  }

  @override
  Future<bool?> get helpAlreadyShown async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    return sharedPrefs.getBool(_HELP_ALREADY_SHOWN);
  }

  @override
  Future<void> setHelpAlreadyShown() async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setBool(_HELP_ALREADY_SHOWN, true);
  }

  @override
  Future<int?> get configState async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    return sharedPrefs.getInt(_CONFIG_STATE_KEY);
  }

  @override
  Future<void> setConfigState(int confState) async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setInt(_CONFIG_STATE_KEY, confState);
  }

  @override
  Future<void> setUseS3(int value) async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    sharedPrefs.setInt(_USE_S3, value);
  }

  @override
  Future<int?> get useS3 async {
    final sharedPrefs = await SharedPrefsPlugin.SharedPreferences.getInstance();
    return sharedPrefs.getInt(_USE_S3);
  }
}
