import 'dart:io';

import 'package:hive_flutter/hive_flutter.dart';
import 'package:crypto/crypto.dart';
import 'dart:convert';
import 'package:kaer_with_panels/app/utils/kr_log_util.dart';
import 'package:path_provider/path_provider.dart';

class KRSecureStorage {
  // 创建一个单例实例
  static final KRSecureStorage _instance = KRSecureStorage._internal();
  factory KRSecureStorage() => _instance;

  // 私有构造函数
  KRSecureStorage._internal();

  // 存储箱名称
  static const String _boxName = 'kaer_secure_storage';
  
  // 加密密钥
  static const String _encryptionKey = 'kaer_secure_storage_key';

  // 初始化 Hive
  Future<void> kr_initHive() async {
    try {
      if (Platform.isMacOS) {
        final baseDir = await getApplicationSupportDirectory();
        await Hive.initFlutter(baseDir.path);
      } else {
        await Hive.initFlutter();
      }
      // 使用加密适配器
      final key = HiveAesCipher(_generateKey());
      await Hive.openBox(_boxName, encryptionCipher: key);
    } catch (e) {
      KRLogUtil.kr_e('初始化 Hive 失败: $e', tag: 'SecureStorage');
    }
  }

  // 生成加密密钥
  List<int> _generateKey() {
    final key = utf8.encode(_encryptionKey);
    final hash = sha256.convert(key);
    return hash.bytes;
  }

  // 获取存储箱
  Box<dynamic> get _box => Hive.box(_boxName);

  // 存储数据
  Future<void> kr_saveData({required String key, required String value}) async {
    try {
      await _box.put(key, value);
    } catch (e) {
      KRLogUtil.kr_e('存储数据失败: $e', tag: 'SecureStorage');
    }
  }

  // 读取数据
  Future<String?> kr_readData({required String key}) async {
    try {
      return _box.get(key) as String?;
    } catch (e) {
      KRLogUtil.kr_e('读取数据失败: $e', tag: 'SecureStorage');
      return null;
    }
  }

  // 删除数据
  Future<void> kr_deleteData({required String key}) async {
    try {
      await _box.delete(key);
    } catch (e) {
      KRLogUtil.kr_e('删除数据失败: $e', tag: 'SecureStorage');
    }
  }

  // 清除所有数据
  Future<void> kr_clearAllData() async {
    try {
      await _box.clear();
    } catch (e) {
      KRLogUtil.kr_e('清除数据失败: $e', tag: 'SecureStorage');
    }
  }

  // 检查键是否存在
  Future<bool> kr_hasKey({required String key}) async {
    try {
      return _box.containsKey(key);
    } catch (e) {
      KRLogUtil.kr_e('检查键失败: $e', tag: 'SecureStorage');
      return false;
    }
  }

  // 保存布尔值
  Future<void> kr_saveBool({required String key, required bool value}) async {
    try {
      await _box.put(key, value);
    } catch (e) {
      KRLogUtil.kr_e('存储布尔值失败: $e', tag: 'SecureStorage');
    }
  }

  // 获取布尔值
  Future<bool?> kr_getBool({required String key}) async {
    try {
      return _box.get(key) as bool?;
    } catch (e) {
      KRLogUtil.kr_e('读取布尔值失败: $e', tag: 'SecureStorage');
      return null;
    }
  }

  // 保存整数
  Future<void> kr_saveInt({required String key, required int value}) async {
    try {
      await _box.put(key, value);
    } catch (e) {
      KRLogUtil.kr_e('存储整数失败: $e', tag: 'SecureStorage');
    }
  }

  // 获取整数
  Future<int?> kr_getInt({required String key}) async {
    try {
      return _box.get(key) as int?;
    } catch (e) {
      KRLogUtil.kr_e('读取整数失败: $e', tag: 'SecureStorage');
      return null;
    }
  }
} 