/*
 * ******************************************************************
 * Copyright (c) 2024 Broadcom. All Rights Reserved.
 * Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
 * and/or its subsidiaries.
 * ******************************************************************
 */

import { Inject, Injectable, NgModuleRef } from "@angular/core";
import { Route, Router } from "@angular/router";
import { ExtensionComponentRegistration, ExtensionComponentRegistry } from "../extension-component.registry";
import { ExtensionContainerGuard } from "../extension-container/extension-container-guard";
import { ExtensionContainerComponent } from "../extension-container/extension-container.component";
import { ExtensionPointHandler } from "../extension-point.handler";
import { ExternalExtensionLoaderState } from "../loader/extension-loader.model";
import { getHashedString } from "../loader/external/create-external-extension-route.step";
import { SERVICES_OVERVIEW_COMPONENT_SCOPE, SERVICES_OVERVIEW_TYPE, SUPPORTED_VERSIONS } from "./services-overview.extension-point.constants";
import { ServicesOverviewExtensionPointManifest } from "./services-overview.extension-point.interfaces";
import { HYPER_PLUGIN_CONFIG, HyperpluginConfig } from "../../../../lib-interfaces";
import { ExtensionPointRoutingHandler } from "../extension-point-routing.handler";

/**
 * This extension point handler supports extension manifests of version 9.5.0 and of type
 * "services:overview".  It will register the defined module with the ExtensionComponentRegistry
 * under the scope of "urn:vmware:vcd:component-scope:services:overview. This scope is intended to be
 * injected in the Primary Navigation under Administration Category via the Router.
 */
@Injectable()
export class ServicesOverviewrExtensionPointHandler implements ExtensionPointHandler {
    constructor(
        @Inject(HYPER_PLUGIN_CONFIG) private HYPER_PLUGIN_CONFIG: HyperpluginConfig,
        private extensionPointRoutingHandler: ExtensionPointRoutingHandler,
        private extensionComponentRegistry: ExtensionComponentRegistry,
        private router: Router
    ) {}

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

    /**
     * Register the defined component with the ExtensionComponentRegistry under the scope of
     * "urn:vmware:vcd:component-scope:services:overview".
     */
    bootstrap(
        extensionBundle: any,
        extensionAngularModule: NgModuleRef<any>,
        extensionPointManifest: ServicesOverviewExtensionPointManifest,
        ordering: number,
        state: ExternalExtensionLoaderState
    ): void {
        if (!extensionPointManifest.module && !extensionPointManifest.component) {
            throw new Error("Services overview exptension point module/component is not defined in manifest.");
        }

        if (extensionPointManifest.module && extensionPointManifest.component) {
            throw new Error("Services overview exptension point only module or component can be defined in manifest, not both.");
        }

        const vcdRoute = state.routes[`${state.spec.id}:${extensionPointManifest.urn}`];

        if (!vcdRoute) {
            throw new Error(`Services overview extension point '${extensionPointManifest.name}'
            defined a module/component but no route was found.`);
        }

        try {
            this.registerDynamicRoutesInStaticModule(
                state.manifest.vendor,
                vcdRoute,
            );
        } catch (error) {
            throw Error(`Unable to register dynamic route ${vcdRoute}
            for extension point ${extensionPointManifest.name} in statc module.`);
        }

        const registration = new ExtensionComponentRegistration(
            SERVICES_OVERVIEW_COMPONENT_SCOPE,
            extensionAngularModule,
            null,
            null,
            [],
            ordering,
            extensionPointManifest,
            state.manifest.locales,
            vcdRoute,
            getHashedString(state.manifest.vendor),
            null,
            state.spec.resourcePath,
        );
        this.extensionComponentRegistry.registerExtensionComponent(registration);
    }

    private registerDynamicRoutesInStaticModule(
        vendor: string,
        vcdRoute: string,
    ): void {
        const extensibleRoute = this.HYPER_PLUGIN_CONFIG.routes.filter((route) => {
            return route.type === SERVICES_OVERVIEW_TYPE;
        })[0];

        if (!extensibleRoute) {
            return;
        }

        const extensibleRouteSegments = extensibleRoute.path.split("/");
        const mainPath = extensibleRouteSegments[0];
        const childSegments = extensibleRouteSegments.slice(1);

        let mainRoute = this.router.config.find((route) => {
            return route.path === mainPath;
        });

        this.extensionPointRoutingHandler.findRouteChain(mainRoute, childSegments).subscribe((pluginsContainerRouter) => {  
            if (!pluginsContainerRouter.children) {
                pluginsContainerRouter.children = [];
            }
    
            const alreadyExistingRouteVendorRoute = pluginsContainerRouter.children.find((route) => {
                return route.path === getHashedString(vendor);
            });
    
            if (!alreadyExistingRouteVendorRoute) {
                const newRoute: Route = {
                    path: getHashedString(vendor),
                    children: [
                        {
                            path: "",
                            pathMatch: "full",
                            redirectTo: vcdRoute,
                        },
                        {
                            path: vcdRoute,
                            component: ExtensionContainerComponent,
                            data: {
                                vcdRoute,
                                fullHeight: extensibleRoute.fullHeight
                            },
                            canDeactivate: [ExtensionContainerGuard],
                            children: [
                                {
                                    path: "**",
                                    component: ExtensionContainerComponent,
                                    canDeactivate: [ExtensionContainerGuard],
                                },
                            ],
                        },
                    ],
                };
    
                pluginsContainerRouter.children.push(newRoute);
                return;
            }
    
            alreadyExistingRouteVendorRoute.children.push({
                path: vcdRoute,
                component: ExtensionContainerComponent,
                data: {
                    vcdRoute,
                    fullHeight: extensibleRoute.fullHeight,
                },
                canDeactivate: [ExtensionContainerGuard],
                children: [
                    {
                        path: "**",
                        component: ExtensionContainerComponent,
                        canDeactivate: [ExtensionContainerGuard],
                    },
                ],
            });
        });
    }
}
