import {
  DateTimeUtil,
  DefaultTooltip,
  GridCellTooltipUtils,
  GridColumnComparatorUtils,
  GridColumnKeyboardUtils,
  GridValueFormatterUtils,
  GridValueRendererUtils,
  PinnedType,
} from "@webbpm/base-package";
import {
  ColDef,
  DateFilter,
  ICellRendererFunc,
  SuppressKeyboardEventParams
} from "ag-grid-community";
import {
  StaticGridColumn
} from "../../../generated/ru/micord/ervu/account_applications/component/property/grid/StaticGridColumn";
import {CustomGridColumnFilterUtils} from "./filter/CustomGridColumnFilterUtils";
import {Moment} from "moment";
import * as moment from "moment-timezone";

export class StaticGridColumnAdapter {

  public static columnToColumnDef(gridRef: any, column: StaticGridColumn) {
    const columnDef: ColDef = {};
    columnDef.headerName = column.displayName ? column.displayName : '';
    columnDef.headerClass = "custom-header";
    columnDef.width = column.width;
    columnDef.suppressSizeToFit = column.widthFixed;
    columnDef.hide = column.hidden;
    columnDef.resizable = !column.widthFixed;
    columnDef.headerComponentParams = {"disable_hiding": column.disableHiding || false};
    columnDef.lockVisible = column.disableHiding;
    columnDef.headerTooltip = column.headerTooltip ? column.headerTooltip : column.displayName;
    columnDef.suppressMenu = column.suppressHeaderMenu;

    if (column.pinned) {
      columnDef.pinned = column.pinned == PinnedType.LEFT ? 'left' : 'right';
    }
    columnDef['gridComp'] = this;
    columnDef.sortable = column.sortable;

    if (column.sortable == null) {
      columnDef.sortable = true;
    }

    if (column.autoHeight) {
      columnDef.autoHeight = column.autoHeight;
      columnDef.cellClass = 'ag-grid-cell-wrap-text';
    }

    if (column.field) {
      columnDef.field = column.field.column;
      let type = column.field.type;

      if (type != null) {
        if (gridRef.getRowModelType() == "clientSide") {
          columnDef.comparator = GridColumnComparatorUtils.columnComparator(type);
        }
        columnDef.valueFormatter = GridValueFormatterUtils.columnFormatter(type);
        columnDef.cellRenderer = gridRef.createRenderer(column);
      }

      if (column.filter !== false) {
        columnDef.floatingFilter = gridRef.floatingFilter;
        columnDef.filter = CustomGridColumnFilterUtils.columnFilter(column.field.filterType);

        if (columnDef.filter === DateFilter) {
          columnDef.filterParams = {
            comparator: function (filterLocalDateAtMidnight, cellValue) {

              if (!cellValue) {
                return -1;
              }

              let filterMoment: Moment = moment.utc(filterLocalDateAtMidnight)
                  .add(-filterLocalDateAtMidnight.getTimezoneOffset(), 'm');
              let cellMoment: Moment = DateTimeUtil.parseToMidnightUTC(cellValue);

              if (filterMoment.isSame(cellMoment)) {
                return 0;
              }

              if (cellMoment.isBefore(filterMoment)) {
                return -1;
              }

              if (cellMoment.isAfter(filterMoment)) {
                return 1;
              }
            },
            browserDatePicker: true,
          };
        }
      }

      columnDef.suppressKeyboardEvent = (params: SuppressKeyboardEventParams) => {
        return GridColumnKeyboardUtils.suppressHomeAndEndKeyboardEvent(params);
      }
    }

    if (column.displayPopup) {
      const renderer: ICellRendererFunc = columnDef.cellRenderer as ICellRendererFunc;
      columnDef.cellRenderer = GridValueRendererUtils.tooltipValueRenderer(renderer);
    }
    else if (column.columnTooltip) {
      columnDef.tooltipComponent = DefaultTooltip;
      columnDef.tooltipValueGetter = GridCellTooltipUtils.fixedValueTooltip(column.columnTooltip);
    }
    else if (!gridRef.suppressColumnTooltip) {
      columnDef.tooltipComponent = DefaultTooltip;
      columnDef.tooltipValueGetter = GridCellTooltipUtils.defaultFormattedTooltip(
          column.field ? column.field.type : null);
    }

    return columnDef;
  }
}
