import { FontIconType } from "../FontIconType";
import { MainStateManager } from "../MainStateManager";
import { INaming } from "../NamingCaption";
import { ElementsOfFormFactory } from "../Page/ElementsOfFormFactory";
import { GridViewFactory } from "./GridViewFactory";

export function AccountColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    level?: number;
}) {
    return null;
}

export function DetailedAccountColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}


export function CustomerColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}


export function CostCenterColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function BankColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function BranchColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function CarColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function CashColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function DriveColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function PersonColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function PurchaseOfficerColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function SaleManagerColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function StakeholderColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function CapitalistColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function VisitorColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function StringColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    width?: number;
    isFillEmptySpace?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    filterFieldName?: string;
    onPrepareShowValue?: (value: string, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}


export function CountColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    width?: number;
    isFillEmptySpace?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function RowColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
}) {
    return null;
}

export function MergeColumns(_props: {
    caption: INaming | string;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    children: React.ReactNode;
}) {
    return null;
}

export function IdColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function QTYColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function NumberOfCheckColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function DateColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: string, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function NumberColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    filterFieldName?: string;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}

export function AmountColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    sortable?: boolean;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    autoHideIfEmpty?: true;
    calcSummery?: true;
    filterFieldName?: string;
    onPrepareShowValue?: (value: number, row: any, previousRow: any, nextRow: any, rowInex: number) => any;
}) {
    return null;
}
export function IconColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    headerIcon?: FontIconType;
    defaultIcon?: FontIconType;
    sizeIcon?: 1 | 2 | 3;
    colorIcon?: string;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    onPrepareShowValue?: (value: any, row: any, previousRow: any, nextRow: any, rowInex: number) => FontIconType | [FontIconType, string] | null;
    onOnDoublClick?: (value: any, row: any, previousRow: any, nextRow: any, rowInex: number, gridViewFactory: GridViewFactory) => void;
}) {
    return null;
}
export function NumberIconColumn(_props: {
    fieldNameOfRow: string;
    caption: INaming | string;
    headerIcon?: FontIconType;
    sizeIcon?: 1 | 2 | 3;
    colorIcon?: string;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
    onPrepareSetColor?: (value: number | null, row: any, previousRow: any, nextRow: any, rowInex: number) => string | null;
    onOnDoublClick?: (value: number | null, row: any, previousRow: any, nextRow: any, rowInex: number, gridViewFactory: GridViewFactory) => void;
}) {
    return null;
}
export function LockUnLockColumn(_props: {
    fieldNameOfRow: string;
    sizeIcon?: 1 | 2 | 3;
    visible?: boolean;
    changeVisibility?: true;
    saveVisibility?: true;
}) {
    return null;
}


export type IGridViewColumnType =
    | 'row'
    | 'heasb'
    | 'heasbName'
    | 'kala'
    | 'kalaName'
    | 'markaz1'
    | 'markaz1Name'
    | 'markaz2'
    | 'markaz2Name'
    | 'markaz3'
    | 'markaz3Name'
    | 'QTY'
    | 'QTYView'
    | 'Fee'
    | 'FeeView'
    | 'id'
    | 'date'
    | 'amount'
    | 'count'
    | 'text'
    | 'icon'
    | 'numberIconColumn'
    | 'lockUnLock'
    | 'button'
    | 'textView'
    | 'checkBox'
    | 'checkTree'
    | 'BaseStructuralGetByCode'
    | 'BaseInheritedDetailedAccountCode'
    | 'payLoad';
interface IGridFactory {
    any: any;
    columns: GridViewColumn[];
    mainStateManager: MainStateManager;
    mergeCaptionColumns: GridViewMergCaptionColumn[] | null;
    elementsOfForm: ElementsOfFormFactory;
}
export class GridViewColumn {
    public isCellValueEmpty = (row: any): boolean => {
        const cellValue = row.getProp(this.fieldName);
        if (!cellValue) {
            return true;
        }
        switch (this.columnType) {
            case 'QTY':
            case 'QTYView':
            case 'amount':
            case 'count':
            case 'id':
            case 'row':
                if (+cellValue === 0) return true;
            default: if (cellValue === '') return true;
        }
        return false;
    }
    public get any() {
        return this as any;
    }
    public index: number;
    public keyOfColumn: string;
    public columnType: IGridViewColumnType;
    public initialzed: boolean = false;
    public headerText: string = 'none';
    public visible: boolean = true;
    public isFillEmptySpace = false;
    public addedByColumnMerge?: boolean;
    public gridViewMergCaptionColumn?: GridViewMergCaptionColumn;

    constructor(
        public gridViewFactory: IGridFactory,
        child: any,
        public headerCaption: INaming | string,
        public headerIcon: FontIconType | undefined,
        public fieldName: any,
        public width: number,
        public autoHideIfEmpty: boolean,
        public filterFieldName?: string,
        public sortable?: boolean,
        public changeVisibility?: boolean,
        public saveVisibility?: boolean,
        public onPrepareShowValue?: (value: any, currentRow: any, previousRow: any, nextRow: any, rowIndex: number) => any,
        public onPrepareSetColor?: (value: any, currentRow: any, previousRow: any, nextRow: any, rowIndex: number) => any,
        public calcSummery?: boolean,
        visible?: boolean,
        isFillEmptySpace?: boolean,
        public defaultIcon?: FontIconType,
        public sizeOfIcon?: 1 | 2 | 3,
        public colorOfIcon?: string,
        public onOnDoublClick?: (value: any, row: any, previousRow: any, nextRow: any, rowInex: number, gridViewFactory: GridViewFactory) => void,

    ) {
        this.index = this.gridViewFactory.columns.length;
        this.columnType = this.getType(child);
        this.keyOfColumn = this.fieldName + this.index.toString();
        this.visible = typeof visible === 'boolean' ? visible : true;
        if (this.autoHideIfEmpty) {
            this.visible = false;
        }

        // if (saveVisibility) {
        //     const result = this.mainStateFactory.pageData.getUserOption(fieldName);
        //     this.visible = result === 'true' ? true : result === 'false' ? false : this.visible;
        // }

        switch (this.columnType) {
            case 'text':
                this.isFillEmptySpace = isFillEmptySpace ? isFillEmptySpace : false;
                this.width = width ? width : 120;
                break;
        }

        this.getHeaderText();
    }

    private getType = (child: any): IGridViewColumnType => {
        if (child.type === AccountColumn) {
            return 'BaseStructuralGetByCode';
        } else if (child.type === DetailedAccountColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === CustomerColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === CostCenterColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === BankColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === BranchColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === CarColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === CashColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === DriveColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === PersonColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === PurchaseOfficerColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === SaleManagerColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === StakeholderColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === CapitalistColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === VisitorColumn) {
            return 'BaseInheritedDetailedAccountCode'
        } else if (child.type === StringColumn) {
            return 'text'
        } else if (child.type === CountColumn) {
            return 'count'
        } else if (child.type === QTYColumn) {
            return 'QTY'
        } else if (child.type === QTYColumn) {
            return 'text'
        } else if (child.type === DateColumn) {
            return 'date'
        } else if (child.type === NumberColumn) {
            return 'text'
        } else if (child.type === AmountColumn) {
            return 'amount'
        } else if (child.type === RowColumn) {
            return 'row';
        } else if (child.type === IdColumn) {
            return 'id';
        } else if (child.type === IconColumn) {
            return 'icon';
        } else if (child.type === NumberIconColumn) {
            return 'numberIconColumn';
        } else if (child.type === LockUnLockColumn) {
            return 'lockUnLock';
        }
        return 'text';
    }

    public checkHeaderTextIfNeedUpdate = (newCaption: INaming) => {
        this.headerCaption = newCaption;
        this.getHeaderText();
    }

    private getHeaderText = () => {
        if (typeof this.headerCaption === 'string') {
            this.headerText = this.headerCaption;
        } else {
            this.headerText = this.gridViewFactory.mainStateManager.getCaptionNaming(this.headerCaption);
        }
    }
}

export class GridViewMergCaptionColumn {
    public index: number;
    public columns: GridViewColumn[] | null = null;
    #visible: boolean = true;
    public fieldName: any;
    public headerText: string = 'none';
    constructor(
        public gridViewFactory: IGridFactory,
        public headerCaption: INaming,
        visible?: boolean,
        public changeVisibility?: boolean,
        public saveVisibility?: boolean,
    ) {
        if (this.gridViewFactory.mergeCaptionColumns === null) {
            this.gridViewFactory.mergeCaptionColumns = [];
        }

        this.index = this.gridViewFactory.mergeCaptionColumns.length;
        this.#visible = typeof visible === 'boolean' ? visible : true;

        this.getHeaderText(gridViewFactory);
    }

    private getHeaderText = (gridViewFactory: IGridFactory) => {
        if (typeof this.headerCaption === 'string') {
            this.headerText = this.headerCaption;
        } else {
            this.headerText = gridViewFactory.mainStateManager.getCaptionNaming(this.headerCaption);
        }
    }

    public set visible(value: boolean) {
        this.#visible = value;
        this.columns?.forEach(c => c.visible = value);
    }
    public get visible() {
        return this.#visible;
    }
}

export interface CellAddressGridView {
    rowindex: number;
    rowData: any;
    column?: GridViewColumn;
}
