/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-cicd-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 EntaxyCICD;
(function (EntaxyCICD) {
    EntaxyCICD.log.debug("loading navigation");

    EntaxyCICD._module
        .config(configureRoutes)
        .component('entaxyCiCdNavigation', {
            template: `<hawtio-tabs tabs="$ctrl.tabs" on-change="$ctrl.goto(tab)"></hawtio-tabs>`,
            controller: EntaxyCICDNavigationController
        })
        .name;
    EntaxyCICD.log.debug("loaded navigation " + EntaxyCICD.navigationModule);

    function EntaxyCICDNavigationController($scope, $location, workspace, localStorage, jolokia, $rootScope) {
        'ngInject';
        let ctrl = this;
        this.$location = $location;
        let selected = workspace.selection;
        entaxyCICDJmxDomain = localStorage['entaxyCICDJmxDomain'] || "ru.entaxy.cicd";

        $scope.$on('jmxTreeClicked', function (event, selectedNode) {
            ctrl.tabs = getTabs();
            let tab;
            if (!selected) {
                selected = selectedNode;
                tab = _.find(ctrl.tabs, { path: ctrl.$location.path() });
                if (!tab) {
                    tab = ctrl.tabs[0];
                }
            } else {
                if (JSON.stringify(selected.folderNames) !== JSON.stringify(selectedNode.folderNames)) {
                    selected = selectedNode;
                    tab = ctrl.tabs[0];
                } else {
                    tab = _.find(ctrl.tabs, {path: ctrl.$location.path()});
                    if (!tab) {
                        tab = ctrl.tabs[0];
                    }
                }
            }
            ctrl.$location.path(tab.path);
        });

        EntaxyCICDNavigationController.prototype.$onInit = function () {
            if (!workspace.selection) {
                this.tabs = [];
            } else {
                this.tabs = getTabs();
            }
        };

        EntaxyCICDNavigationController.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, '/entaxy-cicd'); });
            if (enabledRoutes.length > 0) {
                if (workspace.getSelectedMBeanName()) {
                    if (workspace.hasDomainAndProperties(entaxyCICDJmxDomain, { module: 'projects' })) {
                        tabs.push(new Nav.HawtioTab(TAB_CONFIG.projects.title, TAB_CONFIG.projects.route));
                    } else if (workspace.hasDomainAndProperties(entaxyCICDJmxDomain, { module: 'applications' })) {
                        tabs.push(new Nav.HawtioTab(TAB_CONFIG.applications.title, TAB_CONFIG.applications.route));
                    }
                } else if (shouldShowProjectsTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.projects.title, TAB_CONFIG.projects.route));
                } else if (shouldShowVersionsTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.projectVersions.title, TAB_CONFIG.projectVersions.route));
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.project.title, TAB_CONFIG.project.route));
                } else if (shouldShowProjectTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.project.title, TAB_CONFIG.project.route));
                } else if (shouldShowBuildInfoTabs()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.buildInfoItems.title, TAB_CONFIG.buildInfoItems.route));
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.buildInfoRequirements.title, TAB_CONFIG.buildInfoRequirements.route));
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.buildInfoComponents.title, TAB_CONFIG.buildInfoComponents.route));
                } else if (shouldShowApplicationsTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.applications.title, TAB_CONFIG.applications.route));
                } else if (shouldShowVersionsRevisionsTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.versionsRevisionsInfo.title, TAB_CONFIG.versionsRevisionsInfo.route));
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.application.title, TAB_CONFIG.application.route));
                } else if (shouldShowApplicationTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.application.title, TAB_CONFIG.application.route));
                } else if (shouldShowRevisionTab()) {
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.revision.title, TAB_CONFIG.revision.route));
                    tabs.push(new Nav.HawtioTab(TAB_CONFIG.application.title, TAB_CONFIG.application.route));
                }
            }
            return tabs;
        }

        function shouldShowProjectsTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'projects' &&
                (selectionFolderNames.length === 2 || selectionFolderNames.length === 3);
        }

        function shouldShowVersionsTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'projects' && selectionFolderNames.length === 4;
        }

        function shouldShowProjectTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'projects' &&
                (selectionFolderNames.length === 4 || selectionFolderNames.length === 5);
        }

        function shouldShowBuildInfoTabs() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'projects' && selectionFolderNames.length === 6;
        }

        function shouldShowApplicationsTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'applications' &&
                (selectionFolderNames.length === 2 || selectionFolderNames.length === 3);
        }

        function shouldShowApplicationTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'applications' && selectionFolderNames.length === 5;
        }

        function shouldShowVersionsRevisionsTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'applications' && selectionFolderNames.length === 4;
        }

        function shouldShowRevisionTab() {
            let selectionFolderNames = workspace.selection.folderNames;
            return selectionFolderNames[1] === 'applications' && selectionFolderNames.length === 6;
        }

//        global event for entaxy extras
        $rootScope.$emit('entaxyCiCdNavigationInited', $scope);
    }
    EntaxyCICDNavigationController.$inject = ['$scope', '$location', 'workspace', 'localStorage', 'jolokia', '$rootScope']

    function configureRoutes($routeProvider) {
        $routeProvider.
        when(TAB_CONFIG.projects.route,
            { template: '<entaxy-ci-cd-projects></entaxy-ci-cd-projects>' }).
        when(TAB_CONFIG.projectVersions.route,
            { template: '<entaxy-ci-cd-project-versions></entaxy-ci-cd-project-versions>' }).
        when(TAB_CONFIG.project.route,
            { template: '<entaxy-ci-cd-project></entaxy-ci-cd-project>' }).
        when(TAB_CONFIG.buildInfoItems.route,
            { template: '<entaxy-ci-cd-build-info type="Items"></entaxy-ci-cd-build-info>' }).
        when(TAB_CONFIG.buildInfoRequirements.route,
            { template: '<entaxy-ci-cd-build-info type="Requirements"></entaxy-ci-cd-build-info>' }).
        when(TAB_CONFIG.buildInfoComponents.route,
            { template: '<entaxy-ci-cd-build-info type="Components"></entaxy-ci-cd-build-info>' }).
        when(TAB_CONFIG.applications.route,
            { template: '<entaxy-ci-cd-applications></entaxy-ci-cd-applications>' }).
        when(TAB_CONFIG.application.route,
            { template: '<entaxy-ci-cd-application></entaxy-ci-cd-application>' }).
        when(TAB_CONFIG.versionsRevisionsInfo.route,
            { template: '<entaxy-ci-cd-versions-revisions-info></entaxy-ci-cd-versions-revisions-info>' }).
        when(TAB_CONFIG.revision.route,
            { template: '<entaxy-ci-cd-revision></entaxy-ci-cd-revision>' });
    }
    configureRoutes.$inject = ['$routeProvider'];

})(EntaxyCICD || (EntaxyCICD = {}));

let TAB_CONFIG = {
    projects: {
        title: 'Projects',
        route: '/entaxy-cicd/projects'
    },
    project: {
        title: 'Project',
        route: '/entaxy-cicd/project'
    },
    projectVersions: {
        title: 'Versions',
        route: '/entaxy-cicd/project/versions'
    },
    buildInfoItems: {
        title: 'Items',
        route: '/entaxy-cicd/project/build-info/items'
    },
    buildInfoRequirements: {
        title: 'Requirements',
        route: '/entaxy-cicd/project/build-info/requirements'
    },
    buildInfoComponents: {
        title: 'Components',
        route: '/entaxy-cicd/project/build-info/components'
    },
    applications: {
        title: 'Applications',
        route: '/entaxy-cicd/applications'
    },
    application: {
        title: 'Application',
        route: '/entaxy-cicd/application'
    },
    versionsRevisionsInfo: {
        title: 'Versions & Revisions',
        route: '/entaxy-cicd/application/versions-revisions-info'
    },
    revision: {
        title: 'Revision',
        route: '/entaxy-cicd/application/revision'
    }
};
