import { Component, Input, OnInit } from '@angular/core';
import { EN_AppEntities } from '@app_consts';
import { Department } from '@app_models/basedata/department';
import { WindreamSearchToDepartment } from '@app_models/windream-search-to-department';
import { DepartmentDataService } from '@app_modules/department/department-data.service';
import { AppBaseEntityWrapper } from '@app_services/app.baseentity.wrapper';
import { AppBaseEntityListWrapper } from '@app_services/app.baseentitylist.wrapper';
import { DepartmentWindreamSearchPopupeditComponent } from './department-windream-search-popupedit/department-windream-search-popupedit.component';
import { SelectDepartmentPopupComponent } from './select-department-popup/select-department-popup.component';
import { ColumnConfigList } from '@app_core/components/grid-config/columnconfiglist';
import { MatDialogService } from '@app_core/services/mat-dialog.service';
import { RepositoryService } from '@app_core/services/http/repository.service';
import { LocaleService } from '@app_core/services/localization/locale.service';
import { Globals } from '@app_core/services/globals';
import { ColumnConfig, EN_SortOrder } from '@app_core/components/grid-config/columnconfig';

@Component({
  selector: 'app-department-windream-search',
  templateUrl: './department-windream-search.component.html',
  styleUrls: ['./department-windream-search.component.scss']
})
export class DepartmentWindreamSearchComponent implements OnInit {

  @Input() windreamSearchList: AppBaseEntityListWrapper<WindreamSearchToDepartment>;
  @Input() departmentDetails: AppBaseEntityWrapper<Department>;


  public columnConfigList: ColumnConfigList = new ColumnConfigList();

  constructor(
    private dialog: MatDialogService,
    public departmentDataService: DepartmentDataService,
    public repoService: RepositoryService,
    public localeService: LocaleService,
    public globals: Globals,
  ) {
  }

  ngOnInit() {
    this.initColumns();
  }

  initColumns() {
    let col: ColumnConfig;
    col = this.columnConfigList.addNumber('Seq.', 'seq', 2);
    col.sortIndex = 0;
    col.sortOrder = EN_SortOrder.asc;
    col = this.columnConfigList.add('Suche Kachel', 'windreamSearchName', 10);
    col = this.columnConfigList.addBoolean('Active', 'isActive', 2);
    col.cellTemplate = 'checkbox_template';
    this.columnConfigList.recalcWidth();
  }

  addItem() {
    this.windreamSearchList.focusedEntityShadowed.new();
    this.windreamSearchList.focusedEntityShadowed.entity.departmentId = this.departmentDetails.entityId;
    this.openPopupDialog(this.windreamSearchList.focusedEntityShadowed);
  }

  public openPopupDialog(editEntity: AppBaseEntityWrapper<WindreamSearchToDepartment>) {
    this.departmentDetails.keyDownListenerStopped = true;
    this.dialog.openDialog(DepartmentWindreamSearchPopupeditComponent, {
      baseEntityWrapper: editEntity,
      keyItemName: this.departmentDetails.entity.departmentName,
      readOnly: !this.departmentDetails.inEditMode,
      description: editEntity.entity.entitytitle + (editEntity.entity.entityId === 0 ? ' zuweisen' : ' bearbeiten '),
      list: this.windreamSearchList,
    }, true, () => this.departmentDetails.keyDownListenerStopped = false);
  }


  public get nameOfDepartment(): string {
    return this.departmentDetails.entity.departmentName;
  }


  public changeKeyWS(wsTD: WindreamSearchToDepartment) {
    if (this.departmentDetails.inEditMode) {
      wsTD.isActive = !wsTD.isActive;
      wsTD.entityChanged = true;
      wsTD.save(this.repoService, null);
    }
  }


  public copyAllTiles() {
    this.openSelectDepartmentsPopup(`Wählen Sie Abteilungen, bei welchen alle Suchkacheln von Suchkacheln aus "${this.departmentDetails.entity.departmentName}" ersetzt werden sollen`
      , 'content_copy'
      , (depIds: number[]) => {
        this.repoService.putDataById(EN_AppEntities.DepartmentCopyWindreamTiles, this.departmentDetails.entityId, depIds).subscribe(
          () => this.departmentDetails.informationMessageBox('Kopieren von Suchkacheln', `Die Suchkacheln aus "${this.departmentDetails.entity.departmentName}" haben Suchkacheln aus den ausgewählten Abteilungen erfolgreich ersetzt`)
        );
      }
    );
  }


  public copyCurrentTile() {
    this.openSelectDepartmentsPopup(`Wählen Sie Abteilungen, für welche die Suchkachel "${this.windreamSearchList.focusedItem.windreamSearchName}" aus "${this.departmentDetails.entity.departmentName}" hinzugefügt werden soll`
      , 'content_copy'
      , (depIds: number[]) => {
        this.repoService.putDataById(EN_AppEntities.WindreamSearchToDepartmentCopyWindreamTile, this.windreamSearchList.focusedItemId, depIds).subscribe(
          () => this.departmentDetails.informationMessageBox('Kopieren von Suchkacheln', `Die Suchkachel "${this.windreamSearchList.focusedItem.windreamSearchName}" aus "${this.departmentDetails.entity.departmentName}" wurde zu den ausgewählten Abteilungen erfolgreich hinzugefügt`)
        );
      }
    );
  }


  public openSelectDepartmentsPopup(text: string, iconanme: string, callBack: (depIds: number[]) => void) {
    this.departmentDetails.keyDownListenerStopped = true;
    this.dialog.openDialog(SelectDepartmentPopupComponent
      , { text: text, iconanme: iconanme }
      , true
      , (result) => {
        if (result && callBack) callBack(result);
        this.departmentDetails.keyDownListenerStopped = false;
      });
  }

}
