import { Component } from '@angular/core';
import { EN_AppEntities } from '@app_consts';
import { PopupBaseComponent } from '@app_core/components/popup-base/popup-base.component';
import { Department } from '@app_models/basedata/department';
import { WebApp } from '@app_models/basedata/webapp';
import { WebAppToWebAppRole } from '@app_models/basedata/webapp-to-webapprole';
import { WebAppAdditionalRole } from '@app_models/basedata/webappadditionalrole ';
import { EmployeeToWebApp } from '@app_models/employee-to-webapp';
import { EmployeeDataService } from '@app_modules/employee/employee-data.service';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { AppDataService } from '@app_services/app.data.service';

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

export class EmployeeToWebAppPopupeditComponent extends PopupBaseComponent {
  public webAppRoleList: AppBaseEntityListWrapper<WebAppToWebAppRole> = new AppBaseEntityListWrapper<WebAppToWebAppRole>(WebAppToWebAppRole, 'Applikation Rolenliste', EN_AppEntities.WebAppToWebAppRole);
  public additionalRoleList: AppBaseEntityListWrapper<WebAppAdditionalRole> =
    new AppBaseEntityListWrapper<WebAppAdditionalRole>(WebAppAdditionalRole, 'Applikation Rolenliste', EN_AppEntities.WebAppAdditionalRole);

  public departmentIdList: number[] = this.employeeService.employee2DepartmentList.items.map(e2d => e2d.departmentId);

  constructor(
    public appDataService: AppDataService,
    public employeeService: EmployeeDataService,
  ) {
    super();
    this.webAppRoleList.filter = this.appDataService.webAppFilter;
    this.additionalRoleList.filter = this.appDataService.webAppFilter;
    this.loadRoles();
  }


  loadRoles() {
    if (this.focusedItem.webAppId) {
      this.appDataService.webAppFilter.webAppId = this.focusedItem.webAppId;
      this.webAppRoleList.load();
      if (this.focusedItem.isNew() || this.focusedItem.additionalRoleIdList !== null) this.additionalRoleList.load();
    } else {
      this.webAppRoleList.clear();
      this.additionalRoleList.clear();
    }
  }


  public get focusedItem(): EmployeeToWebApp {
    return <EmployeeToWebApp>(this._focusedItem);
  }


  getWebapps = (wa: WebApp) => {
    return this.focusedItem.entityId !== 0 || !this.list.items.find((item) => item['webAppId'] === wa.entityId) && wa.isActive;
  }


  public filterDepartments = (department: Department) => {
    return this.departmentIdList.indexOf(department.departmentId) > -1;
  }

  public save = () => {
    this.focusedItem.webAppName = this.appDataService.webAppList?.getItemById(this.focusedItem.webAppId)?.webAppName;
    this.focusedItem.departmentName = this.appDataService.departmentList?.getItemById(this.focusedItem.departmentId)?.departmentName;
    this.focusedItem.webAppRoleName = this.appDataService.roleList?.getItemById(this.focusedItem.webAppRoleId)?.webAppRoleName;

    this.baseEntityWrapper.save(() => this.close(true));
  }
}
