import { Component, OnDestroy, OnInit } from '@angular/core';
import { NbSidebarService, NbThemeService, NbMenuItem, NbDialogService } from '@nebular/theme';

import { LayoutService } from '../../../@core/utils';
import { map, takeUntil } from 'rxjs/operators';
import { Subject } from 'rxjs';
import { User } from '../../../../../../backend/Types/Types';
import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
import { IApiService } from '../../../frontcraft/services/ApiService/ApiService';

@Component({
  selector: 'ngx-header',
  styleUrls: ['./header.component.scss'],
  templateUrl: './header.component.html',
})
export class HeaderComponent implements OnInit, OnDestroy {

  private destroy$: Subject<void> = new Subject<void>();
  user: User;
  title = "Loading ... ";

  themes = [
    {
      value: 'default',
      name: 'Light',
    },
    {
      value: 'dark',
      name: 'Dark',
    },
    {
      value: 'cosmic',
      name: 'Cosmic',
    },
    {
      value: 'corporate',
      name: 'Corporate',
    },
  ];

  currentTheme = 'default';
  userMenu: NbMenuItem[] = [];

  sendMessage: any = console.log
  newmessage = false
  lastmessage = "asdasd"
  modifyPermissions

  constructor(
    private sidebarService: NbSidebarService,
    private router: Router,
    private themeService: NbThemeService,
    private layoutService: LayoutService,
    private api: IApiService,
  ) { }

  private setUserMenu = (redirect = this.router.url) => {
    if (this.user.username === 'Guest' && this.user.rank === 'Guest'){
      this.userMenu = [
        { title: 'Login', link: '/auth/login', queryParams: { redirect: redirect } },
        { title: 'Register', link: '/auth/register', queryParams: { redirect: redirect } },
      ]
    }else{
      this.userMenu = [
        { title: 'Profile', link: '/frontcraft/user/' + this.user.username },
        { title: 'Log out', link: '/auth/logout', queryParams: { redirect: redirect } },
      ]
    }
  }

  ngOnInit() {
    this.themeService.changeTheme("dark");

    this.currentTheme = this.themeService.currentTheme;
    this.modifyPermissions = this.api.get('modifyPermissions')
    this.user = this.api.getCurrentUser()
    
    this.setUserMenu()

    this.router.events.subscribe((val) => {
      if(val instanceof NavigationEnd) {
        this.setUserMenu(val.urlAfterRedirects)
      }
    })

    this.api.get('GuildManager').getGuildInfo().then(info => {
      this.title = info.name
    })

    this.themeService.onThemeChange()
      .pipe(
        map(({ name }) => name),
        takeUntil(this.destroy$),
      )
      .subscribe(themeName => this.currentTheme = themeName);
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }

  changeTheme(themeName: string) {
    this.themeService.changeTheme(themeName);
  }

  toggleSidebar(): boolean {
    this.sidebarService.toggle(true, 'menu-sidebar');
    this.layoutService.changeLayoutSize();
    return false;
  }

  logout() {
    this.api.logout()
  }

  navigateHome() {
    this.router.navigateByUrl('/')
    return false;
  }
}
