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

import {
    AfterViewInit,
    Component,
    Directive,
    EventEmitter,
    Input,
    Output,
    ViewContainerRef,
} from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { SubscriptionTracker } from "../../../../subscription-tracker.service";
import {
    EntityContainerExtensionComponent
} from "../../../common/extensibility/details-container/details-container-extension.component";
import {
    ExtensionComponentRegistration,
    ExtensionComponentRegistry,
} from "../../../common/extensibility/extension-component.registry";
import { readParamFromUrlByName } from "../../../common/readParamsFromUrlByName";
import { VDC_ID_ROUTE_PARAM } from "../../routing-paths/vdcs-route-paths";
import { ComponentCreatedData } from "./ComponentCreatedData";
import { COMPONENT_SCOPE_PREFIX } from "../../../../lib-interfaces";

@Component({
    selector: "vcd-resources-component",
    templateUrl: "./resources.component.html",
    styleUrls: ["./resources.component.scss"],
    providers: [SubscriptionTracker],
})
export class ResourcesComponent implements AfterViewInit {
    private extensionPointKey: string = COMPONENT_SCOPE_PREFIX;
    private extensionRegistrations: ExtensionComponentRegistration[];

    resourceExtensionId: string;

    public extension;

    constructor(
        public activatedRoute: ActivatedRoute,
        private extensionComponentRegistry: ExtensionComponentRegistry,
        private router: Router,
        private subscriptionTracker: SubscriptionTracker
    ) {
        readParamFromUrlByName(
            this,
            this.activatedRoute,
            "resourceExtensionId",
            (resourceExtensionId) => {
                this.resourceExtensionId = resourceExtensionId;
                // TODO: this.extensionComponentRegistry.allRegistrationsObservable does not emit values
                //       Until this is fixed get direct access to the all registrations.
                const extensions = [].concat.apply(
                    [],
                    Array.from(this.extensionComponentRegistry["registrations"].values())
                );
                this.extension = extensions.find((extension) => extension.manifest.urn === resourceExtensionId);
            },
            this.subscriptionTracker
        );
    }

    ngAfterViewInit(): void {
    }
}

/**
 * Simply binds the extension registration to the view by invoking createComponent on the view.
 */
@Directive({
    selector: "[vcdExtensionTabContent]",
    providers: [SubscriptionTracker],
})
export class ExtensionTabContentDirective {
    @Input("vcdExtensionTabContent")
    set registration(registration: ExtensionComponentRegistration) {
        if (!registration) {
            return;
        }
        if (this.viewContainer.length > 0) {
            this.viewContainer.clear();
        }

        try {
            const component = registration.createComponent(this.viewContainer);
            this.componentCreated.emit({
                registration,
                component,
            });
            if (registration.component.prototype.hasOwnProperty("contextUrn")) {
                readParamFromUrlByName(
                    this,
                    this.activatedRoute.parent,
                    VDC_ID_ROUTE_PARAM,
                    (vdcId) => {
                        (component.instance as EntityContainerExtensionComponent).contextUrn(vdcId);
                    },
                    this.subscriptionTracker
                );
            }
        } catch (e) {
            console.error(`[vcdExtensionTabContent] Unable to create component`, e);
        }
    }

    @Output()
    componentCreated = new EventEmitter<ComponentCreatedData>();

    constructor(
        public activatedRoute: ActivatedRoute,
        private viewContainer: ViewContainerRef,
        private subscriptionTracker: SubscriptionTracker
    ) {
    }
}

