import { Component, Input, OnInit } from '@angular/core';
import { Department } from '@app_models/basedata/department';
import { WindreamSearchItemToWindreamSearchToDepartment } from '@app_models/windream-search-item-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 { DepartmentWindreamSearchItemPopupeditComponent } from './department-windream-search-item-popupedit.component/department-windream-search-item-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-search-item',
  templateUrl: './department-windream-search-item.component.html',
  styleUrls: ['./department-windream-search-item.component.scss']
})
export class DepartmentWindreamSearchItemComponent implements OnInit {
  @Input() windreamSearchItemList: AppBaseEntityListWrapper<WindreamSearchItemToWindreamSearchToDepartment>;
  @Input() windreamSearchItemDetails: AppBaseEntityWrapper<WindreamSearchItemToWindreamSearchToDepartment>;
  @Input() windreamSearchToDepartmentDetails: AppBaseEntityWrapper<WindreamSearchToDepartment>;
  @Input() departmentDetails: AppBaseEntityWrapper<Department>;

  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('Suchbaustein', 'windreamSearchItemName', 10);
    this.columnConfigList.recalcWidth();
  }

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

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


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


  public copyWindreamOutCols() {
    this.departmentDataService.clpbrdWindreamSearchItemList = this.windreamSearchItemList.items.slice();
    this.departmentDataService.clpbrdWindreamSearchItemHint1 = this.outColKeyItemDescription();
    this.departmentDataService.clpbrdWindreamSearchItemHints = '';
    this.departmentDataService.clpbrdWindreamSearchItemList.forEach(col =>
      this.departmentDataService.clpbrdWindreamSearchItemHints += '\n' + col.windreamSearchItemName);
  }


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

}
