import { Component } from "@angular/core";
import { COMPONENT_SCOPE_PREFIX } from "../framework/common/extensibility/details-container/details-container.extension-point";
import { ExtensionComponentRegistration, ExtensionComponentRegistry } from "../framework/common/extensibility/extension-component.registry";
import { PluginUtilService } from "../framework/common/extensibility/plugin-util.service";
import { RESOURCES_ROUTE } from "../framework/app/plugin-container/plugin-container.module";

@Component({
    selector: "datacenter-details",
    templateUrl: "./datacenter-details.component.html",
    host: {
        "[class.extension-host]": "true",
    },
})
export class DatacenterDetailsComponent {
    verticalNavCollapsed: boolean = false;

    navItems: any[] = [
        {
            title: "Compute",
            iconShape: "applications",
            extensionPoint: `${COMPONENT_SCOPE_PREFIX}:navigation:datacenter:compute`,
            children: [
                {
                    nameKey: "vApps",
                    routerLink: "./vapps"
                },
                {
                    nameKey: "Virtual Machines",
                    routerLink: "./vms"
                }
            ],
        },
        {
            title: "Netwrok",
            iconShape: "network-globe",
            extensionPoint: `${COMPONENT_SCOPE_PREFIX}:navigation:datacenter:network`,
            children: [],
        },
        {
            title: "Storage",
            iconShape: "storage",
            extensionPoint: `${COMPONENT_SCOPE_PREFIX}:navigation:datacenter:storage`,
            children: [],
        },
    ];

    constructor(
        private extensionComponentRegistry: ExtensionComponentRegistry,
        private pluginUtilService: PluginUtilService,
    ) {
        this.loadExtensionPoins();
    }

    private loadExtensionPoins(): void {
        this.navItems.forEach((navItemGroup) => {
            const extensions = this.extensionComponentRegistry.getRegisteredComponents(navItemGroup.extensionPoint);

            extensions.forEach((extension: ExtensionComponentRegistration) => {
                const nameKey = this.pluginUtilService.translate(extension.manifest.name, extension.translations);

                navItemGroup.children.push({
                    nameKey,
                    nameTranslated: true,
                    routerLink: `${RESOURCES_ROUTE}/${
                        extension.vcdRoute
                            ? extension.hashedVendor + "/" + extension.vcdRoute
                            : extension.manifest.urn
                    }`,
                });
            });
        });
    }
}