angular.module('app').service('L', function ($rootScope, $q, $localStorage, BackendService, SQEvents, SQConstants) {

    function loadLanguage() {
        var lastLanguage = $storage.language || SQConstants.getSettings().language;
        return lastLanguage ? lastLanguage : defaultLanguage;
    }

    this.downloadLanguage = function (language) {
        language = language || loadLanguage();

        //fix, in this point languge can be object { title: "English", flag: "en" }; or simple value "English"
        language = (language.title ? language.title : language);

        var deferred = $q.defer();

        BackendService.sendRequest("language/getLanguage", {language : language}, function (result) {
            if ("Map" in window) {
                if (result.translations) {
                    if (window.top.commonData.translations instanceof Map) {
                        window.top.commonData.translations.clear();
                    }
                    window.top.commonData.translations = new Map(result.translations);
                }
            }
            else {
                loadToArray(window.top.commonData.translations, result.translations);
            }

            language = getItem(instance.languages, "title", language);
            
            $rootScope.selectedLanguage = language;
            $storage.language = language;
            window.font = result.font || null;

            console.log("Language and font changed ", language.title, window.font || 'default')

            deferred.resolve();

            SQEvents.notifyListeners(SQEvents.get("LANGUAGE_CHANGED"), language);

            var awWindow = window.top.document.getElementById('algowizard-frame');
            if (awWindow) {
                awWindow = awWindow.contentWindow;
                if (awWindow && awWindow.eaWizardApp) awWindow.eaWizardApp.selectLanguage(language);
            }

            var sqxbusinessSelectLanguage = window.top.sqxbusinessSelectLanguage;
            if (sqxbusinessSelectLanguage) {
                sqxbusinessSelectLanguage(language);
            }

            instance.setFont();
        }, 'GET');

        return deferred.promise;
    }

    this.setFont = function (f) {
        var font = window.parent.font;

        $rootScope.font = "";

        if (font) {
            $rootScope.font = font + ", ";
            console.log(window.appConfig.product + " - setting font to " + font)
        }
    }

    this.tsq = function (originalText, params) {
        if ($rootScope.selectedLanguage.title != "English") {
            return Ltsqt(originalText, params);
        }

        //originalText = originalText ? originalText.trim().replace(/\s\s+/g, " ") : originalText; //remove multiple whitespaces
        return (params ? originalText.format(params) : originalText);
    }

    this.getLangShortCut = function () {
        return !$rootScope.selectedLanguage || !$rootScope.selectedLanguage.flag || $rootScope.selectedLanguage.flag == "en" ? "" : $rootScope.selectedLanguage.flag;
    }

    var $storage = $localStorage.$default();
    var defaultLanguage = { title: "English", flag: "en" };

    this.languages = SQConstants.getLanguages();
    $rootScope.selectedLanguage = loadLanguage();

    window.top.langDebugMode = SQConstants.getSettings().langDebugMode;

    if (!isMainApp() && !isAlgoWizard() && !isCodeEditor()) {
        window.parent.addListener("L-" + window.appConfig.product, SQEvents.get("LANGUAGE_CHANGED"), function (event, data) {
            $rootScope.language = data;
            $rootScope.selectedLanguage = data;

            instance.setFont();

            $rootScope.$applyAsync();
            SQEvents.notifyListeners(SQEvents.get("LANGUAGE_CHANGED"), data);
        });
    }

    var instance = this;
});