/*
 *   Famedly Matrix SDK
 *   Copyright (C) 2019, 2020, 2021 Famedly GmbH
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Affero General Public License as
 *   published by the Free Software Foundation, either version 3 of the
 *   License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   GNU Affero General Public License for more details.
 *
 *   You should have received a copy of the GNU Affero General Public License
 *   along with this program.  If not, see .
 */
import 'dart:convert';
import 'package:async/async.dart';
import 'package:canonical_json/canonical_json.dart';
import 'package:collection/collection.dart';
import 'package:olm/olm.dart' as olm;
import 'package:matrix/encryption/encryption.dart';
import 'package:matrix/encryption/utils/json_signature_check_extension.dart';
import 'package:matrix/encryption/utils/olm_session.dart';
import 'package:matrix/matrix.dart';
import 'package:matrix/msc_extensions/msc_3814_dehydrated_devices/api.dart';
import 'package:matrix/src/utils/run_in_root.dart';
class OlmManager {
  final Encryption encryption;
  Client get client => encryption.client;
  olm.Account? _olmAccount;
  String? ourDeviceId;
  /// Returns the base64 encoded keys to store them in a store.
  /// This String should **never** leave the device!
  String? get pickledOlmAccount =>
      enabled ? _olmAccount!.pickle(client.userID!) : null;
  String? get fingerprintKey =>
      enabled ? json.decode(_olmAccount!.identity_keys())['ed25519'] : null;
  String? get identityKey =>
      enabled ? json.decode(_olmAccount!.identity_keys())['curve25519'] : null;
  String? pickleOlmAccountWithKey(String key) =>
      enabled ? _olmAccount!.pickle(key) : null;
  bool get enabled => _olmAccount != null;
  OlmManager(this.encryption);
  /// A map from Curve25519 identity keys to existing olm sessions.
  Map> get olmSessions => _olmSessions;
  final Map> _olmSessions = {};
  // NOTE(Nico): On initial login we pass null to create a new account
  Future init({
    String? olmAccount,
    required String? deviceId,
    String? pickleKey,
    String? dehydratedDeviceAlgorithm,
  }) async {
    ourDeviceId = deviceId;
    if (olmAccount == null) {
      try {
        await olm.init();
        _olmAccount = olm.Account();
        _olmAccount!.create();
        if (!await uploadKeys(
          uploadDeviceKeys: true,
          updateDatabase: false,
          dehydratedDeviceAlgorithm: dehydratedDeviceAlgorithm,
          dehydratedDevicePickleKey:
              dehydratedDeviceAlgorithm != null ? pickleKey : null,
        )) {
          throw ('Upload key failed');
        }
      } catch (_) {
        _olmAccount?.free();
        _olmAccount = null;
        rethrow;
      }
    } else {
      try {
        await olm.init();
        _olmAccount = olm.Account();
        _olmAccount!.unpickle(pickleKey ?? client.userID!, olmAccount);
      } catch (_) {
        _olmAccount?.free();
        _olmAccount = null;
        rethrow;
      }
    }
  }
  /// Adds a signature to this json from this olm account and returns the signed
  /// json.
  Map signJson(Map payload) {
    if (!enabled) throw ('Encryption is disabled');
    final Map? unsigned = payload['unsigned'];
    final Map? signatures = payload['signatures'];
    payload.remove('unsigned');
    payload.remove('signatures');
    final canonical = canonicalJson.encode(payload);
    final signature = _olmAccount!.sign(String.fromCharCodes(canonical));
    if (signatures != null) {
      payload['signatures'] = signatures;
    } else {
      payload['signatures'] = {};
    }
    if (!payload['signatures'].containsKey(client.userID)) {
      payload['signatures'][client.userID] = {};
    }
    payload['signatures'][client.userID]['ed25519:$ourDeviceId'] = signature;
    if (unsigned != null) {
      payload['unsigned'] = unsigned;
    }
    return payload;
  }
  String signString(String s) {
    return _olmAccount!.sign(s);
  }
  bool _uploadKeysLock = false;
  CancelableOperation