/*-
 * ~~~~~~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 Entaxy;
(function (Entaxy) {
    Entaxy._module.component('entaxyHeader', {
        template:
        `
          <div class="entaxy-header">
            <header>
              <h1 id="entaxy-header-title" ng-class="{'changed': $ctrl.isChanged}">{{$ctrl.title}}</h1>
              <button type="button" class="btn btn-link label-description-popover"
                      popover-placement="{{$ctrl.popoverPlacement}}" popover-trigger="'outsideClick'"
                      popover-class="entaxy-header-popover" popover-is-open="$ctrl.isPopoverOpen"
                      uib-popover="{{$ctrl.objectName}}" ng-if="$ctrl.objectName">
                <span class="pficon pficon-info"></span>
              </button>
            </header>
          </div>
        `,
        controller: entaxyHeaderController
    })
        .name;

    function entaxyHeaderController(workspace, $scope, entaxyPrivateObjectsCacheService) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function () {
            let selectedMbean = workspace.getSelectedMBean();
            populateData(selectedMbean);

            $scope.$on('jmxTreeClicked', function (event, selectedNode) {
                populateData(selectedNode);
            });
        }

        $scope.$watch('$ctrl.isPopoverOpen', function (newValue) {
            if (newValue) {

                let popoverElements = document.getElementsByClassName('entaxy-header-popover');
                let popoverElement = popoverElements ? popoverElements[0] : null;

                if (popoverElement) {
                    let infoSpan = document.getElementsByClassName('pficon-info')[0];
                    let offsetLeft = infoSpan.offsetLeft + 18;

                    let headerTitle = document.getElementById('entaxy-header-title');
                    let offsetRight = infoSpan.offsetLeft - headerTitle.offsetLeft + 12;

                    let header = document.getElementsByClassName('entaxy-header')[0];
                    let isPositioningLeft = (header.offsetWidth - offsetLeft) >= offsetRight;

                    ctrl.popoverPlacement = isPositioningLeft ? 'bottom-left' : 'bottom-right';

                    popoverElement.style.maxWidth = isPositioningLeft ?
                        'calc(100% - ' + offsetLeft + 'px)' : offsetRight + 'px';
                }
            }
        });

        function populateData(selectedNode) {
            ctrl.title = selectedNode.title;
            ctrl.objectName = selectedNode.objectName;

            if (ctrl.objectName) {
                ctrl.isChanged = entaxyPrivateObjectsCacheService.isChanged(ctrl.objectName);
            } else {
                ctrl.isChanged = false;
                let parentNode = selectedNode.parent;
                if (parentNode && parentNode.objectName &&
                        entaxyPrivateObjectsCacheService.hasAddedChildren(parentNode.objectName)) {
                    let addedChildrenInfo = entaxyPrivateObjectsCacheService.getAddedObjectsInfo(parentNode.objectName);
                    if (addedChildrenInfo.find(addedChildInfo =>
                            selectedNode.key === parentNode.key + '-' + addedChildInfo.ui.id)) {
                        ctrl.isChanged = true;
                    }
                }
            }
        }
    }
    entaxyHeaderController.$inject = ['workspace', '$scope', 'entaxyPrivateObjectsCacheService'];

})(Entaxy || (Entaxy = {}));
