export class CounterController {
    constructor($scope) {
        'ngInject';

        this.$scope = $scope;
        this.count = 0;
        if (!this.min) {
            this.min = 1;
        }

        if (!this.max) {
            this.max = 99999;
        }

        this.$onInit = this.init;
    }

    init() {
        let self = this;
        this.ngModel.$render = () => {
            self.count = self.ngModel.$viewValue;
        };

        this.$scope.$watch(function () {
            return self.count;
        }, function (value) {
            self.ngModel.$setViewValue(value);
        });
    }

    onPlus() {
        if (this.count === +this.max) {
            return;
        }

        this.count++;
    }

    onMinus() {
        if (this.count === +this.min) {
            return;
        }

        this.count--;
    }
}

export const counterComponent = {
    bindings: {
        min: '@',
        max: '@'
    },
    require: {
        ngModel: '^ngModel'
    },
    templateUrl: 'app/controls/components/counter/counter.html',
    controller: CounterController
};