import 'dart:convert';

import 'package:shared_preferences/shared_preferences.dart';

class AppSharedPreferences {
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
  Map<String, dynamic> _allData;

  // To Test Saved Data
  Future<Map<String, dynamic>> getAll() async{
    final prefs = (await _prefs);
    _allData = {};
    prefs.getKeys().forEach((key) {
      _allData[key] = prefs.get(key);
    });
    return _allData;
  }

  /// Save String [key] the key for save value [value] the value we need to save it
  setString(String key, String value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return prefs.setString(key, value);
    }
  }

  /// Save List of String [key] the key for save value [value] the value we need to save it
  setStringList(String key, List<String> value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return prefs.setStringList(key, value);
    }
  }

  /// Save Double [key] the key for save value [value] the value we need to save it
  setDouble(String key, double value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return prefs.setDouble(key, value);
    }
  }

  /// Save Bool [key] the key for save value [value] the value we need to save it
  setBool(String key, bool value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return prefs.setBool(key, value);
    }
  }

  /// Save int [key] the key for save value [value] the value we need to save it
  setInt(String key, int value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return prefs.setInt(key, value);
    }
  }

  /// save Object [key] the key for save value [value] the value we need to save it
  setObject(String key, value) async {
    if (value != null) {
      final SharedPreferences prefs = await _prefs;
      return await prefs.setString(key, json.encode(value));
    }
  }

  /// Get String [key] the key was saved
  Future getString(String key) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    return prefs.getString(key);
  }

  /// Get String [key] the key was saved
  getStringWithDefaultValue(String key, String defaultVal) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    String value = prefs.getString(key);
    return value == null ? defaultVal : value;
  }

  /// Get List of String [key] the key was saved
  getStringList(String key) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    return prefs.getStringList(key);
  }

  /// Get Double [key] the key was saved
  getDouble(String key) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    return prefs.getDouble(key);
  }

  /// Get Bool [key] the key was saved
  getBool(String key) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    return prefs.getBool(key);
  }

  /// Get Int [key] the key was saved
  getInt(String key) async {
    final SharedPreferences prefs = await _prefs;
    await prefs.reload();
    return prefs.getInt(key);
  }

  /// Get Object [key] the key was saved
  Future getObject(String key) async {
    try {
      final SharedPreferences prefs = await  _prefs;
      await prefs.reload();
      var string = prefs.getString(key);
      if (string == null) {
        return null;
      }
      return json.decode(string);
    } catch (ex) {
      print(ex);
      print(ex.toString());
    }
  }

  /// clear all saved values in shared preferences
  clear() async {
    final SharedPreferences prefs = await _prefs;
    prefs.clear();
  }

  /// remove value based on [key] the key was saved
  remove(String key) async {
    final SharedPreferences prefs = await _prefs;
    prefs.remove(key).then((value) {
      print(value);
    });
  }
}
