

import * as keycode from '@angular/cdk/keycodes';
import { Component, HostListener, Input, OnInit } from '@angular/core';
import { EN_EmployeeStatus } from '@app_consts';
import { ColumnConfig, EN_SortOrder } from '@app_core/components/grid-config/columnconfig';
import { ColumnConfigList } from '@app_core/components/grid-config/columnconfiglist';
import { Globals } from '@app_core/services/globals';
import { LocaleService } from '@app_core/services/localization/locale.service';
import { MatDialogService } from '@app_core/services/mat-dialog.service';
import { Employee } from '@app_models/employee';
import { EmployeeToDepartment } from '@app_models/employee-to-department';
import { EmployeeToWebApp } from '@app_models/employee-to-webapp';
import { EmployeeDataService } from '@app_modules/employee/employee-data.service';
import { AppBaseEntityWrapper } from '@app_services/app.baseentity.wrapper';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { AppDataService } from '@app_services/app.data.service';
import { EmployeeToWebAppPopupeditComponent } from './employee-webapp.popupedit/employee-webapp.popupedit.component';


@Component({
  selector: 'app-employee-webapp',
  templateUrl: './employee-webapp.component.html',
  styleUrls: ['./employee-webapp.component.scss']
})

export class EmployeeWebappComponent implements OnInit {

  public get employeeWebappList(): AppBaseEntityListWrapper<EmployeeToWebApp> {
    return this.employeeDataService.employee2WebAppList;
  }

  @Input() public employeeDetails: AppBaseEntityWrapper<Employee>;

  public columnConfigList: ColumnConfigList = new ColumnConfigList();

  constructor(
    private dialog: MatDialogService,
    public employeeDataService: EmployeeDataService,
    public appDataService: AppDataService,
    public localeService: LocaleService,
    public globals: Globals,
  ) {
  }

  ngOnInit() {
    this.initColumns();
  }

  initColumns() {
    let col: ColumnConfig;

    if (this.appDataService.showIds) col = this.columnConfigList.addNumber('Id', 'entityId', 7, 0);
    col = this.columnConfigList.add('Webapp', 'webAppName', 15);
    // col = this.columnConfigList.addId('Webapp', 'webAppId', 15);
    // col.calculateSortValue = 'webAppName';
    col.sortIndex = 1;
    col.sortOrder = EN_SortOrder.asc;

    col = this.columnConfigList.add('Abteilung', 'departmentName', 20);
    // col = this.columnConfigList.add('Abteilung', 'departmentId', 20);
    // col.calculateSortValue = 'departmentName';
    col = this.columnConfigList.add('Role', 'webAppRoleName', 15);
    // col = this.columnConfigList.add('Role', 'webAppRoleId', 15);
    // col.calculateSortValue = 'webAppRoleName';
    col = this.columnConfigList.add('Zusatzabteilungen', 'extendedDepartmentNameList', 30);
    col = this.columnConfigList.add('Zusatzrolen', 'additionalRoleNameList', 30);
    this.columnConfigList.recalcWidth();
  }

  addItem() {
    this.employeeWebappList.focusedEntityShadowed.new();
    this.employeeWebappList.focusedEntityShadowed.entity.employeeId = this.employeeDetails.entityId;
    if (this.employeeDataService.employee2DepartmentList.items.length > 0) {
      let mainDepId: number = 0;
      const addDepIds: number[] = [];
      let addDepNames = '';
      this.employeeDataService.employee2DepartmentList.items.forEach((e2d: EmployeeToDepartment) => {
        if (e2d.employeeStatusId === EN_EmployeeStatus.MainFunction) {
          if (mainDepId === 0) mainDepId = e2d.departmentId;
        } else {
          addDepIds.push(e2d.departmentId);
          const depname = this.appDataService.departmentList?.items.find(dep => dep.entityId === e2d.departmentId)?.departmentName;
          addDepNames += depname ? depname + ', ' : '';
        }
      });
      if (mainDepId > 0) this.employeeWebappList.focusedEntityShadowed.entity.departmentId = mainDepId;
      this.employeeWebappList.focusedEntityShadowed.entity.arExtendedDepartmentIdList = addDepIds;
      this.employeeWebappList.focusedEntityShadowed.entity.extendedDepartmentNameList = addDepNames.slice(0, -2);
    }
    this.openPopupDialog(this.employeeWebappList.focusedEntityShadowed);
  }

  dblClick(event: any) {
    if (!this.employeeDetails.entityIsLoading) this.employeeWebappList.focusedEntityShadowed.delayedEdit(event.data['entityId'], this.editItem);
  }

  editItem = () => {
    this.employeeWebappList.focusedEntityShadowed.edit();
    this.openPopupDialog(this.employeeWebappList.focusedEntityShadowed);
  }

  public openPopupDialog(editEntity: AppBaseEntityWrapper<EmployeeToWebApp>) {
    this.employeeDetails.keyDownListenerStopped = true;
    this.dialog.openDialog(EmployeeToWebAppPopupeditComponent, {
      baseEntityWrapper: editEntity,
      keyItemName: this.employeeDetails.entity.firstName + ' ' + this.employeeDetails.entity.lastName,
      readOnly: !this.employeeDetails.inNotViewMode || !this.employeeDetails.entity.isActive,
      description: (editEntity.entity.entityId === 0 ? 'Neue Webbapp zuweisen' : 'Daten zur Webbapp "' + editEntity.entity.webAppName + '" bearbeiten '),
      list: this.employeeWebappList,
    }, true, () => this.employeeDetails.keyDownListenerStopped = false);
  }

  @HostListener('keydown', ['$event'])
  onKeyDown(event: any) {
    switch (event. which) {
      case keycode.ENTER:
        return false;
    }
    return true;
  }


}
