import { Component, Input, OnInit } from '@angular/core';
import { Department } from '@app_models/basedata/department';
import { WindreamIndexToWindreamSearchToDepartment } from '@app_models/windream-index-to-windream-search-to-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 { DepartmentWindreamIndexPopupeditComponent } from './department-windream-index-popupedit/department-windream-index-popupedit.component';
import { ColumnConfigList } from '@app_core/components/grid-config/columnconfiglist';
import { MatDialogService } from '@app_core/services/mat-dialog.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-index',
  templateUrl: './department-windream-index.component.html',
  styleUrls: ['./department-windream-index.component.scss']
})
export class DepartmentWindreamIndexComponent implements OnInit {
  @Input() windreamIndexList: AppBaseEntityListWrapper<WindreamIndexToWindreamSearchToDepartment>;
  @Input() windreamIndexDetails: AppBaseEntityWrapper<WindreamIndexToWindreamSearchToDepartment>;
  @Input() windreamSearchToDepartmentDetails: AppBaseEntityWrapper<WindreamSearchToDepartment>;
  @Input() departmentDetails: AppBaseEntityWrapper<Department>;


  public get windreamSearchName(): string {
    return this.windreamSearchToDepartmentDetails.entity.windreamSearchName;
  }


  public columnConfigList: ColumnConfigList = new ColumnConfigList();
  constructor(
    private dialog: MatDialogService,
    public departmentDataService: DepartmentDataService,
    public localeService: LocaleService,
    public globals: Globals,
  ) {
  }

  ngOnInit() {
    this.initColumns();
  }
  initColumns() {
    let col: ColumnConfig;
    col = this.columnConfigList.add('Seq.', 'seq', 2);
    col.sortIndex = 0;
    col.sortOrder = EN_SortOrder.asc;
    col = this.columnConfigList.add('Ausgabespalte', 'objectTypeAttributeSzName', 15);
    col = this.columnConfigList.add('Indexname', 'attributeSzColumnName', 15);
    this.columnConfigList.recalcWidth();
  }

  addItem() {
    this.windreamIndexList.focusedEntityShadowed.new();
    this.windreamIndexList.focusedEntityShadowed.entity.windreamSearchToDepartmentId = this.windreamSearchToDepartmentDetails.entityId;
    this.openPopupDialog(this.windreamIndexList.focusedEntityShadowed);
  }

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


  private outColKeyItemDescription() {
    return `Ausgabespalten für "${this.windreamSearchToDepartmentDetails.entity.windreamSearchName}" Abteilung "${this.departmentDetails.entity.departmentName}"`;

  }

  public copyWindreamOutCols() {
    this.departmentDataService.clpbrdWindreamOutColList = this.windreamIndexList.items.slice();
    this.departmentDataService.clpbrdWindreamOutColHint1 = this.outColKeyItemDescription();
    this.departmentDataService.clpbrdWindreamOutColHints = '';
    this.departmentDataService.clpbrdWindreamOutColList.forEach(col =>
      this.departmentDataService.clpbrdWindreamOutColHints += '\n' + col.objectTypeAttributeSzName);
  }


  public pasteWindreamOutCols() {
    this.windreamIndexDetails.confirmMessageBoxYesNo('Übernahme von ' + this.departmentDataService.clpbrdWindreamOutColHint1, this.outColKeyItemDescription() + ' werden mit ' + this.departmentDataService.clpbrdWindreamOutColHint1 + ' überschrieben. Sind Sie sicher?',
      () => {
        this.windreamIndexList.deleteAll();
        this.windreamIndexList.items.push(...this.departmentDataService.clpbrdWindreamOutColList);
        this.windreamIndexList.items.forEach(el => {
          el.entityId = 0;
          el.entityChanged = true;
          el.windreamSearchToDepartmentId = this.windreamSearchToDepartmentDetails.entityId;
        });
        this.windreamIndexList.save();
      }
    );
  }

}
