import {Component, CUSTOM_ELEMENTS_SCHEMA, inject, OnInit} from '@angular/core';
import {BreadcrumbComponent} from "../../components/breadcrumb/breadcrumb.component";
import {NavBarComponent} from "../../layouts/admin-layout/nav-bar/nav-bar.component";
import {NavigationComponent} from "../../layouts/admin-layout/navigation/navigation.component";
import {Router, RouterLink, RouterOutlet} from "@angular/router";
import {DOCUMENT, NgClass, NgStyle} from '@angular/common';
import {MatDrawerContainer, MatSidenavModule} from '@angular/material/sidenav';
import {MatButton} from '@angular/material/button';
import { ScrollPanelModule } from 'primeng/scrollpanel';
import {MenuItem} from 'primeng/api';
import {PanelMenu} from 'primeng/panelmenu';
@Component({
  selector: 'app-admin.component',
  imports: [
    BreadcrumbComponent,
    NavBarComponent,
    NavigationComponent,
    RouterOutlet,
    NgClass,
    MatDrawerContainer,
    MatSidenavModule,
    ScrollPanelModule,
    MatButton,
    RouterLink,
    PanelMenu,
    NgStyle,

  ],
  schemas:[CUSTOM_ELEMENTS_SCHEMA],
  templateUrl: './admin.component.html',
  styleUrl: './admin.component.scss'
})
export class AdminComponent implements OnInit {
  private document = inject<Document>(DOCUMENT);
  height = 0;

  items: MenuItem[] | undefined;


  constructor(private router: Router) {
    this.height = 0


  }

  // public props
  navCollapsed: boolean = false;
  navCollapsedMob: boolean = false;

  // public method
  navMobClick() {
    if (this.navCollapsedMob && !document.querySelector('app-navigation.pc-sidebar')?.classList.contains('mob-open')) {
      this.navCollapsedMob = !this.navCollapsedMob;
      setTimeout(() => {
        this.navCollapsedMob = !this.navCollapsedMob;
      }, 100);
    } else {
      this.navCollapsedMob = !this.navCollapsedMob;
    }
    if (document.querySelector('app-navigation.pc-sidebar')?.classList.contains('navbar-collapsed')) {
      document.querySelector('app-navigation.pc-sidebar')?.classList.remove('navbar-collapsed');
    }
  }

  handleKeyDown(event: KeyboardEvent): void {
    if (event.key === 'Escape') {
      this.closeMenu();
    }
  }

  closeMenu() {
    if (document.querySelector('app-navigation.pc-sidebar')?.classList.contains('mob-open')) {
      document.querySelector('app-navigation.pc-sidebar')?.classList.remove('mob-open');
    }
  }

  ngOnInit() {

    if(this.document.defaultView !== null || this.document.defaultView !== undefined) {
      // @ts-ignore
      this.height = this.document.defaultView.innerHeight;
    }
    else
      this.height = 0;

    this.items = [
      {
        label: 'Admin',
        icon: 'pi pi-palette',
        command: () => {
          this.router.navigate(['/main/admin/welcome']);
        },
        items: [
          {
            label: 'Settings',
            icon: 'pi pi-eraser',
            // route: '/main/admin/settings'
            command: () => {
              this.router.navigate(['/main/admin/settings']);
            }
          },
          {
            label: 'System',
            icon: 'pi pi-heart',
            route: '/main/admin/system'
          }
        ]
      }
    ];
  }

}
