import { Component, OnInit, Input, ChangeDetectorRef, DoCheck, ChangeDetectionStrategy, Output, EventEmitter, ViewChild, ElementRef, AfterViewInit, HostListener } from '@angular/core';
import { AppBaseEntityWrapper } from '@app_services/app.baseentity.wrapper';
import { DepartmentDataService } from '../department-data.service';
import { WindreamSearchToDepartment } from '@app_models/windream-search-to-department';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { WindreamIndexToWindreamSearchToDepartment } from '@app_models/windream-index-to-windream-search-to-department';
import { Department } from '@app_models/basedata/department';
import { WindreamSearchItemToWindreamSearchToDepartment } from '@app_models/windream-search-item-to-windream-search-to-department';
import { WindreamSearchItem } from '@app_models/windream-search-item';
import { NgForm } from '@angular/forms';
import { reduce } from 'rxjs/operators';
import { AuthorizeService } from '@app_core/services/authorize.service';
import { PageLoadingService } from '@app_core/services/pageloading.service';
import { EN_EntityBeforeSaveCallBackResults } from '@app_core/consts';
import { LoginPopupComponent } from '@app_core/components/login/login-popup/login-popup.component';

@Component({
  selector: 'app-department-detail',
  templateUrl: './department-detail.component.html',
  styleUrls: ['./department-detail.component.scss'],
})
export class DepartmentDetailComponent implements AfterViewInit {

  @ViewChild('detailForm') detailForm: NgForm;

  @Input() public departmentDetails: AppBaseEntityWrapper<Department>;

  public get windreamSearchToDepartmentDetails(): AppBaseEntityWrapper<WindreamSearchToDepartment> {
    return this.departmentDataService.windreamSearchToDepartmentDetails;
  }

  constructor(
    public departmentDataService: DepartmentDataService,
    public authorizeService: AuthorizeService,
    private pageLoadingService: PageLoadingService,
  ) {
  }
  public get windreamSearchList(): AppBaseEntityListWrapper<WindreamSearchToDepartment> {
    return this.departmentDataService.windreamSearchToDepartmentList;
  }
  public get windreamSearchDetails(): AppBaseEntityWrapper<WindreamSearchToDepartment> {
    return this.departmentDataService.windreamSearchToDepartmentDetails;
  }


  public get windreamSearchItemToWindreamSearchToDepartmentList(): AppBaseEntityListWrapper<WindreamSearchItemToWindreamSearchToDepartment> {
    return this.departmentDataService.windreamSearchItemToWindreamSearchToDepartmentList;
  }
  public get windreamSearchItemToWindreamSearchToDepartmentDetails(): AppBaseEntityWrapper<WindreamSearchItemToWindreamSearchToDepartment> {
    return this.departmentDataService.windreamSearchItemToWindreamSearchToDepartmentDetails;
  }
  public get windreamSearchItemList(): AppBaseEntityListWrapper<WindreamSearchItem> {
    return this.departmentDataService.windreamSearchItemList;
  }


  public get windreamIndexList(): AppBaseEntityListWrapper<WindreamIndexToWindreamSearchToDepartment> {
    return this.departmentDataService.windreamIndexToWindreamSearchToDepartmentList;
  }
  public get windreamIndexDetails(): AppBaseEntityWrapper<WindreamIndexToWindreamSearchToDepartment> {
    return this.departmentDataService.windreamIndexToWindreamSearchToDepartmentDetails;
  }


  public get focusedItem(): Department {
    return this.departmentDetails.entity;
  }

  ngAfterViewInit(): void {
    this.departmentDetails.beforeNewCallBack = this.newDepartmentInit.bind(this);
    this.departmentDetails.beforeSaveCallBack = this.beforeSaveDepartment.bind(this);
  }

  newDepartmentInit(department: Department) {
    this.departmentDataService.selectedDocumentarts = [];
    department.departmentTypeId = 1;
    department.clientId = 24110;
  }

  async beforeSaveDepartment(department: Department): Promise<EN_EntityBeforeSaveCallBackResults> {
    const maxId: number = this.departmentDataService.departmentList.items.reduce((max, currentItem) => (currentItem.entityId > max ? currentItem.entityId : max), 0);
    if (department.isNew()) department.departmentId = -(maxId + 1);
    this.departmentDataService.documentArtToDepartmentList.match2KeysArray(department, 'departmentId', this.departmentDataService.selectedDocumentarts, 'documentArtId');
    return EN_EntityBeforeSaveCallBackResults.Ok_Continue;
  }


  public save = () => { //because of calling as callback of cancel
    this.pageLoadingService.startSpinner();
    this.departmentDetails.save((department: Department) => this.afterSave(department), null, () => this.pageLoadingService.hideSpinner());
  }

  afterSave(department: Department) {
    // this.departmentDetails.loadDetails(this.catalogDataService.departmentCategoryList);

  }

  public cancel = () => {
    // this.setCurrentMaster4Subdepartment = null;
    this.departmentDetails.cancelWithSave(this.save);
  }


  @HostListener('window:keydown', ['$event'])
  keyboardInput(event: any) {
    return LoginPopupComponent.LOGIN_IS_SHOWN || this.departmentDetails.shortcutsHandler(event, this.save, this.cancel);
  }

}
