angular.module("app").directive("timeSpinner", function ($rootScope, $timeout) {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            min: '@',
            max: '@',
            step: '@',
            sqModel: '=',
            sqDisabled: '=',
            notConvert: '=',
        },
        templateUrl: "../app/directives/timeSpinner/timeSpinner.html",
        link: function (scope, element, attrs) {

            var changeTimeout = null;

            if (isNaN(scope.sqModel)) {
                scope.sqModel = scope.min;
            }

            var innerModel = scope.sqModel;
            if (scope.notConvert) {
                innerModel = getSecondsFromTime(innerModel);
                scope.modelSQ = {
                    val: scope.sqModel
                };
            }
            else {
                scope.modelSQ = {
                    val: getTimeFromMilis(innerModel)
                };
            }

            var watcher2 = scope.$watch('sqModel', function (newValue, oldValue) {
                innerModel = scope.sqModel;
                if (scope.notConvert) {
                    innerModel = getSecondsFromTime(innerModel);
                }
                scope.modelSQ.val = getTimeFromMilis(innerModel);
            }, true);

            scope.$on('$destroy', function (event, args) {
                watcher2();
            }, true);

            scope.spinnerMinus = function () {
                if (scope.sqDisabled) {
                    return;
                }
                if (innerModel - scope.step >= scope.min) {
                    innerModel -= scope.step;
                }
                scope.modelSQ.val = getTimeFromMilis(innerModel);
                updateSqModel();
            }

            scope.spinnerPlus = function () {
                if (scope.sqDisabled) {
                    return;
                }
                innerModel = parseInt(innerModel);
                scope.step = parseInt(scope.step);
                if (innerModel + scope.step <= scope.max) {
                    innerModel += scope.step;
                }
                scope.modelSQ.val = getTimeFromMilis(innerModel);
                updateSqModel();
            }

            scope.changeValue = function (val) {
                $timeout(function () {
                    var timeParts = scope.modelSQ.val.split(':');
                    var totalTime = "00:00";

                    if (timeParts.length == 2) {
                        totalTime = ("00" + parseInt(timeParts[0])).slice(-2) + ":" + ("00" + parseInt(timeParts[1])).slice(-2);
                    }
                    else if (timeParts.length == 1) {
                        totalTime = ("00" + parseInt(timeParts[0])).slice(-2) + ":00";
                    }
                    scope.modelSQ.val = totalTime;
                    innerModel = getSecondsFromTime(totalTime);
                    updateSqModel();
                }, 0, false);
            }

            function updateSqModel() {
                if (scope.notConvert) {
                    scope.sqModel = getTimeFromMilis(innerModel);
                }
                else {
                    scope.sqModel = innerModel;
                }
                scope.$evalAsync();
            }

            function getTimeFromMilis(milis) {
                var date = new Date(null);
                date.setSeconds(milis);
                return date.toISOString().substr(11, 5);
            }

            function getSecondsFromTime(value) {
                var timeParts = value.split(':');
                if (timeParts.length < 2) {
                    return 0;
                }

                return (parseInt(timeParts[0]) * 60 + parseInt(timeParts[1])) * 60;
            }

        }
    };
});