/*
 * ******************************************************************
 * Copyright (c) 2025 Broadcom. All Rights Reserved.
 * Broadcom Confidential. The term "Broadcom" refers to Broadcom Inc.
 * and/or its subsidiaries.
 * ******************************************************************
 */
import { CommonModule } from "@angular/common";
import { HttpClient, HttpClientModule } from '@angular/common/http';
import { NgModule, APP_INITIALIZER, Injector } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { StoreModule } from '@ngrx/store';
import { ClarityModule, ClrIconModule } from '@clr/angular';
import { I18nModule } from "@vcd/i18n";
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { environment } from '../environments/environment';
import { CONTAINER_BRANDING } from '../environments/branding';
import { AppConfigService } from './app-config.service';
import { CONTAINER_CREDENTIALS } from '../environments/access';
import { DataService } from "./common/data.service";
import { HomeComponent } from "./home/home.component";
import { AllInOneComponent } from "./all-in-one/all-in-one.component";
import { RefreshTokenService } from "./jwt/refresh-token.service";

import '@cds/core/icon/register.js';
import { ClarityIcons, sunIcon, moonIcon, pluginIcon, applicationsIcon } from '@cds/core/icon';
import { BOOTSTRAP_DETAILS, HYPER_PLUGIN_THEME, HyperPluginModule, HyperPluginViewEncapsulation } from "@vcf/ng-hyper-plugin-framework";
import { BootstrapDetailsService, hyperPluginFactory } from "./providers/hyper-plugin.factory";
import { ActiveThemeService } from "./services/active-theme.service";
import { VmActionsDemoComponent } from "./all-in-one/vm-actions/vm-actions-demo.component";

ClarityIcons.addIcons(sunIcon);
ClarityIcons.addIcons(moonIcon);
ClarityIcons.addIcons(pluginIcon);
ClarityIcons.addIcons(applicationsIcon);

@NgModule({
	declarations: [
		AppComponent,
		HomeComponent,
		AllInOneComponent,
		VmActionsDemoComponent,
	],
	imports: [
		BrowserModule,
		CommonModule,
		AppRoutingModule,
		ClarityModule,
		ClrIconModule,
		HttpClientModule,
		BrowserAnimationsModule,
		StoreModule.forRoot({}),

		/**
		 * Extensibility Essentials
		 */
		I18nModule.forRoot(),
		HyperPluginModule.forRoot({
			root: '',
			useHash: false,
			routes: [
				{
					path: `aio/plugins`,
					type: 'legacy:navigation:primary',
					fullHeight: true,
				},
				{
					path: `aio/plugins`,
					type: 'navigation:primary',
					fullHeight: true,
				},
				{
					path: `aio/plugins`,
					type: 'navigation:services:overview',
					fullHeight: true,
				},
				{
					path: `aio/vcf-services`,
					type: 'navigation:services:vcf',
					fullHeight: true,
				},
			],
			encapsulation: HyperPluginViewEncapsulation.Emulated,
			tokenHolder: {
				localStorage: {
					jwt: {
						propertyPath: "jwt",
					},
					authcontext: {
						propertyPath: "authcontext",
					},
					username: {
						propertyPath: "username",
					},
				},
			},
		}),
	],
	providers: [
		{ provide: CONTAINER_BRANDING, useValue: environment['branding'] },
		{ provide: CONTAINER_CREDENTIALS, useValue: environment['credentials'] },
		{ provide: APP_INITIALIZER, useFactory: bootstrapApplication, multi: true, deps: [AppConfigService] },
		RefreshTokenService,
		DataService,
		ActiveThemeService,
		BootstrapDetailsService,
		{
			provide: APP_INITIALIZER,
			useFactory: hyperPluginFactory,
			multi: true,
			deps: [HttpClient, BootstrapDetailsService, Injector],
		},
		{
			provide: BOOTSTRAP_DETAILS,
			useFactory: (bootstrapDetailsService: BootstrapDetailsService) => {
				return bootstrapDetailsService.bootstrapDetails;
			},
			deps: [BootstrapDetailsService],
		},
		{
            provide: HYPER_PLUGIN_THEME,
            useFactory: (activeThemeService: ActiveThemeService) => {
                return activeThemeService.themeMode$;
            },
            deps: [ActiveThemeService],
        },
	],
	bootstrap: [AppComponent]
})
export class AppModule {}

export function bootstrapApplication(appConfig: AppConfigService): () => Promise<boolean> {
	return () => new Promise<boolean>((resolve) => {
		console.group('[application bootstrap]');
		appConfig.login().then((tokens) => {
			const [jwt, authToken] = tokens;
			localStorage.setItem("jwt", jwt);
			localStorage.setItem("authtoken", authToken);
		});
		resolve(true);
		console.groupEnd();
	});
}
