/*
 * This file is part of EspoCRM and/or AtroCore.
 *
 * EspoCRM - Open Source CRM application.
 * Copyright (C) 2014-2019 Yuri Kuznetsov, Taras Machyshyn, Oleksiy Avramenko
 * Website: http://www.espocrm.com
 *
 * AtroCore is EspoCRM-based Open Source application.
 * Copyright (C) 2020 AtroCore GmbH.
 *
 * AtroCore as well as EspoCRM is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * AtroCore as well as EspoCRM is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with EspoCRM. If not, see http://www.gnu.org/licenses/.
 *
 * The interactive user interfaces in modified source and object code versions
 * of this program must display Appropriate Legal Notices, as required under
 * Section 5 of the GNU General Public License version 3.
 *
 * In accordance with Section 7(b) of the GNU General Public License version 3,
 * these Appropriate Legal Notices must retain the display of the "EspoCRM" word
 * and "AtroCore" word.
 */

Espo.define('language', ['ajax'], function (Ajax) {

    var Language = function (cache) {
        this.cache = cache || null;
        this.data = {};
        this.fallbackData = {}
        this.name = 'default';
        this.fallbackName = null;
        this.localeId = null;
    };

    _.extend(Language.prototype, {

        data: null,

        cache: null,

        url: 'I18n',

        has: function (name, category, scope) {
            if (scope in this.data) {
                if (category in this.data[scope]) {
                    if (name in this.data[scope][category]) {
                        return true;
                    }
                }
            }
        },

        get: function (scope, category, name) {
            let data = this.data;
            if (this.fallbackData && !this.has(name, category, scope)) {
                data = this.fallbackData;
            }
            if (scope in data) {
                if (category in data[scope]) {
                    if (name in data[scope][category]) {
                        return data[scope][category][name];
                    }
                }
            }
            if (scope == 'Global') {
                return name;
            }
            return false;
        },

        translate: function (name, category, scope) {
            scope = scope || 'Global';
            category = category || 'labels';
            var res = this.get(scope, category, name);
            if (res === false && scope != 'Global') {
                res = this.get('Global', category, name);
            }
            return res;
        },

        translateOption: function (value, field, scope) {
            var translation = this.translate(field, 'options', scope);
            if (typeof translation != 'object') {
                translation = {};
            }
            return translation[value] || value;
        },

        loadFromCache: function (loadDefault, isFallback = false) {
            var name = isFallback ? this.fallbackName : this.name;
            if (!isFallback && loadDefault) {
                name = 'default';
            }
            if (this.cache) {
                var cached = this.cache.get('app', 'language-' + name);
                if (cached) {
                    if (isFallback) {
                        this.fallbackData = cached;
                        window.SvelteLanguage.setFallbackTranslations(cached);
                    } else {
                        this.data = cached;
                        window.SvelteLanguage.setTranslations(cached);
                    }
                    return true;
                }
            }
            return null;
        },

        clearCache: function () {
            if (this.cache) {
                this.cache.clear('app', 'language-' + this.name);
                this.cache.clear('app', 'language-' + this.fallbackName);
            }
        },

        storeToCache: function (loadDefault, isFallback = false) {
            var name = isFallback ? this.fallbackName : this.name;
            if (!isFallback && loadDefault) {
                name = 'default';
            }
            if (this.cache) {
                this.cache.set('app', 'language-' + name, isFallback ? this.fallbackData : this.data);
            }
        },

        load: function (callback, disableCache, loadDefault, fallback = null) {
            this.once('sync', callback);
            if (!disableCache) {
                if (this.loadFromCache(loadDefault)) {
                    this.trigger('sync');
                    return;
                } else {
                    this.fetch(disableCache, loadDefault, fallback);
                }
                if (this.loadFromCache(false, true)) {
                    this.trigger('sync')
                } else {
                    this.fetchFallback(disableCache, fallback);
                }


            }

        },

        fetch: function (disableCache, loadDefault) {
            const data = {}
            if (loadDefault) {
                data.default = true;
            } else if (this.localeId) {
                data.locale = this.localeId
            }
            Ajax.getRequest(this.url, data).then(function (data) {
                this.data = data;
                window.SvelteLanguage.setTranslations(data);
                if (!disableCache) {
                    this.storeToCache(loadDefault);
                }
                this.trigger('sync');
            }.bind(this));
        },

        fetchFallback: function (disableCache) {
            if (this.fallbackName) {
                Ajax.getRequest(this.url, { locale: this.fallbackName }).then(function (data) {
                    this.fallbackData = data;
                    window.SvelteLanguage.setFallbackTranslations(data);
                    if (!disableCache) {
                        this.storeToCache(false, true);
                    }
                    this.trigger('sync');
                }.bind(this));
            }
        },

        sortFieldList: function (scope, fieldList) {
            return fieldList.sort(function (v1, v2) {
                return this.translate(v1, 'fields', scope).localeCompare(this.translate(v2, 'fields', scope));
            }.bind(this));
        },

        sortEntityList: function (entityList, plural) {
            var category = 'scopeNames';
            if (plural) {
                category += 'Plural';
            }
            return entityList.sort(function (v1, v2) {
                return this.translate(v1, category).localeCompare(this.translate(v2, category));
            }.bind(this));
        }

    }, Backbone.Events);

    return Language;

});