/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2021 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */
/**
 * The main entry point for the Simple module
 */
var Entaxy = (function (Entaxy) {

    /**
    * The name of this plugin
    */
    Entaxy.pluginName = 'entaxy-plugin';

    /**
    * This plugin's logger instance
    */
    Entaxy.log = Logger.get('entaxy-plugin');

    /**
    * The top level path of this plugin on the server
    */
    Entaxy.contextPath = "/entaxy-plugin/";

    Entaxy.log.info("loading entaxy plugin")
    Entaxy._module = angular.module(Entaxy.pluginName, [
        'angularResizable'
    ])
    .component('entaxy', {
        template:
           `<div class="tree-nav-layout">
                <div class="sidebar-pf sidebar-pf-left" resizable r-directions="['right']">
                  <entaxy-tree-header></entaxy-tree-header>
                  <entaxy-tree></entaxy-tree>
                </div>
                <div class="tree-nav-main">
                  <jmx-header></jmx-header>
                  <entaxy-navigation></entaxy-navigation>
                  <div class="contents" ng-view></div>
                </div>
           </div>
          `
          })
    .run(configurePlugin);

  function configurePlugin(mainNavService, workspace, helpRegistry, preferencesRegistry, localStorage, preLogoutTasks, documentBase, $templateCache, $rootScope) {
        var entaxyJmxDomain = localStorage['entaxyJmxDomain'] || "ru.entaxy.esb";
        mainNavService.addItem({
            title: 'Entaxy',
            basePath: '/entaxy',
            template: '<entaxy></entaxy>',
            isValid: function () { return workspace.treeContainsDomainAndProperties(entaxyJmxDomain); }
        });

        function mBeansTreeLocalization(children) {
        	if (children && children.length >0) {
        		for (var i=0; i<children.length; i++) {
        			let c = children[i];
        			if (L10N.hasOwnProperty(c.title)) {
        				c.title = L10N[c.title];
        			}
        			if (c.children && c.children.length > 0) {
        				mBeansTreeLocalization(c.children);
        			}
        		}
        	}
        }

//      global event for entaxy extras
        $rootScope.$emit('entaxyWorkspacePreparing', workspace);

        workspace.addNamedTreePostProcessor('localize', (tree) => {
        	 if (tree) {
        		 let domain = tree.get(entaxyJmxDomain);
        		 if (domain) {
        			 console.log('domain ');
        			 console.log(domain.children);
        			 mBeansTreeLocalization(domain.children);
        		 }
        	 }
        });

        // clean up local storage upon logout
/*        preLogoutTasks.addTask('CleanupArtemisCredentials', function () {
            Artemis.log.debug("Clean up Artemis credentials in local storage");
            localStorage.removeItem('artemisUserName');
            localStorage.removeItem('artemisPassword');
        }); */
    }
    configurePlugin.$inject = ['mainNavService', 'workspace', 'helpRegistry', 'preferencesRegistry', 'localStorage', 'preLogoutTasks', 'documentBase', '$templateCache','$rootScope'];

  return Entaxy;

})(Entaxy || {});

// tell the Hawtio plugin loader about our plugin so it can be
// bootstrapped with the rest of AngularJS
hawtioPluginLoader.addModule(Entaxy.pluginName);

var L10N = {
		platform: 'Платформа',
		adapters: 'Адаптеры',
		factories: 'Фабрики',
		runtime:'Состояние',
		connections:'Коннекции',
		profiles:'Профили',
		connectors:'Коннекторы',
		routes:'Маршруты'
	};
