import { Component, OnInit, ViewChild, OnDestroy } from '@angular/core';
import { ErrorComponent } from '../../core/error/component/error.component';
import { Subject, Observable } from 'rxjs';
import { ErrorService } from '../../core/error/component/error.service';
import { takeUntil, distinctUntilChanged, map } from 'rxjs/operators';
import { ModuleSwitcher } from '../../core/models/app-switcher.enum';
import { ModuleSwitcherService } from '../../core/services/module-switcher.service';
import { RemissionLeaveDialogComponent } from '../../modules/remission/components/remission-leave-dialog/remission-leave-dialog.component';
import { AppService } from '../../core/services/app.service';
import { LocationService } from '../../core/services/location-service';
// tslint:disable-next-line: max-line-length
import { RemissionReminderDialogComponent } from '../../modules/remission/components/remission-reminder-dialog/remission-reminder-dialog.component';
// tslint:disable-next-line: max-line-length
import { RemissionStartedLeaveDialogComponent } from '../../modules/remission/components/remission-started-leave-dialog/remission-started-leave-dialog.component';

@Component({
  selector: 'app-content',
  templateUrl: './content.component.html',
  styleUrls: ['./content.component.scss'],
})
export class ContentPageComponent implements OnInit, OnDestroy {
  @ViewChild(ErrorComponent) element: ErrorComponent;
  @ViewChild(RemissionStartedLeaveDialogComponent)
  remissionStartedLeaveDialog: RemissionStartedLeaveDialogComponent;
  @ViewChild(RemissionLeaveDialogComponent)
  remissionLeavingDialog: RemissionLeaveDialogComponent;
  @ViewChild(RemissionReminderDialogComponent)
  remissionReminderDialog: RemissionReminderDialogComponent;
  module: ModuleSwitcher = ModuleSwitcher.Customer;
  destroy$ = new Subject();
  showBreadcrumbs$: Observable<boolean>;
  showFilter$: Observable<boolean>;

  constructor(private errorService: ErrorService, private moduleSwitcherService: ModuleSwitcherService, private appService: AppService) {
    this.errorService
      .getErrors()
      .pipe(takeUntil(this.destroy$))
      .subscribe((errors) => {
        this.element.openErrorModal(errors);
      });

    this.appService.openStartedRemissionLeavingDialog$.pipe(takeUntil(this.destroy$)).subscribe(() => {
      this.remissionStartedLeaveDialog.openDialog();
    });

    this.appService.leavingRemission$.pipe(takeUntil(this.destroy$)).subscribe(() => {
      this.remissionLeavingDialog.openDialog();
    });

    this.appService.remissionReminder$.pipe(takeUntil(this.destroy$)).subscribe(() => {
      this.remissionReminderDialog.openDialog();
    });
  }

  ngOnInit() {
    this.moduleSwitcherService.moduleSwitcher$
      .pipe(
        takeUntil(this.destroy$),
        distinctUntilChanged((prev, curr) => (prev && curr ? prev === curr : false))
      )
      .subscribe((activeModule: ModuleSwitcher) => {
        this.module = activeModule;
      });
  }

  ngOnDestroy(): void {
    this.destroy$.next();
  }
}
