import 'dart:convert';

import 'package:hmg_patient_app_new/services/logger_service.dart';
import 'package:shared_preferences/shared_preferences.dart';

abstract class CacheService {
  Future<void> saveString({required String key, required String value});

  Future<void> saveInt({required String key, required int value});

  Future<void> saveDouble({required String key, required double value});

  Future<void> saveBool({required String key, required bool value});

  Future<void> saveStringList({required String key, required List<String> value});

  String? getString({required String key});

  int? getInt({required String key});

  double? getDouble({required String key});

  bool? getBool({required String key});

  List<String>? getStringList({required String key});

  Future<dynamic> getObject({required String key});

  Future<void> saveObject({required String key, required dynamic value});

  Future<void> remove({required String key});

  Future<void> clear();
}

class CacheServiceImp implements CacheService {
  final SharedPreferences sharedPreferences;
  final LoggerService loggerService;

  CacheServiceImp({
    required this.sharedPreferences,
    required this.loggerService,
  });

  @override
  Future<void> saveString({required String key, required String value}) async {
    await sharedPreferences.setString(key, value);
  }

  @override
  Future<void> saveInt({required String key, required int value}) async {
    await sharedPreferences.setInt(key, value);
  }

  @override
  Future<void> saveDouble({required String key, required double value}) async {
    await sharedPreferences.setDouble(key, value);
  }

  @override
  Future<void> saveBool({required String key, required bool value}) async {
    await sharedPreferences.setBool(key, value);
  }

  @override
  Future<void> saveStringList({required String key, required List<String> value}) async {
    await sharedPreferences.setStringList(key, value);
  }

  @override
  String? getString({required String key}) => sharedPreferences.getString(key);

  @override
  int? getInt({required String key}) => sharedPreferences.getInt(key);

  @override
  double? getDouble({required String key}) => sharedPreferences.getDouble(key);

  @override
  bool? getBool({required String key}) => sharedPreferences.getBool(key);

  @override
  List<String>? getStringList({required String key}) => sharedPreferences.getStringList(key);

  @override
  Future<void> remove({required String key}) async {
    await sharedPreferences.remove(key);
  }

  @override
  Future<dynamic> getObject({required String key}) async {
    try {
      await sharedPreferences.reload();
      var string = sharedPreferences.getString(key);
      if (string == null) return null;
      return json.decode(string);
    } catch (ex) {
      loggerService.errorLogs(ex.toString());
      return null;
    }
  }

  @override
  Future<void> saveObject({required String key, required dynamic value}) async {
    try {
      await sharedPreferences.setString(key, json.encode(value));
    } catch (ex) {
      loggerService.errorLogs(ex.toString());
    }
  }

  @override
  Future<void> clear() async {
    await sharedPreferences.clear();
  }
}
