/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-branding-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 Branding module
 */
var Branding = (function (Branding) {

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

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

  /**
   * The top level path of this plugin on the server
   */
  Branding.contextPath = '/entaxy-branding';
  if (hawtioPluginLoader.getPlugins().hasOwnProperty(Branding.pluginName)) {
    Branding.contextPath = hawtioPluginLoader.getPlugins()[Branding.pluginName]['Context'];
  }

  /**
   * This plugin's AngularJS module instance.
   */
  Branding.module = angular.module(Branding.pluginName, [])
    .run(initPlugin);

  /**
   * Here you can overwrite hawtconfig.json by putting the JSON
   * data directly to configManager.config property.
   */
  function initPlugin(configManager, aboutService, $templateCache) {
    configManager.config = {
      "branding": {
        "appName": "Entaxy Management Console",
        "appLogoUrl": `${Branding.contextPath}/plugin/img/entaxy-white.png`,
        "companyLogoUrl": `${Branding.contextPath}/plugin/img/entaxy.png`,
        "css": `${Branding.contextPath}/plugin/css/entaxy.css`,
        "favicon": `${Branding.contextPath}/plugin/img/favicon.png`
      },
      "login": {
        "description": "Entaxy Management Console",
        "links": [
          {
            "url": "/entaxy-docs/index.html",
			"target": "_blank",
            "text": "User Manual"
          },
          {
            "url": "https://entaxy.ru/",
            "text": "Website"
          }
        ]
      },
      "about": {
        "title": "Entaxy Management Console",
        "productInfo": [],
        "additionalInfo": "",
        "imgSrc": `${Branding.contextPath}/plugin/img/entaxy.png`
      },
      "disabledRoutes": []
    };

	entaxyTemplateCache = $templateCache; 

    aboutService.addProductInfo('Entaxy', '${project.version}');
    // Calling this function is required to apply the custom css and
    // favicon settings
    Core.applyBranding(configManager);

    Branding.log.info(Branding.pluginName, "loaded");

  }
  initPlugin.$inject = ['configManager', 'aboutService', '$templateCache'];

  return Branding;

})(Branding || {});

// !! that's absolutely awful !!
// but there's no other way to make those links open in new window
window.linkModifier = window.setInterval(function() {
	if (document.readyState == "complete" && document.getElementsByTagName("footer").item(0)) {
            var collection = document.getElementsByTagName("footer").item(0).getElementsByTagName("a");
//            collection.map(el => return el.item(0).setAttribute("target", "_blank"));
            for (var i=0; i<collection.length; i++)
                collection.item(i).setAttribute("target", "_blank");
            window.clearInterval(window.linkModifier);
	}
}, 500);	

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

// entaxyTemplateCache.get("app/src/login/login.component.html")
//	.replace('<a href="{{link.url}}">', '<a href="{{link.url}}" target="_blank">');


