import { ICellEditorComp } from "ag-grid-community";
import { Parser } from 'expr-eval';


export default class NumberCellEditor implements ICellEditorComp {
    eGui: HTMLInputElement;
    cancelBeforeStart: boolean;
    private fractionDigits: number = 2;
    getValue() {
        if (this.eGui.value == null || this.eGui.value.length == 0) return null;
        try {
            const expr = this.eGui.value
                .replace(new RegExp('\\,', 'g'), function (match) {
                return match === ',' ? '.': ','
                })
            let value = Parser.evaluate('roundTo(' + expr + ', '+ this.fractionDigits +')');
            
            return value;
        }
        catch(error) {
            console.error('Error when evaluating expression : ' + error);
            return null;
        }
        
        //return this.eGui.value;
    }
    getGui() {
        return this.eGui;
    }
    afterGuiAttached() {
        this.eGui.focus();
    }
    isCancelBeforeStart() {
        return this.cancelBeforeStart;
    }
    // example - will reject the number if it contains the value 007
    // - not very practical, but demonstrates the method.
    isCancelAfterEnd () {
        return false;
        //var value = this.getValue();
        //return value.indexOf('007') >= 0;
    }
    destroy() {

    }
    isPopup() {
        return false;
    }
    init?(params: import("ag-grid-community").ICellEditorParams | any ) {  
         // create the cell
        this.eGui = document.createElement('input');
//        this.eGui.type = 'number';

        if (this.isCharNumeric(params.charPress)) {
            this.eGui.value = params.charPress;
        } else {
            if (params.value !== undefined && params.value !== null) {
                //this.eGui.value = params.value.toLocaleString != null ? params.value.toLocaleString('fr-BE') : params.value;
                this.eGui.value = params.value;
            }
        }  
        this.eGui.addEventListener('keypress', (event) => {
            if (!this.isKeyPressedNumeric(event)) {
                this.eGui.focus();
                if (event.preventDefault) event.preventDefault();
            } else if (this.isKeyPressedNavigation(event)) {
                event.stopPropagation();
            }
        });

        // only start edit if key pressed is a number, not a letter
        var charPressIsNotANumber = params.charPress && ('1234567890+-()/*,.'.indexOf(params.charPress) < 0);
        this.cancelBeforeStart = charPressIsNotANumber;
        if (params.fractionDigits != null) {
            this.fractionDigits = params.fractionDigits;
        }
    }    

    isCharNumeric(charStr) {
        return ('1234567890+-()/*,.'.indexOf(charStr) >= 0)
        //return !!/\d/.test(charStr);
    }
    
    isKeyPressedNumeric(event) {
        var charCode = this.getCharCodeFromEvent(event);
        var charStr = String.fromCharCode(charCode);
        return this.isCharNumeric(charStr);
    }
    isKeyPressedNavigation (event) {
        return event.keyCode === 39
            || event.keyCode === 37;
    }
    getCharCodeFromEvent(event) {
        event = event || window.event;
        return (typeof event.which == "undefined") ? event.keyCode : event.which;
    }
}
