/*
 * Copyright 2020-2022 VMware, Inc. All rights reserved. VMware Confidential
 */

import { Injectable, NgModuleRef } from "@angular/core";
import { COMPONENT_SCOPE_PREFIX } from "../details-container/details-container.extension-point";
import { ExtensionPointManifest } from "../extensibility.model";
import { ExtensionComponentRegistration, ExtensionComponentRegistry } from "../extension-component.registry";
import { WizardExtensionWithValidationComponent } from "../extension-point-orchestrator/extension-point-container.interface";
import { ExtensionPointHandler } from "../extension-point.handler";
import { ExternalExtensionLoaderState } from "../loader/extension-loader.model";
import { getHashedString } from "../loader/external/create-external-extension-route.step";
/**
 * This scope is intended to be injected in the create org wizard/modal
 * (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
export const CREATE_ORG_COMPONENT_SCOPE = `${COMPONENT_SCOPE_PREFIX}:create-org`;

/**
 * Dashboard manifest type (constant value)
 */
export const CREATE_ORG_TYPE = "create-org";
export type CREATE_ORG_TYPE = "create-org";

/**
 * The manifest versions this is supported by.
 */
export const SUPPORTED_VERSIONS: string[] = ["9.5.0"];

/**
 * Extends create org wizard/modal by injecting component(s) inside.
 */
export interface CreateOrgExtensionPointManifest extends ExtensionPointManifest {
    readonly type: "create-org";
    readonly component: string;
}

/**
 * This extension point handler supports extension manifests of version 9.5.0 and of type
 * "create-org". It will register the defined component with the ExtensionComponentRegistry
 * under the scope of "urn:vmware:vcd:component-scope:create-org".  This scope is intended to be
 * injected in the create org wizard/modal (i.e. with <ng-container vcdExtensionComponents scope="...">)
 */
@Injectable()
export class CreateOrgExtensionPointHandler implements ExtensionPointHandler {
    constructor(private extensionComponentRegistry: ExtensionComponentRegistry) {}

    /**
     * Supports version 9.5.0 and type "create-org".
     */
    supports(version: string, type: string): boolean {
        return SUPPORTED_VERSIONS.indexOf(version) != -1 && type === CREATE_ORG_TYPE;
    }

    /**
     * Register the defined component with the ExtensionComponentRegistry under the scope of
     * "urn:vmware:vcd:component-scope:create-org".
     */
    bootstrap(
        extensionBundle: any,
        extensionAngularModule: NgModuleRef<any>,
        extensionPointManifest: CreateOrgExtensionPointManifest,
        ordering: number,
        state: ExternalExtensionLoaderState
    ): void {
        if (!extensionPointManifest.component) {
            throw new Error("Create Org component not defined in manifest");
        }

        // const component = extensionBundle[extensionPointManifest.component];
        // if (!component) {
        //     throw new Error(
        //         `Create Org component '${extensionPointManifest.component}' ` + "not exported by extension bundle"
        //     );
        // }

        // if (!Object.prototype.isPrototypeOf.call(WizardExtensionWithValidationComponent.prototype, component.prototype)) {
        //     throw new Error(
        //         `Create Org component '${extensionPointManifest.component}' ` +
        //             "not inheriting from WizardExtensionPointComponent base class"
        //     );
        // }

        if (
            !extensionPointManifest.render ||
            (!extensionPointManifest.render.before && !extensionPointManifest.render.after)
        ) {
            throw new Error(
                `Create Org component '${extensionPointManifest.component}' ` + "doesn't have render property defined"
            );
        }

        const registration = new ExtensionComponentRegistration(
            CREATE_ORG_COMPONENT_SCOPE,
            extensionAngularModule,
            // state.angularModuleFactories[`${state.spec.id}:${extensionPointManifest.urn}`],
            null,
            // component,
            null,
            [],
            ordering,
            extensionPointManifest,
            state.manifest.locales,
            null,
            getHashedString(state.manifest.vendor)
        );
        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }
}
