/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-branding-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~~~~~~
 */
 /**
 * 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) {

    let isUsedColorSchemeDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;

    if (document.readyState == "complete") {
        if (isUsedColorSchemeDark) {
            document.body.classList.add('color_scheme_dark');
        } else {
            document.body.classList.add('color_scheme_default');
        }
    }

    let appLogoUrl = isUsedColorSchemeDark ?
            `${Branding.contextPath}/plugin/img/entaxy_white_transparent_without_paddings.png` :
            `${Branding.contextPath}/plugin/img/entaxy_color_transparent_without_paddings.png`;

    configManager.config = {
      "branding": {
        "appName": "Entaxy Management Console",
        "appLogoUrl": appLogoUrl,
        "companyLogoUrl": `${Branding.contextPath}/plugin/img/entaxy_white_transparent_without_paddings.png`,
        "css": `${Branding.contextPath}/plugin/css/entaxy.css`,
        "favicon": `${Branding.contextPath}/plugin/img/favicon_e_color_transparent.png`
      },
      "login": {
        "description": "Entaxy Management Console",
        "links": [
          {
            "url": "/entaxy-docs/entaxy-core/latest/index.html",
			"target": "_blank",
            "text": "User Manual"
          },
          {
            "url": "/camel-docs/manual/3.4.x/index.html",
			"target": "_blank",
            "text": "Camel Manual"
          },
          {
            "url": "https://entaxy.ru/",
            "text": "Website"
          }
        ]
      },
      "about": {
        "title": "Entaxy Management Console",
        "productInfo": [],
        "additionalInfo": "",
        "imgSrc": `${Branding.contextPath}/plugin/img/entaxy_white_transparent.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">');


