import { Column } from 'devextreme/ui/data_grid';

export interface ILookup {
  dataSource: any;
  valueExpr: string;
  displayExpr: string;
}

export const enum EN_Alignment {
  right = 'right',
  left = 'left',
  center = 'center',
}

export const enum EN_SortOrder {
  asc = 'asc',
  desc = 'desc',
}


export type Type_FractionDigits = null | -1 | 0 | 1 | 2 | 3 | 4 | 5 | 6;
export type Type_Datentyp = string | number | Date;

export class ColumnConfig {
  allowSorting = true;
  allowEditing = true;
  private _sortIndex: number;
  public get sortIndex(): number {
    return this._sortIndex;
  }
  public set sortIndex(value: number) {
    this._sortIndex = value;
    if (!this.sortOrder) this.sortOrder = EN_SortOrder.asc;
  }
  sortOrder: EN_SortOrder;
  alignment: EN_Alignment;
  cellTemplate: string;
  visible: boolean = true;
  visibleIndex: number;
  fixed: boolean = false;
  headerCellTemplate: string;
  width: string;
  name: string;
  minWidth: number;
  _widthFactor: number = 1;
  allowHeaderFiltering: boolean = false;
  allowFiltering: boolean = true;
  dataType: string;
  groupIndex: number;
  cssClass: string;
  subColumns: ColumnConfig[] = [];

  lookup: ILookup;
  format: any;
  calculateCellValue: Function;
  calculateDisplayValue: Function | string;
  calculateGroupValue: Function | string;
  calculateSortValue: Function | string;
  customizeText: Function | string;


  public get widthFactor(): number {
    return this.groupIndex !== undefined ? 0 : this._widthFactor;
  }

  public set widthFactor(v: number) {
    this._widthFactor = v;
    if (this._widthFactor < 0) this.width = '' + (- this._widthFactor) + 'px';
    else this.width = this._widthFactor + '%';
  }


  constructor(public caption: string = '', public dataField: string = '', widthFactor: number = 1, fractionDigits?: Type_FractionDigits) {
    if (fractionDigits || fractionDigits === 0) {
      if (fractionDigits >= 0) this.cellTemplate = 'numberTemplate_' + fractionDigits;
      this.alignment = EN_Alignment.right;
    }
    this.widthFactor = widthFactor;
  }
}
