import ngModule from '../../module';
import Field from '../field';

export default class InputNumber extends Field {
    constructor($element, $scope, $compile) {
        super($element, $scope, $compile);
        this.buildInput('number');
    }

    /**
     * Gets max value
     */
    get max() {
        return this.input.max;
    }

    /**
     * Sets max allowed value
     *
     * @param {Number} value - Value
     */
    set max(value) {
        this.input.max = value;
        this.validateValue();
    }

    /**
     * Gets min value
     */
    get min() {
        return this.input.min;
    }

    /**
     * Sets min allowed value
     *
     * @param {Number} value - Value
     */
    set min(value) {
        this.input.min = value;
        this.validateValue();
    }

    /**
     * Gets min step value
     */
    get step() {
        return parseFloat(this.input.step);
    }

    /**
     * Sets min step value
     *
     * @param {Number} value - Value
     */
    set step(value) {
        this.input.step = value;
        this.validateValue();
    }

    stepUp() {
        this.input.stepUp();
    }

    stepDown() {
        this.input.stepDown();
    }

    onStep(event, way) {
        if (event.defaultPrevented) return;

        if (way == 'up')
            this.stepUp();
        else
            this.stepDown();

        this.input.dispatchEvent(new Event('change'));
    }
}

ngModule.vnComponent('vnInputNumber', {
    template: require('./index.html'),
    controller: InputNumber,
    bindings: {
        min: '<?',
        max: '<?',
        step: '<?',
        displayControls: '<?',
        clearDisabled: '<?'
    }
});