import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class LocalStoreManager {
  static SharedPreferences? _prefs;
  // call this method from iniState() function of mainApp().
  static Future<bool> init() async {
    _prefs = await SharedPreferences.getInstance();
    return true;
  }

  //sets
  static Future<bool> setBool(
    String key,
    bool value,
  ) async =>
      await _prefs!.setBool(key, value);

  static Future<bool> setDouble(String key, double value) async => await _prefs!.setDouble(key, value);

  static Future<bool> setInt(String key, int value) async => await _prefs!.setInt(key, value);

  static Future<bool> setString(String key, String value) async => await _prefs!.setString(key, value);

  static Future<bool> setObject(String key, Map<String, dynamic> object) async {
    try {
      final String value = jsonEncode(object);
      await _prefs!.setString(key, value);
      return true;
    } catch (ex) {
      return false;
    }
  }

  static Future<bool> setStringList(String key, List<String> value) async => await _prefs!.setStringList(key, value);

  //gets
  static bool getBool(String key) => _prefs?.getBool(key) ?? false;

  static double getDouble(String key) => _prefs?.getDouble(key) ?? 0;

  static int getInt(String key) => _prefs?.getInt(key) ?? 0;

  static String getString(String key) => _prefs?.getString(key) ?? "";

  static List<String> getStringList(String key) => _prefs!.getStringList(key) ?? <String>[];

  static Map<String, dynamic>? getObject(String key) {
    try {
      final String value = getString(key);
      return jsonDecode(value);
    } catch (ex) {
      return null;
    }
  }

  //deletes..
  static Future<bool> remove(String key) async => await _prefs!.remove(key);

  static Future<bool> clear() async => await _prefs!.clear();

  static bool checkKey(String key) {
    final bool checkValue = _prefs!.containsKey(key);
    return checkValue;
  }
}
