import { AuthService } from '@/core/services/auth.service';
import { CommonModule } from '@angular/common';
import { Component, computed, inject, Input, TemplateRef } from '@angular/core';
import { RouterModule } from '@angular/router';
import { MenuItem } from 'primeng/api';
import { Button } from 'primeng/button';
import { MenuModule } from 'primeng/menu';
import { StyleClassModule } from 'primeng/styleclass';
import { Toolbar } from 'primeng/toolbar';
import images from 'src/assets/images';
import { LayoutService } from '../service/layout.service';

@Component({
  selector: 'app-topbar',
  standalone: true,
  imports: [RouterModule, CommonModule, StyleClassModule, MenuModule, Button, Toolbar],
  templateUrl: './app.topbar.component.html',
  host: {
    class: 'sticky top-0 z-50',
  },
})
export class AppTopbar {
  @Input() showMenu: boolean = false;
  @Input() startTemplate?: TemplateRef<any> | null = null;
  @Input() centerTemplate?: TemplateRef<any> | null = null;
  @Input() endTemplate?: TemplateRef<any> | null = null;

  constructor(public layoutService: LayoutService) {}
  private authService: AuthService = inject(AuthService);

  readonly logo = images.logo;

  toggleDarkMode() {
    this.layoutService.layoutConfig.update((state) => ({ ...state, darkTheme: !state.darkTheme }));
  }

  username = computed(() => this.authService.currentAccount()?.username || 'کاربر ناشناس');
  panelTitle = computed(
    () => this.layoutService.panelInfo()?.title || 'پنل مدیریت صورت‌حساب‌های مالیاتی',
  );

  logout = () => {
    this.authService.logout();
  };

  profileMenuItems: MenuItem[] = [
    {
      label: this.username(),
      icon: 'pi pi-user',
      disabled: true,
    },
    {
      label: 'راهنمای سامانه',
      icon: 'pi pi-question-circle',
      disabled: true,
    },
    {
      label: 'خروج',
      icon: 'pi pi-sign-out',
      command: this.logout,
    },
  ];
}
