'use strict';
/** 
 * @property scope 
 * @property location
 * @property route
 * @property System
 * @property OdjeljenjeService
 */

var AppController = angular.module('app', [])
    .controller('AppController', [
        '$scope',
        '$timeout',
        '$location',
        '$route',
        '$locale',
        '$window',
        'Config',
        'System',
        'Global',
        '$routeParams',
        'NavigationService',
        'JezikService',
        function ($scope,$timeout, $location, $window, $locale, $route, Config, System, Global, $routeParams, NavigationService, JezikService) {
            $locale.id = $location.search().Culture || Globalize.culture().name;
            var cul = Globalize.culture().name;
            $scope.ui = {};
            if ($locale.id != cul) {
                Global.init($scope);
                $scope.language.current = $location.search().Culture || Globalize.culture().name;
            }
            else {
                $locale.id = 'sr';
                $scope.ui = {};
                Global.init($scope);
                $scope.language.current = 'sr';
            }


            $scope.language.current = $location.search().Culture || Globalize.culture().name;
            // Cacth all errors
            System.Broadcast.on($scope, Config.HTTP_ERROR_KEY, function (e, val) {
                switch (val) {
                    case 401:
                    case 403:
                        window.location.href = Config.API_LOGIN_URL + "?ReturnUrl=" + window.location.href;
                        break;
                    default:
                }
            });

            // Message
            System.Message.on($scope, function (e, message) {
                $scope.ui.alert = { type: message.type, text: message.value };
                $timeout(function () {
                    $scope.ui.alert = null;
                }, 5000);
            });

            // Cacth http status for loader
            System.Broadcast.on($scope, Config.HTTP_STATUS_KEY, function (e, val) {
                $scope.ui.loader = (val == Config.HTTP_REQUEST_STATUS.LOADING);
            });

            // catch route change
            $scope.$on('$routeChangeSuccess', function (scope, next, current) {
                // close all popovers
                angular.element(':qusion-popover').popover('close');
            });

            NavigationService.language($scope.navigationKey)
                .then(function (jezici) {
                    $scope.jezicine = jezici;
                    $scope.isActive = function (jezik) {
                        if (jezik.name == $locale.id)
                            return true;
                        return false;
                    };

                });

            // determines if provided link is active
            $scope.isActive = function (jezik) {
                if (jezik.name == $locale.id)
                    return true;
                return false;
            };

            $scope.setLang = function (q, la) {
                $locale.id = la;
                $scope.ui = {};
                
                Global.init($scope);
                $scope.language.current = la;
                $scope.Culture = q;
                $scope.query = q;
                angular.extend($scope.query, q);
                $location.search($scope.query);
                $scope.isActive = function (jezik) {
                    if (jezik.name == la)
                        return true;
                    return false;
                };
            };
  
        }
    ]);

