/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2023 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('entaxyConnectorsWithFlow', {
        bindings: {
            direction: '@',
            connectors: '<',
            profileName: '<',
            editCustomizationPointFn: '<'
        },
        template:
        `
          <div class="connectors-group-container">
            <span class="connectors-group-title">{{$ctrl.connectorsGroupTitle}}</span>
            <div class="connectors-with-flow-container">
              <div class="connectors-container">
                <div class="connector-block" ng-repeat="connector in $ctrl.connectors"
                        ng-click="$ctrl.editConnectorProperties(connector, $event)">
                  <span>{{connector.name}}</span>
                  <div id="{{connector.name}}_{{customizationPoint}}" class="customization-point-block"
                        ng-repeat="customizationPoint in $ctrl.connectorCustomizationPoints"
                        ng-click="$ctrl.editConnectorCustomizationPoint(connector, customizationPoint, $event)">
                    {{customizationPoint}}
                  </div>
                </div>
              </div>
              <div id="{{$ctrl.direction}}-flow-block" class="flow-block">
                <span>{{$ctrl.flowName}}</span>
                <div id="{{$ctrl.direction}}-flow_{{customizationPoint}}" class="customization-point-block"
                      ng-repeat="customizationPoint in $ctrl.flowCustomizationPoints"
                      ng-click="$ctrl.editFlowCustomizationPoint(customizationPoint, $event)">
                  {{customizationPoint}}
                </div>
              </div>
            </div>
          </div>
        `,
        controller: entaxyConnectorsWithFlowController
    })
    .name;

    function entaxyConnectorsWithFlowController(workspace, operationsService, entaxyService) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            if (ctrl.direction) {
                let isDirectionIn = ctrl.direction === 'in';
                ctrl.connectorsGroupTitle = '--- ' + ctrl.direction.toUpperCase() + ' ---';
                ctrl.flowName = ctrl.direction.toUpperCase() + '-FLOW';
                ctrl.connectorCustomizationPoints = isDirectionIn ? ['pre-route', 'response'] : ['pre-output', 'postprocess'];
                ctrl.flowCustomizationPoints = isDirectionIn ? ['pre-route', 'response'] : ['pre-route', 'postprocess'];
            } else {
                Entaxy.log.error('Connectors group direction is not defined');
            }
        }

        ctrl.editConnectorProperties = function (connector, event) {
            event.stopPropagation();

            let mbeanName = workspace.getSelectedMBeanName();
            let itemType = 'Connector';
            let mode = Entaxy.MODAL_MODES.EDIT;

            entaxyService.openEditItemModalAndProcessResults(mbeanName, itemType, connector.name, mode);
        }

        ctrl.editConnectorCustomizationPoint = function (connector, customizationPoint, event) {
            event.stopPropagation();

            let mbeanName = workspace.getSelectedMBeanName();
            let propertyName = getPropertyNameForConnectorCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(mbeanName, 'getConnectorConfig', [ connector.name ], propertyName);
        }

        ctrl.editFlowCustomizationPoint = function (customizationPoint, event) {
            event.stopPropagation();

            let selectedMbean = workspace.getSelectedMBean();
            let mbeanName = selectedMbean.parent.objectName;
            let propertyName = getPropertyNameForFlowCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(mbeanName, 'getProfileConfig', [ ctrl.profileName ], propertyName);
        }

        function getPropertyNameForConnectorCustomizationPoint(customizationPointName) {
            switch (customizationPointName) {
                case 'pre-route':
                case 'pre-output':
                    return customizationPointName;
                case 'response':
                case 'postprocess':
                    return 'responseRoute';
            };
        }

        function getPropertyNameForFlowCustomizationPoint(customizationPointName) {
            switch (customizationPointName) {
                case 'pre-route':
                    return ctrl.direction + '-flow-' + customizationPointName;
                case 'response':
                case 'postprocess':
                    return ctrl.direction + '-flow-response';
            };
        }
    }
    entaxyConnectorsWithFlowController.$inject = ['workspace', 'operationsService', 'entaxyService'];

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