import 'package:flutter/cupertino.dart';
import 'package:hub/shared/helpers/base_storage.dart';
import 'package:hub/shared/helpers/secure_storage.dart';
import 'package:hub/shared/helpers/shared_preferences_storage.dart';
import 'package:hub/shared/helpers/sqlite_storage.dart';

class StorageHelper {
  static final StorageHelper _instance = StorageHelper._internal();
  factory StorageHelper() => _instance;
  StorageHelper._internal();

  late BuildContext? _context;

  BuildContext? get context => _context;
  set context(BuildContext? context) => _context = context;

  late bool _isRecovered = false;
  bool get isRecovered => _isRecovered;
  set isRecovered(bool isRecovered) => _isRecovered = isRecovered;

  Future<void> init() async {
    await SecureStorage.instance.init();
    await SharedPreferencesStorage.instance.init();
    await SQLiteStorage.instance.init();

    String? isFirstRun = await SharedPreferencesStorage.instance.get(SharedPreferencesKey.isFirstRun.value);

    if (isFirstRun != 'false') {
      await SharedPreferencesStorage.instance.set(SharedPreferencesKey.isFirstRun.value, 'false');
      await SecureStorage.instance.set(SecureStorageKey.isLogged.value, 'false');
    }
  }

  Future<String?> get(String key, Storage storage) {
    switch (storage) {
      case Storage.SecureStorage:
        return SecureStorage.instance.get(key);
      case Storage.SharedPreferences:
        return SharedPreferencesStorage.instance.get(key);
      case Storage.SQLiteStorage:
        return SQLiteStorage.instance.get(key);
    }
  }

  Future<void> set(String key, String value, Storage storage) {
    switch (storage) {
      case Storage.SecureStorage:
        return SecureStorage.instance.set(key, value);
      case Storage.SharedPreferences:
        return SharedPreferencesStorage.instance.set(key, value);
      case Storage.SQLiteStorage:
        return SQLiteStorage.instance.set(key, value);
    }
  }

  Future<void> delete(String key, Storage storage) {
    switch (storage) {
      case Storage.SecureStorage:
        return SecureStorage.instance.delete(key);
      case Storage.SharedPreferences:
        return SharedPreferencesStorage.instance.delete(key);
      case Storage.SQLiteStorage:
        return SQLiteStorage.instance.delete(key);
    }
  }

  Future<void> clearAll(Storage storage) {
    switch (storage) {
      case Storage.SecureStorage:
        return SecureStorage.instance.clearAll();
      case Storage.SharedPreferences:
        return SharedPreferencesStorage.instance.clearAll();
      case Storage.SQLiteStorage:
        return SQLiteStorage.instance.clearAll();
    }
  }
}
