import { Component, ViewChild } from '@angular/core';
import { MatIconModule } from '@angular/material/icon';
import { MatMenuModule, MatMenuTrigger } from '@angular/material/menu';
import { MatButtonModule } from '@angular/material/button';
import { Router } from '@angular/router';
import { AuthService } from '../../auth/authService/auth.service';
import { NotifierService } from '../notifierService/notifier.service';
import { ScoreTableService } from '../../score-table/score-table.service';

@Component({
  selector: 'app-header-menu',
  standalone: true,
  imports: [MatButtonModule, MatMenuModule, MatIconModule],
  templateUrl: './header-menu.component.html',
  styleUrl: './header-menu.component.css',
})
export class HeaderMenuComponent {
  @ViewChild(MatMenuTrigger) trigger!: MatMenuTrigger;

  constructor(
    private router: Router,
    private authService: AuthService,
    private notifierService: NotifierService,
    private scoreTableService: ScoreTableService
  ) {}

  isUserLoggedIn() {
    return this.authService.getCurrentUser();
  }

  isOnScoreTable() {
    return this.router.url === '/score-table';
  }

  navigateToProfileOrScoreboard() {
    this.scoreTableService.setShowUserGames(false);
    if (this.isOnScoreTable()) {
      this.router.navigate(['/user-profile']);
    } else {
      this.router.navigate(['/score-table']);
    }
  }

  login() {
    this.router.navigate(['/login']);
  }

  logout() {
    this.authService.logout();
    this.trigger.closeMenu();
    this.router.navigate(['/score-table']);
    window.location.reload();
    this.notifierService.showNotification('Du wurdest ausgeloggt.', 'OK');
  }
}
