import { Component, NgZone, ViewChild } from '@angular/core';
import { MatSidenav, MatSidenavModule } from '@angular/material/sidenav';
import { NavigationStart, Router, RouterLink, RouterOutlet } from '@angular/router';
import { MatMenuModule } from '@angular/material/menu';
import packageInfo from 'package.json';
import { delay } from 'rxjs';
import { environment } from 'src/environments/environment';
import { AuthService } from '../../auth/auth.service';
import { DataService } from '../../services/data.service';
import { DeviceService } from '../../services/device.service';
import { MatDividerModule } from '@angular/material/divider';
import { MatListModule } from '@angular/material/list';
import { MatButtonModule } from '@angular/material/button';
import { MatToolbarModule } from '@angular/material/toolbar';
import { MatIconModule } from '@angular/material/icon';
import { NgIf, DatePipe } from '@angular/common';
import { MainMenuComponent } from 'src/app/components/main-menu/main-menu.component';
import moment from "moment";

@Component({
    selector: 'diunabi-main-view',
    templateUrl: './main-view.component.html',
    styleUrls: ['./main-view.component.scss'],
    standalone: true,
    imports: [NgIf, MatIconModule, MatToolbarModule, MatButtonModule, MatSidenavModule,
      MatListModule, RouterLink, MatDividerModule, RouterOutlet, DatePipe, MatMenuModule, MainMenuComponent]
})
export class MainViewComponent {
  @ViewChild('snav') snav?: MatSidenav;
  appVersion = packageInfo.azureBuild;

  currentDate = moment().toDate();
  flipPhone = false;
  loading = false;
  environment = environment;

  constructor(
    public data$: DataService,
    public device$: DeviceService,
    private router$: Router,
    private ngZone$: NgZone,
    public auth$: AuthService,
  ) {
    this.router$.events.subscribe((event) => {
      if (event instanceof NavigationStart && device$.isMobile()) {
        this.snav?.close();
      }
    });

    //listen to loading subject
    data$.showLoader.pipe(delay(0)).subscribe(loading => {
      this.ngZone$.run(() => {
        this.loading = loading;
      });
    });
  }
  logout() {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    google.accounts.id.disableAutoSelect();
    window.location.reload();
  }
}
