/// <reference path="../../../Scripts/underscore.js" />
/// <reference path="../../common/utils/enums.js" />
/// <reference path="../nav-bar-util.js" />

// AppNavBarController controller for the navigation bar of the application. This controller is responsible for 
// manage the different types of the tabs and corresponding menus.
frameworkModule.controller('AppNavTabController', ['$scope', '$rootScope', '$log', 'enums', 'appTabManager', 'appRoute', '$timeout', 
function ($scope, $rootScope, $log, enums, appTabManager, appRoute, $timeout)
{
        'use strict';
        $log.debug('AppNavTabController.ctor()...');

        var triggeredbyUser = false;
        // select the specified tab from UI or code
        var selectTab = function (tab) {
            $log.debug('AppNavTabController.selectTab(): ' + JSON.stringify({
                tabId: tab.tabId,
                tabName: tab.tabName
            }));

            if ($scope.selectedTab !== tab) {
                appRoute.navigateFromTab(tab, triggeredbyUser);
                triggeredbyUser = false;
                $scope.selectedTab = tab;
                appTabManager.selectOpenedTab(tab);
            }           
        };

        // click the specified tab from UI
        var clickTab = function (tab) {
            $log.debug('AppNavTabController.clickTab(): ' + JSON.stringify({
                tabId: tab.tabId,
                tabName: tab.tabName
            }));

            triggeredbyUser = true;

           
        };

        var navigate = function (tabType, eventData) {
            $timeout(function () {
                appRoute.navigate(tabType, eventData);
            }, 0);
        };

        (function initialize() {

            $log.debug('AppNavTabController.initialize()...');

            // properties
            $scope.selectedTab = null;
            $scope.tabs = appTabManager.tabs;

            // methods
            $scope.selectTab = selectTab;
            $scope.clickTab = clickTab;
            $scope.closeTab = function (index) {
                $timeout(function () {
                    appTabManager.closeTabByIndex(index);

                    appRoute.navigateFromTab($scope.tabs[0], triggeredbyUser);
                    triggeredbyUser = false;
                    $scope.selectedTab = $scope.tabs[0];
                    appTabManager.selectOpenedTab($scope.tabs[0]);
                }, 0);
            };
            
            // register events
            $rootScope.$on('event:editChart', function (e, d)
            {
                navigate(enums.tabType.chartEdit,
                new appRoute.chartSettingEventData({
                    needActivate: true,
                    chartId: d
                }));
                $scope.selectedTab = null;
            });

            // register events
            $rootScope.$on('event:closeCurrentTab', function ()
            {
                appTabManager.closeCurrentTab();

                appRoute.navigateFromTab($scope.tabs[0], triggeredbyUser);
                triggeredbyUser = false;
                $scope.selectedTab = $scope.tabs[0];
                appTabManager.selectOpenedTab($scope.tabs[0]);
            });

            // always load worklist as primary tab at the very beginning

            
            navigate(enums.tabType.dashboard, 
                new appRoute.comonEventData({
                    needActivate: true
                }));
            

            // register events
            $scope.$on('$stateChangeStart',
                function (event, toState, toParams)
                {
                    // transitionTo() promise will be rejected with 
                    // a 'transition prevented' error
                   
                    if (toState == null) {
                        return;
                    }

                    var url = toState.url;

                    var paramList = Object.keys(toParams);

                    _.forEach(paramList, function (item)
                    {
                        url = url.replace("{" + item + "}", toParams[item]);
                    });
                    
                    if (typeof (toState.name) !== 'undefined')
                    {
                       

                        $log.debug("state name:" + toState.name);

                        navigate(enums.tabType.commonUrl,
                        new appRoute.comonEventData({
                            needActivate: true,
                            stateName:toState.name,
                            url: url,
                            param: toParams.param
                        }));
                    }
                });

        }());
    }
]);