'use strict';

var Resource =  angular.module('localization.services', [
])
    .service('Resource', [
        '$http',
        'Config',
        function ($http, Config) {
            var resources;
            var get = function (key) {
                if (resources)
                    return resources[key];
            };

            var load = function (name, lang) {
                var url = {
                    Name: name,
                    Lang: lang
                };
                return $http.get(Config.BASE_LOCALIZE_URL + '/' + lang + '/' + name + '.json', { cache: true }).success(function (data) {
                    resources = data;
                });
            };

            return {
                get: get,
                load: load
            };
        }
    ])
    .directive('qnLocal', ['$interpolate', function ($interpolate) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {

                scope.$watch('locale', function (value) {
                    
                    var options = scope.$eval(attrs.qnOptions);
                    options = options || {};
                    var output = Mustache.render(scope.locale[attrs.qnLocal], options);
                    element.text(output);
                });
                
                scope.$watch(attrs.qnOptions, function (value) {
                    if (value) {
                        var options = scope.$eval(attrs.qnOptions);
                        options = options || {};
                        var output = Mustache.render(scope.locale[attrs.qnLocal], options);
                        element.text(output);
                    }
                }, true);

            }
        };
    }])
.directive('qnPluralize', ['$locale', '$interpolate', function ($locale, $interpolate) {
    var BRACE = /{}/g;
    return {
        restrict: 'EA',
        link: function (scope, element, attr) {
            var numberExp = attr.count,
                whenExp = element.attr(attr.$attr.when), // this is because we have {{}} in attrs
                offset = attr.offset || 0,
                whens = scope.$eval(whenExp),
                whensExpFns = {},
                startSymbol = $interpolate.startSymbol(),
                endSymbol = $interpolate.endSymbol();

            scope.$watch(function () {
                var value = parseFloat(scope.$eval(numberExp));
                whens = scope.$eval(whenExp);
                angular.forEach(whens, function (expression, key) {
                    whensExpFns[key] =
                      $interpolate(expression.replace(BRACE, startSymbol + numberExp + '-' +
                        offset + endSymbol));
                });
                if (!isNaN(value)) {
                    if (!whens[value]) value = $locale.pluralCat(value - offset);
                    return whensExpFns[value](scope, element, true);
                } else {
                    return '';
                }
            }, function (newVal) {
                element.text(newVal);
            });
        }
    };
}]);