import { I18N } from 'aurelia-i18n';
import { ColDef, ICellRendererParams } from "ag-grid-community";
import { FieldType, Various } from '../../../model/common-model';
import * as breeze from 'breeze-client';
import { ListViewModelBase, SelectAllHeaderComponent } from "../../../vm/list-view-model-base";
import { Container } from 'aurelia-framework';
import { AGGridColumnsWidth } from './../../../constants/constants';


export class GridHelper {
    public static getSelectedColDef<T extends breeze.Entity>(vm: ListViewModelBase<T>, coldef: ColDef = {}): ColDef {
        return Object.assign({
            headerName: "",
            field: "isSelected",
            maxWidth: AGGridColumnsWidth.IsSelected,
            minWidth: AGGridColumnsWidth.IsSelected,
            cellRenderer: "selectionCellRenderer",
            suppressMenu: true,
            sortable: false,
            resizable: false,
            suppressMovable: true,
            filter: false,
            pinned: "left",
            headerComponent: SelectAllHeaderComponent,
            headerComponentParams: { VM: vm }
        }, coldef);
    }

    public static getCreatedTimeColDef(coldef: ColDef = {}): ColDef {
        let i18n = Container.instance.get(I18N);
        return Object.assign({
            headerName: i18n.tr("general.createdTime", { ns: "common" }),
            field: "createdTime",
            hide: true,
            floatingFilterComponentParams: {
                suppressFilterButton: true,
                dateFormat: Various.DateFormatWithHours
            },
            type: FieldType.Date,
            showRowGroup: i18n.tr("general.generalInformation", { ns: "common" })
        }, coldef);
    }

    public static getCreatedFullNameColDef(entityUserName: string, coldef: ColDef = {}): ColDef {
        let i18n = Container.instance.get(I18N);
        return Object.assign({
            headerName: i18n.tr("general.createdByFullName", { ns: "common" }),
            field: "createdByFullName",
            hide: true,
            type: FieldType.User,
            filterParams: {
                userServiceName: entityUserName
            },
            floatingFilterComponentParams: {
                suppressFilterButton: true,
                userServiceName: entityUserName
            },
            showRowGroup: i18n.tr("general.generalInformation", { ns: "common" })
        }, coldef);
    }

    public static getUpdatedTimeColDef(coldef: ColDef = {}): ColDef {
        let i18n = Container.instance.get(I18N);
        return Object.assign({
            headerName: i18n.tr("general.updatedTime", { ns: "common" }),
            field: "updatedTime",
            hide: true,
            floatingFilterComponentParams: {
                suppressFilterButton: true,
                dateFormat: Various.DateFormatWithHours
            },
            type: FieldType.Date,
            showRowGroup: i18n.tr("general.generalInformation", { ns: "common" })
        }, coldef);
    }

    public static getUpdatedFullNameColDef(entityUserName: string, coldef: ColDef = {}): ColDef {
        let i18n = Container.instance.get(I18N);
        return Object.assign({
            headerName: i18n.tr("general.updatedByFullName", { ns: "common" }),
            field: "updatedByFullName",
            hide: true,
            type: FieldType.User,
            filterParams: {
                userServiceName: entityUserName
            },
            floatingFilterComponentParams: {
                suppressFilterButton: true,
                userServiceName: entityUserName
            },
            showRowGroup: i18n.tr("general.generalInformation", { ns: "common" })
        }, coldef);
    }

    public static getBaseEntityColDef(entityUserName: string, params?: {
        baseColDef?: ColDef, createdTimeColDef?: ColDef, createdFullNameColDef?: ColDef, updatedTimeColDef?: ColDef, updatedFullNameColDef?: ColDef
    }): ColDef[] {
        if (params == null) {
            params = { baseColDef: {} };
        }
        if (params.baseColDef == null) {
            params.baseColDef = {};
        }
        return [
            this.getCreatedTimeColDef(Object.assign({}, params.baseColDef, params.createdTimeColDef ?? {})),
            this.getCreatedFullNameColDef(entityUserName, Object.assign({}, params.baseColDef, params.createdFullNameColDef ?? {})),
            this.getUpdatedTimeColDef(Object.assign({}, params.baseColDef, params.updatedTimeColDef ?? {})),
            this.getUpdatedFullNameColDef(entityUserName, Object.assign({}, params.baseColDef, params.updatedFullNameColDef ?? {})),
        ];
    }
}