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

        this.$scope = $scope;
        this.$onInit = this.init;
        this.ischecked = false;
    }

    init() {
        this.$scope.$watch('$ctrl.inputModel', () => {
            this.ischecked = this.inputModel;
            if (this.trueValue && this.falseValue) {
                this.inputModel === this.trueValue ? this.ischecked = true : null;
                this.inputModel === this.falseValue ? this.ischecked = false : null;
            }
            if (this.trueValue && !this.falseValue) {
                this.ischecked = this.inputModel === this.trueValue;
                this.falseValue = false;
            }
        });
    }
    onChange() {
        // e.stopPropagation();
        // e.preventDefault();
        if (this.forbidden || this.ban) {
            return;
        }
        if (this.trueValue && (this.falseValue === false || this.falseValue)) {
            this.ischecked = !this.ischecked;
            this.inputModel = this.ischecked ? this.trueValue : this.falseValue;
            return;
        }
        this.inputModel = !this.inputModel;
        this.ischecked = this.inputModel;
    }
}

export const checkboxComponent = {
    bindings: {
        labelText: '<',
        trueValue: '<',
        falseValue: '<',
        forbidden: '<',
        ban: '<',
        inputModel: '='
    },
    transclude: {
        'append-body': '?appendBody'
    },
    templateUrl: 'app/controls/components/checkbox/checkbox.component.html',
    controller: CheckboxController
};