'use strict';

angular.module('angularJSApp.directives',[])
    // Compare to another value
    // Such as confirm password
    .directive("compareTo", function () {

        return {
            require: "ngModel",
            scope: {
                otherModelValue: "=compareTo"
            },
            link: function (scope, element, attributes, ngModel) {
                ngModel.$validators.compareTo = function (modelValue) {

                    var cp = (modelValue === scope.otherModelValue.$modelValue);
                    ngModel.$setValidity('repeat', cp);
                    return cp;
                };

                scope.$watch("otherModelValue.$modelValue", function () {
                    ngModel.$validate();
                });
            }
        };
    })
    .directive("lowerThan", function () {

        var link = function($scope, $element, $attrs, ctrl) {

            var validate = function(viewValue) {
                var comparisonModel = $attrs.lowerThan;
                var t, f;

                if(!viewValue || !comparisonModel){
                    // It's valid because we have nothing to compare against
                    ctrl.$setValidity('lowerThan', true);
                }
                if (comparisonModel) {
                    var to = comparisonModel.split("/");
                    t = new Date(to[2], to[1] - 1, to[0]);
                }
                if (viewValue) {
                    var from = viewValue.split("/");
                    f = new Date(from[2], from[1] - 1, from[0]);
                }

                ctrl.$setValidity('lowerThan', Date.parse(t) <= Date.parse(f));
                // It's valid if model is lower than the model we're comparing against

                return viewValue;
            };

            ctrl.$parsers.unshift(validate);
        };

        return {
            require: 'ngModel',
            link: link
        };
    })
    .directive("higherThan", function () {

        var link = function($scope, $element, $attrs, ctrl) {

            var validate = function(viewValue) {
                var comparisonModel = $attrs.higherThan;
                var t, f;

                if(!viewValue || !comparisonModel){
                    // It's valid because we have nothing to compare against
                    ctrl.$setValidity('higherThan', true);
                }
                if (comparisonModel) {
                    var to = comparisonModel.split("/");
                    t = new Date(to[2], to[1] - 1, to[0]);
                }
                if (viewValue) {
                    var from = viewValue.split("/");
                    f = new Date(from[2], from[1] - 1, from[0]);
                }
                ctrl.$setValidity('higherThan', Date.parse(t) >= Date.parse(f));
                // It's valid if model is higher than the model we're comparing against

                return viewValue;
            };

            ctrl.$parsers.unshift(validate);
        };

        return {
            require: 'ngModel',
            link: link
        };
    })
    .directive('disallowSpace', function () {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, modelCtrl) {

                modelCtrl.$parsers.push(function (inputValue) {

                    var transformedInput = inputValue.replace(/ /g, '');

                    if (transformedInput!==inputValue) {
                        modelCtrl.$setViewValue(transformedInput);
                        modelCtrl.$render();
                    }

                    return transformedInput;
                });
            }
        };
    });
