import 'package:cipher_guard_sdk/src/presentation/facade/cipher_guard_sdk_api.dart';
import 'package:cipher_guard_sdk/src/presentation/wiring/cipher_guard_sdk_core.dart';
import 'package:cipher_guard_sdk/src/domain/entities/rsa_key_pair.dart';
import 'package:cipher_guard_sdk/src/domain/entities/session_key.dart';
import 'package:cipher_guard_sdk/src/domain/entities/encrypted_message.dart';

/// SDK API 实现
class CipherGuardSdkApiImpl implements CipherGuardSdkApi {
  final CipherGuardSdkCore _core;

  CipherGuardSdkApiImpl({required CipherGuardSdkCore core}) : _core = core;

  // ── 活跃密钥内存存储（登录后 setActiveKeyPair 写入，退出登录 clearActiveKeyPair 清除）──

  String? _activePublicKey;
  String? _activePrivateKey;

  @override
  Future<String?> platformVersion() => _core.platform.getPlatformVersion();

  @override
  Future<RsaKeyPair> generateRsaKeyPair({int keySize = 1024}) {
    return _core.encryptionRepo.generateRsaKeyPair(keySize: keySize);
  }

  @override
  Future<String> encryptPrivateKey({
    required String privateKey,
    required String password,
  }) {
    return _core.encryptionRepo.encryptPrivateKey(
      privateKey: privateKey,
      password: password,
    );
  }

  @override
  Future<String> decryptPrivateKey({
    required String encryptedPrivateKey,
    required String password,
  }) {
    return _core.encryptionRepo.decryptPrivateKey(
      encryptedPrivateKey: encryptedPrivateKey,
      password: password,
    );
  }

  @override
  Future<SessionKey> generateSessionKey({int initialRound = 1}) {
    return _core.encryptionRepo.generateSessionKey(initialRound: initialRound);
  }

  @override
  Future<String> encryptSessionKey({
    required String sessionKey,
    required String publicKey,
  }) {
    return _core.encryptionRepo.encryptSessionKey(
      sessionKey: sessionKey,
      publicKey: publicKey,
    );
  }

  @override
  Future<String> decryptSessionKey({
    required String encryptedSessionKey,
    required String privateKey,
  }) {
    return _core.encryptionRepo.decryptSessionKey(
      encryptedSessionKey: encryptedSessionKey,
      privateKey: privateKey,
    );
  }

  @override
  Future<EncryptedMessage> encryptMessage({
    required String plaintext,
    required String sessionKey,
    required int round,
  }) {
    return _core.encryptionRepo.encryptMessage(
      plaintext: plaintext,
      sessionKey: sessionKey,
      round: round,
    );
  }

  @override
  Future<String> decryptMessage({
    required String encryptedData,
    required String sessionKey,
    required int round,
  }) {
    return _core.encryptionRepo.decryptMessage(
      encryptedData: encryptedData,
      sessionKey: sessionKey,
      round: round,
    );
  }

  @override
  void setActiveKeyPair({
    required String publicKey,
    required String privateKey,
  }) {
    _activePublicKey = publicKey;
    _activePrivateKey = privateKey;
  }

  @override
  void clearActiveKeyPair() {
    _activePublicKey = null;
    _activePrivateKey = null;
  }

  @override
  Future<String> encryptSessionKeyWithActiveKey({required String sessionKey}) {
    final key = _activePublicKey;
    if (key == null) {
      throw StateError('Active key pair not set. Call setActiveKeyPair first.');
    }
    return encryptSessionKey(sessionKey: sessionKey, publicKey: key);
  }

  @override
  Future<String> decryptSessionKeyWithActiveKey({
    required String encryptedSessionKey,
  }) {
    final key = _activePrivateKey;
    if (key == null) {
      throw StateError('Active key pair not set. Call setActiveKeyPair first.');
    }
    return decryptSessionKey(
      encryptedSessionKey: encryptedSessionKey,
      privateKey: key,
    );
  }

  @override
  void clearCaches() {
    // No KDF cache to clear — message encryption uses raw keys.
    // Placeholder for future RSA cache clearing if needed.
  }

  @override
  Future<void> syncEncryptionKey({
    required String chatId,
    required int activeRound,
    required int round,
    required String activeKey,
    required bool isSingle,
  }) {
    return _core.encryptionRepo.syncEncryptionKeysToNative(
      chatId: chatId,
      activeRound: activeRound,
      round: round,
      activeKey: activeKey,
      isSingle: isSingle,
    );
  }

  @override
  Future<void> syncAllEncryptionKeys({
    required Map<String, Map<String, dynamic>> chatMap,
  }) {
    return _core.encryptionRepo.syncAllEncryptionKeys(chatMap: chatMap);
  }

  @override
  Future<void> setAesSecret({required String aesSecret}) {
    return _core.encryptionRepo.setAesSecret(aesSecret: aesSecret);
  }

  @override
  Future<String?> decryptPushNotification({required String encryptedData}) {
    return _core.encryptionRepo.decryptPushNotification(
      encryptedData: encryptedData,
    );
  }
}
