import { Component, OnInit, OnDestroy, ChangeDetectorRef } from '@angular/core';

import { of, Subscription } from 'rxjs';
import { map, catchError, take } from 'rxjs/operators';

import { ElectronProxyService } from '../commons/service/electron-proxy.service';
import { ProbeService } from '../commons/service/probe.service';
import { MenuEvent } from '../commons/type';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit, OnDestroy {
  title = 'scanner-app';
  showTitleBar: boolean;
  block: boolean;

  menuSubscription: Subscription;
  displayAbout: boolean;
  displayExportCSV: boolean;
  displayPreferences: boolean;
  displayPrint: boolean;

  public constructor(
    private changeDetector: ChangeDetectorRef,
    private electronProxyService: ElectronProxyService,
  ) {
    // this.showTitleBar = !__LINUX__;
    this.showTitleBar = false;
    this.block = false;
  }

  ngOnInit(): void {
    // this.probeService.connect();
    this.electronProxyService.sendReady(performance.now());
    this.menuSubscription = this.electronProxyService.menuObservable()
      .pipe(
        map((name: MenuEvent) => {
          switch (name) {
            case 'show-about':
              this.displayAbout = true;
              break;
            case 'show-export-csv':
              this.displayExportCSV = true;
              break;
            case 'show-preferences':
              this.displayPreferences = true;
              break;
            case 'show-print':
              this.displayPrint = true;
              break;
            default:
              break;
          }
          this.changeDetector.detectChanges();
        }),
        catchError(error => {
          return of();
        }),
      )
      .subscribe();

  }

  ngOnDestroy(): void {
    this.menuSubscription.unsubscribe();
  }

  // onMenuClosed(event) {
  //   this.changeDetector.markForCheck();
  // }
}
