import {ChangeDetectionStrategy, Component} from "@angular/core";
import {
  CustomLoadingComponent,
  EditableGrid,
  Event, GridRows,
  GridV2,
  GridV2Column, ObjectRef,
  Visible
} from "@webbpm/base-package";
import {
  StaticGridColumn
} from "../../../generated/ru/micord/ervu/account_applications/component/property/grid/StaticGridColumn";
import {
  ColDef,
  FilterChangedEvent,
  ICellRendererParams,
  ITooltipParams,
  ValueFormatterParams,
  ValueGetterParams
} from "ag-grid-community";
import {StaticGridColumnAdapter} from "./StaticGridColumnAdapter";

@Component({
             moduleId: module.id,
             selector: 'ervu-static-grid',
             templateUrl: './../../../../../src/resources/template/account_applications/component/grid/ErvuStaticGrid.html',
             changeDetection: ChangeDetectionStrategy.OnPush
           })
export class ErvuStaticGrid extends GridV2 implements CustomLoadingComponent {
  @Visible("false")
  public columnFiltersChanged: Event<any> = new Event<any>();
  public disableLoad: boolean;
  @ObjectRef()
  public editableGridRef: EditableGrid;

  getColumns(): any[] {
    return this.getScriptsInChildren(GridV2Column)
        .map(columnV2 => columnV2.getScript(StaticGridColumn));
  }

  load(): void {
    if (this.disableLoad) {
      return;
    }
    super.load();
  }

  setKeyValue(gridRows: GridRows): Promise<void> {
    this.setValue(gridRows);
    if (this.editableGridRef) {
      this.editableGridRef.addRows(gridRows.rows);
    }
    return Promise.resolve();
  }

  protected columnV2ToColumnDef(column: GridV2Column): ColDef {
    let gridColumn = column.getScript(StaticGridColumn);
    let colDef = this.columnToColumnDef(gridColumn);
    if (column.renderer) {
      colDef.cellRenderer = (params: ICellRendererParams) => column.renderer.render(params);
    }
    if (column.valueGetter) {
      colDef.valueGetter = (params: ValueGetterParams) => column.valueGetter.get(params);
    }
    if (column.tooltipValueGetter) {
      if (gridColumn.columnTooltip) {
        throw new Error(
            "Only one type of tooltip should be specified: tooltipValueGetter or columnTooltip");
      }
      colDef.tooltipValueGetter = (params: ITooltipParams) => column.tooltipValueGetter.get(params);
    }

    column.setGridRef(this);
    column.setColDef(colDef);
    return colDef;
  }

  protected columnToColumnDef(column: any): ColDef {
    let colDef = StaticGridColumnAdapter.columnToColumnDef(this, column);
    let columnComp = column.context;
    colDef['columnUid'] = columnComp.getObjectId();

    if (columnComp.cellCssClassRulesProvider) {
      colDef.cellClassRules = columnComp.cellCssClassRulesProvider.getRules();
    }

    if (columnComp.valueFormatter) {
      colDef.valueFormatter = (params?: ValueFormatterParams) => {
        return columnComp.valueFormatter.format(params);
      }
    }
    return colDef;
  }

  @Visible()
  public getRowDataSize(): number {
    return this.rowData ? this.rowData.length : 0;
  }

  @Visible()
  public hasColumnFilters(): boolean {
    const filterModel: { [key: string]: any; } = this.gridApi.getFilterModel();
    return !!filterModel && Object.keys(filterModel).length > 0;
  }

  public columnFilterChanged(event: FilterChangedEvent) {
    this.columnFiltersChanged.trigger(event);
    super.columnFilterChanged(event);
  }
}