import 'dart:convert';
import 'package:hive/hive.dart';

class AppStorage {
  // static const _storage = FlutterSecureStorage();
  // static Future<Map<String, String>> getAllData() async {
  //   final allData = await _storage.readAll();
  //   return allData;
  // }
  // // Put methods
  // static Future<void> putString(String key, String value) async {
  //   await _storage.write(key: key, value: value);
  // }
  //
  // static Future<void> putInt(String key, int value) async {
  //   await _storage.write(key: key, value: value.toString());
  // }
  //
  // static Future<void> putDouble(String key, double value) async {
  //   await _storage.write(key: key, value: value.toString());
  // }
  //
  // static Future<void> putBoolean(String key, bool value) async {
  //   await _storage.write(key: key, value: value.toString());
  // }
  //
  // static Future<void> putObject(String key, Object value) async {
  //   final jsonString = jsonEncode(value);
  //   await _storage.write(key: key, value: jsonString);
  // }
  //
  // static Future<void> putList(String key, List<Object> value) async {
  //   final jsonString = jsonEncode(value);
  //   await _storage.write(key: key, value: jsonString);
  // }
  //
  // // Get methods with optional fallbacks
  // static Future<String> getString(String key, {String fallback = ''}) async {
  //   final value = await _storage.read(key: key);
  //   return value ?? fallback;
  // }
  //
  // static Future<int> getInt(String key, {int fallback = 0}) async {
  //   final value = await _storage.read(key: key);
  //   return value != null ? int.parse(value) : fallback;
  // }
  //
  // static Future<double> getDouble(String key, {double fallback = 0.0}) async {
  //   final value = await _storage.read(key: key);
  //   return value != null ? double.parse(value) : fallback;
  // }
  //
  // static Future<bool> getBoolean(String key, {bool fallback = false}) async {
  //   final value = await _storage.read(key: key);
  //   return value != null && value.toLowerCase() == 'true' ? true : fallback;
  // }
  //
  // static Future<Object> getObject(String key, {Object fallback = const {}}) async {
  //   final value = await _storage.read(key: key);
  //   return value != null ? jsonDecode(value) : fallback;
  // }
  //
  // static Future<List<Object>> getList(String key, {List<Object> fallback = const []}) async {
  //   final value = await _storage.read(key: key);
  //   return value != null ? List<Object>.from(jsonDecode(value)) : fallback;
  // }

// Delete method (same as before)

  static Future<void> putString(String key, String value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, value);
    await box.close();
  }

  static Future<void> delete(String key) async {
    final box = await Hive.openBox('appBox');
    await box.delete(key);
    await box.close();
  }

  static Future<void> putInt(String key, int value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, value);
    await box.close();
  }

  static Future<void> putDouble(String key, double value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, value);
    await box.close();
  }

  static Future<void> putBoolean(String key, bool value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, value);
    await box.close();
  }

  static Future<void> putObject(String key, Object value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, jsonEncode(value));
    await box.close();
  }

  static Future<void> putList(String key, List<Object> value) async {
    final box = await Hive.openBox('appBox');
    await box.put(key, jsonEncode(value));
    await box.close();
  }

  static Future<String> getString(String key, {String fallback = ''}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value;
  }

  static Future<int> getInt(String key, {int fallback = 0}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value;
  }

  static Future<double> getDouble(String key, {double fallback = 0.0}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value;
  }

  static Future<bool> getBoolean(String key, {bool fallback = false}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value;
  }

  static Future<Object> getObject(String key,
      {Object fallback = const {}}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value != null ? jsonDecode(value) : fallback;
  }

  static Future<List<Object>> getList(String key,
      {List<Object> fallback = const []}) async {
    final box = await Hive.openBox('appBox');
    final value = box.get(key, defaultValue: fallback);
    await box.close();
    return value != null ? List<Object>.from(jsonDecode(value)) : fallback;
  }

  static Future<List<dynamic>> getAllData() async {
    final box = await Hive.openBox('appBox');
    final keys = box.keys.toList();
    final List<dynamic> allData = [];

    for (final key in keys) {
      final value = box.get(key);
      allData.add({'key': key, 'value': value});
    }

    await box.close();
    return allData;
  }
}
