/**
 * Copyright © 2024 Nevis Security AG. All rights reserved.
 */

import { Version } from './Version';

/**
 * The object containing information about Nevis Mobile Authentication SDK for Android.
 *
 * @group Essentials
 */
export abstract class AndroidMetaData {
	/**
	 * The current version of Nevis Mobile Authentication SDK for Android.
	 */
	abstract mobileAuthenticationVersion: Version;

	/**
	 * The SHA-256 hash of the signing certificate of the application that includes the Nevis Mobile
	 * Authentication SDK for Android.
	 *
	 * This information can be used to properly configure the {@link https://docs.nevis.net/configurationguide/use-cases/Mobile-Authentication/Mobile-Authentication-Use-Cases/Mobile-Authentication-with-Deep-Links#for-android | well-known}
	 * file on the backend, to use {@link https://developer.android.com/training/app-links/verify-android-applinks | Android App Links}.
	 */
	abstract signingCertificateSha256: string;

	/**
	 * The {@link https://docs.nevis.net/mobilesdk/guide/configuration#facet-id | facet ID} used by
	 * Nevis Mobile Authentication SDK for Android.
	 *
	 * The facet ID of your application has to match the list of allowed facets stored provided by
	 * the facet service of the backend.
	 */
	abstract applicationFacetId: string;

	/**
	 * Default constructor for {@link AndroidMetaData}.
	 *
	 * @param mobileAuthenticationVersion the current version of Nevis Mobile Authentication SDK for Android.
	 * @param signingCertificateSha256 the SHA-256 hash of the signing certificate of the application.
	 * @param applicationFacetId the facet ID used by Nevis Mobile Authentication SDK for Android.
	 * @returns an {@link AndroidMetaData} instance.
	 */
	static create(
		mobileAuthenticationVersion: Version,
		signingCertificateSha256: string,
		applicationFacetId: string
	): AndroidMetaData {
		return new AndroidMetaDataImpl(
			mobileAuthenticationVersion,
			signingCertificateSha256,
			applicationFacetId
		);
	}

	/**
	 * Alternate constructor that creates a {@link AndroidMetaData} from a json.
	 *
	 * @param json contains the source for instance creation.
	 * @returns an {@link AndroidMetaData} instance.
	 */
	static fromJson(json: any): AndroidMetaData {
		return AndroidMetaDataImpl.fromJson(json);
	}
}

export class AndroidMetaDataImpl extends AndroidMetaData {
	mobileAuthenticationVersion: Version;
	signingCertificateSha256: string;
	applicationFacetId: string;

	constructor(
		mobileAuthenticationVersion: Version,
		signingCertificateSha256: string,
		applicationFacetId: string
	) {
		super();
		this.mobileAuthenticationVersion = mobileAuthenticationVersion;
		this.signingCertificateSha256 = signingCertificateSha256;
		this.applicationFacetId = applicationFacetId;
	}

	static fromJson(json: any): AndroidMetaDataImpl {
		const mobileAuthenticationVersion = Version.fromJson(json.mobileAuthenticationVersion);

		return new AndroidMetaDataImpl(
			mobileAuthenticationVersion,
			json.signingCertificateSha256,
			json.applicationFacetId
		);
	}
}
