/**
 * Created by robin on 12/15/16.
 */

(function () {
    'use strict';

    angular.module('appSdk')
        .factory('AppUtilsBusiness', ['$rootScope', '$translate', 'luBusyBusiness', 'luRequest',
            function($rootScope, $translate, luBusyBusiness, luRequest) {

            var AppUtilsBusiness = {};

            AppUtilsBusiness.toolbarTitle = null;

            AppUtilsBusiness.convertOrderBy = function(orderBy)
            {
                if (orderBy[0] == '-') {
                    orderBy = orderBy.substr(1) + ":DESC";
                }
                else {
                    orderBy = orderBy + ":ASC";
                }
                return orderBy;
            };

            AppUtilsBusiness.setTitle = function(title)
            {
                $rootScope.title = title + ' - ' + AppUtilsBusiness.tr('common.appName');
            };

            AppUtilsBusiness.setToolbarTitle = function(title)
            {
                AppUtilsBusiness.toolbarTitle = title;
                $rootScope.$emit('toolbar.title', {title: title});
            };

            AppUtilsBusiness.getToolbarTitle = function()
            {
                return AppUtilsBusiness.toolbarTitle;
            };

            AppUtilsBusiness.mdTableLabels = function()
            {
                return {
                    of: AppUtilsBusiness.tr('common.pagination.of'),
                    page: AppUtilsBusiness.tr('common.pagination.page'),
                    rowsPerPage: AppUtilsBusiness.tr('common.pagination.rowsPerPage')
                };
            };

            AppUtilsBusiness.cgBusy = function(luBusyGroup)
            {
                return {
                    promise: luBusyBusiness.promise(luBusyGroup),
                    message: AppUtilsBusiness.tr('common.busyLoading'),
                    templateUrl:'views/busy-template.html'
                };
            };

            AppUtilsBusiness.tr = function(id, data)
            {
                return $translate.instant(id, data);
            };

            AppUtilsBusiness.addApiVersionChangedCallback = function(callback)
            {
                var apiVersion = null;
                luRequest.addHook('resolve', function(url, method, dataGet, dataPost, luBusyGroups, data)
                {
                    if (apiVersion == null) {
                        apiVersion = data.version;
                    }
                    else if (data.version != null && data.version != apiVersion) {
                        callback(apiVersion, data.version);
                        apiVersion = data.version;
                    }
                });

                luRequest.addHook('reject', function(url, method, dataGet, dataPost, luBusyGroups, error)
                {
                    if (apiVersion == null) {
                        apiVersion = error.data.version;
                    }
                    else if (error.data.version != null && error.data.version != apiVersion) {
                        callback(apiVersion, error.data.version);
                        apiVersion = error.data.version;
                    }
                });
            };

            AppUtilsBusiness.articleTypes = ['persons', 'locations', 'countries'];

            AppUtilsBusiness.articleFieldTypes = ['date/date', 'text/plain', 'number/number'];

            AppUtilsBusiness.articleFieldProperties = ['birthDate', 'deathDate', 'capital', 'language', 'area', 'population'];

            return AppUtilsBusiness;
        }]);
})();