/*
 * Copyright (C) 2017 Robert Bosch GmbH Copyright (C) 2017 Robert Bosch Manufacturing Solutions GmbH, Germany. All rights reserved.
 */

import { Component, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute, Router } from '@angular/router';
import {
  BciMessagesService,
  LoggerService,
  LogoutComponent,
  NavigationService,
  SidebarNavItem,
  LogLevel,
} from '@bci-web-core/core';
import { Observable } from 'rxjs';
import { map, mergeMap, tap } from 'rxjs/operators';

@Component({
  selector: 'bci-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss'],
})
export class AppComponent implements OnInit {
  title = 'Web Core Demo';
  sidebarLinks: SidebarNavItem[] = [];
  sidebarFooterLinks: SidebarNavItem[] = [
    this.bciMessagesService.sidebarLink(),
    {
      title: 'FAQ',
      url: 'faq',
      icon: 'Bosch-Ic-help',
    },
    {
      title: 'Maja Mustermann',
      overlay: {
        component: LogoutComponent,
        config: { title: 'Do you want to logout?', onLogout: () => this.logout() },
      },
      icon: 'Bosch-Ic-user',
    },
    {
      title: 'Changelog',
      // split current url at '/demos' and add url part to changelog to see the different changelog on deployed and snapshot version
      url: window.location.href.split('/demos', 1) + '/docs/core/changelog.html',
      icon: 'Bosch-Ic-clipboard',
    },
    this.navigationService.getImprintSidebarLink(),
  ];
  extendedNavigation = false;

  constructor(
    private titleService: Title,
    private bciMessagesService: BciMessagesService,
    private navigationService: NavigationService,
    private logger: LoggerService,
    private router: Router,
    private route: ActivatedRoute
  ) { }

  getSidebarLinks(): Observable<SidebarNavItem[]> {
    return this.route.queryParamMap.pipe(
      map((params) => params.get('extendedNavigation')),
      tap((param) => {
        if (param === 'true') {
          this.extendedNavigation = true;
        } else if (param === 'false') {
          this.extendedNavigation = false;
        }
      }),
      mergeMap(() => this.navigationService.getNavigationItems()),
      map((items) => {
        if (this.extendedNavigation) {
          items.find((item) => item.title === 'Navigation').items.find((item) => item.title === 'Breadcrumb').items = [
            {
              title: 'Getting Started',
              url: 'navigation/breadcrumbs-demo/getting-started',
              routerLinkActiveOptions: {
                exact: false,
              },
            },
            {
              title: 'Summary',
              url: 'navigation/breadcrumbs-demo/summary',
              routerLinkActiveOptions: {
                exact: false,
              },
            },
          ];
        }
        return items;
      })
    );
  }

  ngOnInit() {
    this.logger.logLevel = LogLevel.TRACE;
    this.titleService.setTitle(this.title);
    this.getSidebarLinks().subscribe((sidebarLinks) => (this.sidebarLinks = sidebarLinks));
  }

  logout() {
    this.logger.info('Logout mock');
    this.router.navigate(['login']);
  }
}
