import { Component, Input, Output, EventEmitter, ViewChildren, QueryList } from '@angular/core';
import { StateService } from '../../services/state.service';
import { ActivatedRoute } from '@angular/router';
import { take, first, takeUntil } from 'rxjs/operators';
import { NbDialogService, NbTooltipDirective } from '@nebular/theme';
import { emit } from 'cluster';
import { MsgBoxService } from '../../services/msg-box.service';
import { RbjDialogService } from '../../services/rbj-dialog.service';
import { formatCurrency } from '@angular/common';
import { Subject } from 'rxjs';
@Component({
  selector: 'rbj-screen-footer',
  templateUrl: './screen-footer.component.html',
  styleUrls: ['./screen-footer.component.scss']
})
export class ScreenFooterComponent {

  @ViewChildren(NbTooltipDirective) tooltips: QueryList<NbTooltipDirective>;
  private _disableSave: boolean;

  page: number;
  pages: number[] = [];
  maxPage: number = Number.POSITIVE_INFINITY; // default no limit
  minPage: number = 1;
  toolTips: string[] = [];
  private destroy$: Subject<void> = new Subject<void>();

  @Output() focusFirstInvalid = new EventEmitter();

  public get disableSave(): boolean {
    return this._disableSave;
  }
  @Input()
  public set disableSave(v: boolean) {
    if (this._disableSave != v) {

      this._disableSave = v;
      if (true === this._disableSave && this.tooltips) {
        this.tooltips.forEach(element => { element.hide(); });
      }
      this.stateService.dataChanged = !v;
    }
  }

  @Input() isPagedScreen: boolean


  public get waitingForScreenLock(): boolean {
    return this.stateService.waitingForScreenLock;
  }


  constructor(
    private stateService: StateService,
    private route: ActivatedRoute,
    private dialogService: RbjDialogService,
    private msgBoxService: MsgBoxService
  ) {
    this.stateService.setPagesSubject.pipe(takeUntil(this.destroy$)).subscribe({ next: (pages) => this.setupPages(pages) });
    this.stateService.setToolTipsSubject.pipe(takeUntil(this.destroy$)).subscribe({ next: (toolTips) => this.setupToolTips(toolTips) });
    this.stateService.setPageMaxSubject.pipe(takeUntil(this.destroy$)).subscribe({ next: (maxPage) => this.maxPage = maxPage });
    this.stateService.setPageMinSubject.pipe(takeUntil(this.destroy$)).subscribe({ next: (minPage) => this.minPage = minPage });
    this.stateService.setPageDataSubject.pipe(takeUntil(this.destroy$)).subscribe({ next: (page) => this.page = page });

  }

  ngOnDestroy(): void {
    this.destroy$.next();
    this.destroy$.complete();
  }
  setupPages(pages: number[]) {
    this.page = 1;
    this.route.queryParamMap
      .pipe(take(1))
      .subscribe(paramMap => {
        if (paramMap.has('page')) {
          this.page = parseInt(paramMap.get('page'));
        }
      });
    this.pages = pages;
  }

  setupToolTips(toolTips: string[]) {
    this.toolTips = toolTips;
  }

  gotoPage(page: number) {

    this.page = page;
    this.stateService.gotoPage(this.page);
  }

  nextScreen() {
    if (false == this.stateService.checkInvalidFields()) {
      //this.stateService.nextScreen();
    }
  }

  prevScreen() {
    if (false == this.stateService.checkInvalidFields()) {
      //this.stateService.prevScreen();
    }
  }

  createNewPage(page: number) {
    //this.stateService.createPage(page);
  }

  save() {
    if (false == this.stateService.checkInvalidFields() && false == (this.disableSave || this.disableSave)) {
      this.stateService.save();
    }
  }

  cancel() {


  }
  reload() {
    this.stateService.cancel();
  }
}
