import 'package:cache_manager/cache_manager.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:telnow_mobile_new/src/storage/sharedpreferences/shared_preferences_manager.dart';

class CacheMan {
  static void writeData(String key, dynamic data) {
    Map<String, dynamic> json = new Map<String, dynamic>();
    json['data'] = data;

    WriteCache.setJson(key: "cache-$key", value: json);
  }

  static readData(String key) async {
    try {
      return await ReadCache.getJson(key: "cache-$key");
    } catch(e) {
      return null;
    }
  }

  static void deleteData(String key) {
    DeleteCache.deleteKey("cache-$key");
  }

  static Config config(cacheKey, {duration = const Duration(days: 1)}) {
    return Config(cacheKey, stalePeriod: duration);
  }

  static void clearData() async {
    try {
      final SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
      Set<String> keys = await sharedPreferences.getKeys();
      keys.forEach((el) {
        if (el.startsWith('cache-')) {
          sharedPreferences.remove(el);
        }
      });
    } catch(e) {

    }
  }
}