/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
var EntaxyH2;
(function (EntaxyH2) {
    EntaxyH2.log.debug("loading navigation");
    var TAB_CONFIG = {
        attributes: {
            title: 'Attributes',
            route: '/h2/attributes'
        },
        operations: {
            title: 'Operations',
            route: '/h2/operations'
        },
        chart: {
            title: 'Chart',
            route: '/h2/charts'
        },
        console: {
            title: 'H2 Console',
            route: '/h2/console'
        }
    };
    EntaxyH2._module
    .config(configureRoutes)
    .component('entaxyH2Navigation', {
        template: `<hawtio-tabs tabs="$ctrl.tabs" on-change="$ctrl.goto(tab)"></hawtio-tabs>`,
        controller: EntaxyH2NavigationController
    })
    .name;
    EntaxyH2.log.debug("loaded navigation " + EntaxyH2.navigationModule);

    function EntaxyH2NavigationController($scope, $location, workspace, localStorage, jolokia) {
        'ngInject';
        var ctrl = this;
        this.$location = $location;
        entaxyH2JmxDomain = localStorage['entaxyH2JmxDomain'] || "org.h2";

		entaxyH2workspace = workspace;

        $scope.$on('jmxTreeClicked', function () {
            ctrl.tabs = getTabs();
            var tab = _.find(ctrl.tabs, { path: ctrl.$location.path() });
            if (!tab) {
                tab = ctrl.tabs[0];
            }
            ctrl.$location.path(tab.path);
        });

        EntaxyH2NavigationController.prototype.$onInit = function () {
            this.tabs = getTabs();
        };

        EntaxyH2NavigationController.prototype.goto = function (tab) {
            this.$location.path(tab.path);
        };

        function getTabs() {

            var tabs = [];
            var enabledRoutes = Object.keys(TAB_CONFIG)
                .map(function (config) { return TAB_CONFIG[config].route; })
                .filter(function (route) { return _.startsWith(route, '/h2'); });
            if (enabledRoutes.length > 0) {
				
				if (shouldShowConsole(workspace)) {
					tabs.push(new Nav.HawtioTab(TAB_CONFIG.console.title, TAB_CONFIG.console.route));	
				}
				
                tabs.push(new Nav.HawtioTab(TAB_CONFIG.attributes.title, TAB_CONFIG.attributes.route));
                tabs.push(new Nav.HawtioTab(TAB_CONFIG.operations.title, TAB_CONFIG.operations.route));
                tabs.push(new Nav.HawtioTab(TAB_CONFIG.chart.title, TAB_CONFIG.chart.route));
            }
            return tabs;
        }

		function shouldShowConsole(workspace) {
			return workspace.selectionHasDomain(entaxyH2JmxDomain)
				&& workspace.selection 
				&& workspace.selection.entries.path;  
		}
		
    }
    EntaxyH2NavigationController.$inject = ['$scope', '$location', 'workspace', 'localStorage', 'jolokia']

    function configureRoutes($routeProvider) {
       $routeProvider.
        when('/h2/attributes', { templateUrl: 'plugins/jmx/html/attributes/attributes.html' }).
        when('/h2/operations', { template: '<operations></operations>' }).
        when('/h2/charts', { templateUrl: 'plugins/jmx/html/charts.html' }).
		when('/h2/console', { template: '<entaxy-h2-console></entaxy-h2-console>' });
    }
    configureRoutes.$inject = ['$routeProvider'];

})(EntaxyH2 || (EntaxyH2 = {}));

