<%#
 Copyright 2013-2024 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { Component, inject, OnInit<% if (microfrontend && enableTranslation && applicationTypeGateway) { %>, createNgModule, Injector<% } %> } from '@angular/core';
import { Router, RouterModule } from '@angular/router';
<%_ if (enableTranslation) { _%>
import { TranslateService } from '@ngx-translate/core';
<%_ } _%>

import { StateStorageService } from 'app/core/auth/state-storage.service';
import SharedModule from 'app/shared/shared.module';
import HasAnyAuthorityDirective from 'app/shared/auth/has-any-authority.directive'
import { VERSION } from 'app/app.constants';
<%_ if (enableTranslation) { _%>
import { LANGUAGES } from 'app/config/language.constants';
import ActiveMenuDirective from './active-menu.directive';
<%_ } _%>
import { Account } from 'app/core/auth/account.model';
import { AccountService } from 'app/core/auth/account.service';
import { LoginService } from 'app/login/login.service';
import { ProfileService } from 'app/layouts/profiles/profile.service';
import { EntityNavbarItems } from 'app/entities/entity-navbar-items';
<% if (microfrontend && applicationTypeGateway) { %>
import { loadNavbarItems, loadTranslationModule } from 'app/core/microfrontend';
<%_ } _%>
import NavbarItem from './navbar-item.model';

@Component({
  standalone: true,
  selector: '<%= jhiPrefixDashed %>-navbar',
  templateUrl: './navbar.component.html',
  styleUrl: './navbar.component.scss',
  imports: [
    RouterModule,
    SharedModule,
    HasAnyAuthorityDirective,
<%_ if (enableTranslation) { _%>
    ActiveMenuDirective,
<%_ } _%>
  ],
})
export default class NavbarComponent implements OnInit {
  inProduction?: boolean;
  isNavbarCollapsed = true;
<%_ if (enableTranslation) { _%>
  languages = LANGUAGES;
<%_ } _%>
  openAPIEnabled?: boolean;
  version = '';
  account: Account | null = null;
  entitiesNavbarItems: NavbarItem[] = [];
<%_ if (applicationTypeGateway && microfrontend) { _%>
  <%_ for (const remote of microfrontends) { _%>
  <%= remote.lowercaseBaseName %>EntityNavbarItems: NavbarItem[] = [];
  <%_ } _%>
<%_ } _%>

  private loginService = inject(LoginService);
<%_ if (enableTranslation) { _%>
  private translateService = inject(TranslateService);
  private stateStorageService = inject(StateStorageService);
  <%_ if (applicationTypeGateway && microfrontend) { _%>
  private injector = inject(Injector);
  <%_ } _%>
<%_ } _%>
  private accountService = inject(AccountService);
  private profileService = inject(ProfileService);
  private router = inject(Router);

  constructor() {
    if (VERSION) {
      this.version = VERSION.toLowerCase().startsWith('v') ? VERSION : `v${VERSION}`;
    }
  }

  ngOnInit(): void {
    this.entitiesNavbarItems = EntityNavbarItems;
    this.profileService.getProfileInfo().subscribe(profileInfo => {
      this.inProduction = profileInfo.inProduction;
      this.openAPIEnabled = profileInfo.openAPIEnabled;
    });

    this.accountService.getAuthenticationState().subscribe(account => {
      this.account = account;
<%_ if (applicationTypeGateway && microfrontend) { _%>
      this.loadMicrofrontendsEntities();
<%_ } _%>
    });
  }

<%_ if (enableTranslation) { _%>
  changeLanguage(languageKey: string): void {
    this.stateStorageService.storeLocale(languageKey);
    this.translateService.use(languageKey);
  }
<%_ } _%>

  collapseNavbar(): void {
    this.isNavbarCollapsed = true;
  }

  login(): void {
<%_ if (!authenticationTypeOauth2) { _%>
    this.router.navigate(['/login']);
<%_ } else { _%>
    this.loginService.login();
<%_ } _%>
  }

  logout(): void {
    this.collapseNavbar();
    this.loginService.logout();
    this.router.navigate(['']);
  }

  toggleNavbar(): void {
    this.isNavbarCollapsed = !this.isNavbarCollapsed;
  }
<%_ if (applicationTypeGateway && microfrontend) { _%>

  loadMicrofrontendsEntities(): void {
    // Lazy load microfrontend entities.
  <%_ for (const remote of microfrontends) { _%>
    loadNavbarItems('<%= remote.lowercaseBaseName %>').then(
      async items => {
        this.<%= remote.lowercaseBaseName %>EntityNavbarItems = items;
    <%_ if (enableTranslation && applicationTypeGateway) { _%>
        try {
          const LazyTranslationModule = await loadTranslationModule('<%= remote.lowercaseBaseName %>');
          createNgModule(LazyTranslationModule, this.injector);
        } catch (error) {
          // eslint-disable-next-line no-console
          console.log('Error loading <%= remote.lowercaseBaseName %> translation module', error);
        }
    <%_ } _%>
      },
      error => {
        // eslint-disable-next-line no-console
        console.log('Error loading <%= remote.lowercaseBaseName %> entities', error);
      }
    );
  <%_ } _%>
  }
<%_ } _%>
}
