import { Component, OnInit, OnDestroy, effect, computed, Signal, } from '@angular/core';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common';
import { MenuItem } from 'primeng/api';
import { MenuModule } from 'primeng/menu';
import { ButtonModule} from 'primeng/button';
import { ButtonGroupModule } from 'primeng/buttongroup';
import { Subscription } from 'rxjs';
import { AuthenticationService } from '../user-services';
import { Utils } from '../shares';
import { User } from '../models';


@Component({
    selector: 'app-toolbar',
    imports: [CommonModule, MenuModule, ButtonModule, ButtonGroupModule],
    templateUrl: './toolbar.component.html',
    styleUrl: './toolbar.component.css'
})
export class ToolbarComponent implements OnInit, OnDestroy {
  title = 'Safe Assessment Unit UI';
  isAuth = false;
  hasRoleAdmin = false;
  hasRoleReport = false;
  homeUrl = "/person";
  oneMenu: MenuItem[] = [];
  reportMenu: MenuItem[] = [];
  systemMenu: MenuItem[] = [];
  currentUserName = "";
  currentUserS: Signal<User> = computed (() => { 
    const currentUser = this.authenticationService.authChange();
    this.isAuth =false;
    if (currentUser.username) {
          this.isAuth =true;
          this.assignRole(currentUser);
          console.log(`${this.title} on auth currentUser signal now ${this.isAuth}`);
          // add this to the one menu
          if (this.hasRoleAdmin) {

            let i = 0;
            for (i = 0; i < this.systemMenu.length; i++) {
              this.oneMenu.push(this.systemMenu[i]);
            }

          }
          //add the logout here
          this.oneMenu.push(
            {
              label: 'Logout', icon: 'pi pi-power-off', command: () => {
                this.logout()
              }
            }
          );
        }
      return currentUser;
      });

  private subscription: Subscription = new Subscription();
  constructor(
    private router: Router,
    private authenticationService: AuthenticationService
  ) {
    this.authenticationService.isReport = false;
    this.authenticationService.isHome = true;
  }
  onHome(): void {
    this.authenticationService.isHome = true;
    this.router.navigate([this.homeUrl]);
  }
  get isHome(): boolean {
    return this.authenticationService.isHome;
  }
  get loginUser(): string {
    let result = "";
    let greeting = "Good ";
    const d = new Date();
    const hour = d.getHours();
    if (hour >= 5 && hour < 12)
      greeting = "Good morning, ";
    else if (hour >= 12 && hour < 17)
      greeting = "Good afternoon, ";
    else if (hour >= 17 && hour < 21)
      greeting = "Good evening, ";
    else
      greeting = "Good night, ";
    if (this.isAuth) {
      const user = Utils.getCurrentUser();
      result = user.firstName;
    }
    return greeting + result;
  }
  get isReport(): boolean {

    return this.authenticationService.isReport;
  }
  initOneMenu(): void {
    this.oneMenu = [
      {
        label: 'Home', icon: 'pi pi-slack', command: () => {
          this.router.navigate([this.homeUrl]);

        }
      }
    ];
  }
  initSystemMenu(): void {
    this.systemMenu = [
/*
      {
        label: 'Lookup', icon: 'pi pi-bars', command: () => {
          this.router.navigate(['/lookup']);
          this.authenticationService.isReport = false;
          this.authenticationService.isHome = false;
        }
      },
      {
        label: 'Waiting List', icon: 'pi pi-map-marker', command: () => {
          this.router.navigate(['/waitinglist']);
          this.authenticationService.isReport = false;
          this.authenticationService.isHome = false;
        }
      },
      */
      {
        label: 'Person', icon: 'pi pi-users',
        command: () => {
          this.router.navigate(['/person']);

        }
      },
      {
        label: 'Import', icon: 'pi pi-users',
        command: () => {
          this.router.navigate(['/import']);

        }
      },
       {
        label: 'Family tree', icon: 'pi pi-users',
        command: () => {
          this.router.navigate(['/familytree']);

        }
      },
     
      {
        label: 'Staff', icon: 'pi pi-users',
        command: () => {
          this.router.navigate(['/staff']);

        }
      },

    ];
  }

  logout(): void {
    this.initOneMenu();
    this.authenticationService.logout();
    this.router.navigate(['/login']);
  }
  assignRole(user: User): void {
    const userRole = Utils.getUserRole(user);
    this.hasRoleReport = Utils.canRunReport(userRole);
    this.currentUserName = user.firstName;
    //this.hasRoleAdmin = true;
    this.hasRoleAdmin = userRole == Utils.getAdminRoleName();
    //console.log(this.msg + " hasRoleAdmin ", this.hasRoleAdmin);

  }
  ngOnInit(): void {
    this.initOneMenu();
    this.initSystemMenu();
    console.log("ngOnInit toolbar called");
   // this.initStart();
  }
 
  ngOnDestroy(): void {
    this.subscription.unsubscribe();
  }
}
