'use strict';

angular.module("global", [
    'config.services'
])
    .service("Global", [
        '$http',
        '$location',
        '$route',
        '$timeout',
        'Config',
        function ($http, $location, $route, $timeout, Config) {
            var $scope;

            var URL = {
                RESOURCE: '/Areas/cms/Scripts/Languages'
            };

            var init = function (scope) {

                $scope = scope;

                var defaults = {
                    current: '',
                    defaultLanguage: '',
                    languages: {},

                    get: function (lang) {
                        if (lang && !$scope.language.languages[lang]) {
                            $http.get(URL.RESOURCE + '/' + lang + '/' + lang + '.json')
                                .success(function (data) {
                                    angular.extend($scope.language.languages, data);
                                });
                            Globalize.culture(lang);
                            $.get('App/SetCulture', { "culture": lang }, function (data) {
                            });
                            $route.reload();
                        }
                    }
                };

                $scope.language = defaults;

                $scope.language.get($scope.language.defaultLanguage);

                $scope.$watch('laguage.defaultLanguage', function (newValue, oldValue) {
                    $timeout(function () {
                        $scope.language.get(newValue);
                    }, 1);
                });

                $scope.$watch('language.current', function (newValue, oldValue) {
                    if (newValue == $scope.language.defaultLanguage) {
                    }
                    else {
                        $scope.language.get(newValue);
                    }
                });

                var resourceScope = function (key, properties, scope) {
                    return resource(key, properties, scope);
                };


                var resource = function (key, properties, scope) {
                    //alert("resource" + key);
                    if (scope)
                        $scope = scope;
                    var current = $scope.language.current;
                    var ref = $scope.language.languages[current];

                    if (!ref) {
                        return '';
                    }

                    if (key === undefined)
                        key = "undefined";

                    var parts = key.split('.');
                    var value = '';

                    angular.forEach(parts, function (v, k) {
                        if (k == parts.length - 1) {
                            value = ref[v];
                        } else {
                            if (!ref[v]) {
                                return false;
                            }
                            ref = ref[v];
                        }
                    });

                    if (!value || value.length == 0) {
                        //console.log("using default language line " + key);
                        ref = $scope.language.languages[$scope.language.current];
                        angular.forEach(parts, function (v, k) {
                            if (k == parts.length - 1) {
                                value = ref[v];
                            } else {
                                if (!ref[v]) {
                                    return false;
                                }
                                ref = ref[v];
                            }
                        });
                    }

                    var returns = value || key;

                    if (properties != null) {
                        try {
                            var s = "'" + (returns.replace(/\{/g, "', ").replace(/\}/g, ", '"))
                                + "'";
                            var fn = new Function('obj', "with (obj) { return [" + s + "].join('');}");
                            return fn(properties);
                        } catch (error) {
                            return '';
                        }
                    }
                    return returns + '';
                };

                $scope.$resource = resource;
                $scope.$resourceScope = resourceScope;
            };

            return {
                init: init
            };

        }]);