/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import IRegistrar from '../../src/registrars/IRegistrar';
import Identifier from '../../src/Identifier';
import IdentifierDocument from '../../src/IdentifierDocument';
import RegistrarCryptoOptions from '../../src/registrars/RegistrarCryptoOptions';
import CryptoFactory from '../../src/crypto/plugin/CryptoFactory';
import { KeyStoreInMemory, SubtleCryptoNode } from '../../src';
import JoseProtocol from '../../src/crypto/protocols/jose/JoseProtocol';

/**
 * Implementation of a registrar for testing.
 */
export default class TestRegistrar implements IRegistrar {
  private identifier: any;
  private identifierDocument: any;

  /**
   * Crypto options for the Registrar
   */
  public registrarCryptoOptions: RegistrarCryptoOptions = new RegistrarCryptoOptions(
    new CryptoFactory(new KeyStoreInMemory(), SubtleCryptoNode.getSubtleCrypto()), 
    new JoseProtocol());

  /**
   * Prepares the resolver for the test run.
   * @param identifier to use for the test.
   * @param identifierDocument to use for the test.
   */
  public prepareTest (identifier: Identifier, identifierDocument: IdentifierDocument) {
    this.identifier = identifier;
    this.identifierDocument = identifierDocument;
  }

  /**
   * Registers the identifier document on the ledger
   * returning the identifier generated by the registrar.
   * @inheritdoc
   */
  public async register (identifierDocument: IdentifierDocument): Promise<Identifier> {
    // if (this.identifierDocument === identifierDocument) {
    //   return this.identifier;
    // }

    // throw new Error('Not found');
    console.log(identifierDocument);
    console.log(this.identifierDocument);
    return this.identifier;
  }

  /**
   * Uses the specified input for generating
   * an identifier reference, but does not register
   * with a ledger.
   * @inheritdoc
   */
  public async generateIdentifier (input: any): Promise<Identifier> {
    const document = IdentifierDocument.create(`${input.id}:12345abcde`, input.publicKeys);
    this.identifierDocument = document;
    this.identifier = new Identifier(document);
    return this.identifier;
  }
}
