import { CommonModule, isPlatformBrowser } from '@angular/common';
import { AfterViewInit, Component, HostListener, PLATFORM_ID, inject } from '@angular/core';
import { ActivatedRoute, NavigationEnd, Router, RouterOutlet } from '@angular/router';
import { initFlowbite } from 'flowbite';
import { filter } from 'rxjs/operators';
import build from '../build';
import { ConfirmationComponent } from './components/confirmation/confirmation.component';
import { ConfirmationService } from './components/confirmation/confirmation.service';
import { EMailComponent } from './components/email/email.component';
import { FooterComponent } from './components/footer/footer.component';
import { HeaderComponent } from './components/header/header.component';
import { MessageContainerComponent } from './components/message/message-container.component';
import { SearchModalCommercialComponent } from './components/search-modal/search-modal-commercial.component';
import { SearchModalComponent } from './components/search-modal/search-modal.component';
import { AuditService } from './services/audit.service';
import { GeoService } from './services/geo.service';
import { LoadingService } from './services/loading.service';
import { UserService } from './services/user.service';

@Component({
  selector: 'app-root',
  standalone: true,
  imports: [CommonModule, RouterOutlet, HeaderComponent, FooterComponent, MessageContainerComponent, SearchModalComponent, SearchModalCommercialComponent, ConfirmationComponent, EMailComponent],
  providers: [],
  templateUrl: './app.component.html',
  styleUrl: './app.component.scss',
})
export class AppComponent implements AfterViewInit {
  build = build;
  title = 'bizmatch';
  actualRoute = '';
  private platformId = inject(PLATFORM_ID);
  private isBrowser = isPlatformBrowser(this.platformId);

  public constructor(
    public loadingService: LoadingService,
    private router: Router,
    private activatedRoute: ActivatedRoute,
    private userService: UserService,
    private confirmationService: ConfirmationService,
    private auditService: AuditService,
    private geoService: GeoService,
  ) {
    this.router.events.pipe(filter(event => event instanceof NavigationEnd)).subscribe(() => {
      let currentRoute = this.activatedRoute.root;
      while (currentRoute.children[0] !== undefined) {
        currentRoute = currentRoute.children[0];
      }
      // Hier haben Sie Zugriff auf den aktuellen Route-Pfad
      this.actualRoute = currentRoute.snapshot.url[0].path;

      // Re-initialize Flowbite after navigation to ensure all components are ready
      if (this.isBrowser) {
        setTimeout(() => {
          initFlowbite();
        }, 50);
      }
    });
  }
  ngOnInit() {
    // Navigation tracking moved from constructor
  }

  ngAfterViewInit() {
    // Initialize Flowbite for dropdowns, modals, and other interactive components
    // Note: Drawers work automatically with data-drawer-target attributes
    if (this.isBrowser) {
      initFlowbite();
    }
  }

  @HostListener('window:keydown', ['$event'])
  handleKeyboardEvent(event: KeyboardEvent) {
    if (event.shiftKey && event.ctrlKey && event.key === 'V') {
      this.showVersionDialog();
    }
  }
  showVersionDialog() {
    this.confirmationService.showConfirmation({ message: `App Version: ${this.build.timestamp}`, buttons: 'none' });
  }
  isFilterRoute(): boolean {
    const filterRoutes = ['/businessListings', '/commercialPropertyListings', '/brokerListings'];
    return filterRoutes.includes(this.actualRoute);
  }
}
