import { CustomColDef } from './../../model/common-model';
import { GridHeaderBar } from './../../resources/elements/grid-header-bar';
import { AnchorTab } from './../../ui/anchor-tab/ui-anchor-tab';
import { ColDef } from 'ag-grid-community';
import { autoinject } from 'aurelia-framework';
import { FloatingBoxViewModel } from './floating-box';
import { autoAdjustColumnsSize } from './../../ag-grid/agUtils';
import { Config } from './../../config';

export interface ViewParameterColumn {
  headerName: string;
  field: string;
  visible: boolean;
  column: ColDef;
}

@autoinject
export class ViewParameterBox extends FloatingBoxViewModel {
  private parameters: Map<string, Array<ViewParameterColumn>> = new Map();
  public tabList: Array<AnchorTab> = [];

  private columnSearch: string;

  constructor(private containerInstance: GridHeaderBar, private config: Config) {
    super('./view-parameter-box.html');
  }

  public async activate(config: any) {
    await super.activate(config);
    this.tabList?.splice(0);
    this.parameters = new Map();
    let rowGroup = (this.containerInstance.gridOptions.columnDefs as CustomColDef[]).groupBy("showRowGroup");
    rowGroup.forEach((values, key) => {
      let columns: Array<ViewParameterColumn> = new Array();
      let keyValue = key != null ? key.replace(" ", "-") : '';
      if (keyValue != '') {
        this.tabList.push(new AnchorTab(keyValue, key, true));
      }
      for (let column of values) {
        let columnData = this.containerInstance.gridOptions.columnApi.getColumn(column.field);
        if ((column.headerName != null && column.headerName != '' && column.hideInParameter != true && columnData != null)) {
          let viewColumn = {
            headerName: column.headerName,
            field: column.field,
            column: column,
            visible: columnData.isVisible()
          } as ViewParameterColumn;
          columns.push(viewColumn);
          this.containerInstance.disposables.push(
            this.containerInstance.bindingEngine.propertyObserver(viewColumn, 'visible').subscribe((newValue, oldValue) => {
              this.containerInstance.gridOptions.columnApi.setColumnVisible(viewColumn.field, newValue);
              window.dispatchEvent(new Event('resize'));
            })
          );
        }
      }
      if (this.config.agGridConfig.sortColumnByHeaderName) {
        columns.sortBy("headerName");
      }
      this.parameters.set(keyValue, columns);
    });
  }

  public getTitle(): string {
    return this.containerInstance.i18n.tr('common:grid.parametersView');
  }

  public selectAll() {
    let isVisible: boolean = false;

    for (const columns of this.parameters.values()) {
      if (columns.some(column => !column.visible)) {
        isVisible = true;
        break;
      }
    }

    for (const columns of this.parameters.values()) {
      columns.filter(y => y.visible != isVisible).forEach((column: ViewParameterColumn) => {
        column.visible = isVisible;
      });
    }
  }

  private onAdjustColumnsChanged() {
    autoAdjustColumnsSize(this.containerInstance.gridOptions);
  }
}
