﻿"use strict";

// global navigation controller
app.controller("navController", ["$rootScope",
                                 "$log",
                                 "$scope",
                                 "$location",
                                 "localStorageService",
                                 "authService",
                                 "ngAuthSettings",
                                 "categoryDataService",
                                 "judgeRulesService",
                                 "navBroadcastService",
    function ($rootScope,
              $log,
              $scope,
              $location,
              localStorageService,
              authService,
              ngAuthSettings,
              categoryDataService,
              judgeRulesService,
              navBroadcastService) {
        var self = this;

        $scope.judgeRulesService = judgeRulesService;
        $scope.navBroadcastService = navBroadcastService;
        $scope.fullname = "";
        $scope.emailAddress = "";
        $scope.categories = [];
        $scope.enableLocaleSelection = $rootScope.enableLocaleSelection;
        $scope.goToHome = _goToHome;

        function _goToHome() {
            var authData = localStorageService.get(ngAuthSettings.authDataKey);
            $location.path(authData.homeUrl);
        }

        $scope.getUserInfo = function () {
            authService.getUserInfo().then(function (results) {
                $scope.fullname = results.data.fullname;
                $scope.emailAddress = results.data.emailAddress;
            },
            function (error) {
                console.error('error on getUserInfo');
            });
        };
        $scope.isActive = function (path) {
            return $location.path().substr(0, path.length) == path;
        };
        $scope.showBackButton = function () {
            return ($rootScope.isDetailsView);
        };
        
        $scope.goCategoryBack = function () {
            if (typeof $scope.detailsCategory === "undefined" || $scope.detailsCategory === null) {
                $location.path("/");
            }
            else {
                return ($location.path('/categorySubmissions/' + $scope.detailsCategory.id));
            }
        };

        $scope.isLoggedIn = function () {
            var authData = localStorageService.get(ngAuthSettings.authDataKey);
            if (typeof authData !== "undefined" && authData !== null)
                return true;
            return false;
        };
        $scope.logout = function () {
            $scope.closeMenuHint();

            $scope.fullname = "";
            $scope.emailAddress = "";
            
            authService.logOut();
        };

        $scope.closeMenuHint = function () {
            $log.log("closeMenuHint called");
            $rootScope.showMenuHint = false;
        };

        $scope.changeLocale = function (locale) {
            var temp = window.location.href.replace("en#", "__TOM__LOCALE__").replace("pt#", "__TOM__LOCALE__").replace("es#", "__TOM__LOCALE__");
            window.location.href = temp.replace("__TOM__LOCALE__", locale + "#");
        }

        function _setNavigationFlags() {
            $scope.getUserInfo();
            $scope.showHelp = judgeRulesService.showHelp;
            $scope.showChangePassword = !judgeRulesService.hideChangePassword();
            $scope.showUserProfile = judgeRulesService.showUserProfile();
        }

        $scope.$on("logged in", function () {
            _setNavigationFlags();
        });
        $scope.init = function () {
            judgeRulesService.resetForceProfilePage();
            $rootScope.showMenuHint = false;
            if ($rootScope.navInited) {                
                self.initUI();
            }
            else {
                console.i('init nav controller ...');

                if ($scope.isLoggedIn()) {
                    _setNavigationFlags();
                }

                var urlPath = window.location; // can't use $location here, as you can only inject providers into the config
                $scope.currentLocale = urlPath.pathname.substr(1);

                $rootScope.navInited = true;

                if ($scope.enableLocaleSelection) {
                    $scope.locales = [{
                        name: "English",
                        code: "en"
                    }, {
                        name: "Portuguese",
                        code: "pt"
                    }, {
                        name: "Spanish",
                        code: "es"
                    }];
                }

                self.initUI();
            }
        };

        self.initUI = function () {
            if (typeof $scope.hideCategoryList === "undefined") {
                if (typeof judgeRulesService.hideCategoryList === "function") {
                    $scope.hideCategoryList = judgeRulesService.hideCategoryList();
                }
                else if (typeof judgeRulesService.hideCategoryList !== "undefined") {
                    $scope.hideCategoryList = judgeRulesService.hideCategoryList;
                }
                else {
                    $scope.hideCategoryList = false;
                }
            }
        };
        // ----- Main routine
        $scope.init();
    }]);
