import { Component, ElementRef, HostListener, ViewChild } from '@angular/core';
import { Phone } from '../phone/phone';
import { Mail } from '../mail/mail';

@Component({
  selector: 'app-header',
  imports: [Phone, Mail],
  templateUrl: './header.html',
  styleUrl: './header.css',
})
export class Header {
  isMenuOpen = false;

  @ViewChild('menuContainer')
  menuContainer!: ElementRef;

  toggleMenu(): void {
    this.isMenuOpen = !this.isMenuOpen;
  }

  closeMenu(): void {
    this.isMenuOpen = false;
  }

  scrollTo(id: string): void {
    document.getElementById(id)?.scrollIntoView({ behavior: 'smooth' });
  }

  @HostListener('document:click', ['$event'])
  onDocumentClick(event: MouseEvent): void {
    if (!this.isMenuOpen) {
      return;
    }

    const clickedInside = this.menuContainer.nativeElement.contains(
      event.target,
    );

    if (!clickedInside) {
      this.closeMenu();
    }
  }

  scrollToTop(): void {
    window.scrollTo({
      top: 0,
      behavior: 'smooth',
    });
  }
}
