/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2024 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: '<',
            editProfilePropertiesFn: '<',
            editPropertiesFn: '<',
            saveConnectorFn: '<',
            updateFn: '<'
        },
        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-class="{'has-changes': connector.isChanged}"
                        ng-click="$ctrl.editConnectorProperties(connector, $event)">
                  <div class="connector-info">
                    <div class="connector-title-container">
                      <span class="profile-diagram-support-span" ng-if="connector.isChanged"></span>
                      <span class="bold connector-title" data-toggle="tooltip" title="{{connector.displayName}}">
                        {{connector.displayName}}
                      </span>
                      <span class="pficon pficon-save" ng-click="$ctrl.saveConnector(connector, $event)"
                            data-toggle="tooltip" title="Save connector changes" ng-if="connector.isChanged"></span>
                    </div>
                    <span class="connector-classifier">{{connector.classifier}}</span>
                  </div>
                  <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"
                    ng-click="$ctrl.editProfileProperties($ctrl.direction + '-flow', $event)">
                <span>{{$ctrl.flowName}}</span>
                <div class="out-flow-customization-points-container" ng-if="$ctrl.isDirectionOut">
                  <div id="{{$ctrl.direction}}-flow_connector-selector"
                        class="customization-point-block connector-selector"
                        ng-click="$ctrl.editFlowCustomizationPoint('connector-selector', $event)"
                        data-toggle="tooltip" title="connector-selector">
                    <span class="connector-selector-icon"></span>
                  </div>
                  <div id="{{$ctrl.direction}}-flow_pre-route"
                        class="customization-point-block pre-route"
                        ng-click="$ctrl.editFlowCustomizationPoint('pre-route', $event)">
                    pre-route
                  </div>
                </div>
                <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) {
        'ngInject';

        let ctrl = this;

        ctrl.$onInit = function() {
            if (ctrl.direction) {
                let isDirectionIn = ctrl.direction === 'in';
                ctrl.isDirectionOut = !isDirectionIn;
                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'] : ['postprocess'];
            } else {
                Entaxy.log.error('Connectors group direction is not defined');
            }
        }

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

            ctrl.editPropertiesFn(connector, Entaxy.RUNTIME_TYPE.CONNECTOR, 'Connector');
        }

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

            let propertyName = getPropertyNameForConnectorCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(connector.mbeanName, propertyName, connector);
        }

        ctrl.editProfileProperties = function (groupName, event) {
            event.stopPropagation();

            ctrl.editProfilePropertiesFn(groupName);
        }

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

            let selectedMbeanName = workspace.getSelectedMBeanName();
            let propertyName = getPropertyNameForFlowCustomizationPoint(customizationPoint);

            ctrl.editCustomizationPointFn(selectedMbeanName, 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';
                case 'connector-selector':
                    return customizationPointName;
            };
        }

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

            ctrl.saveConnectorFn(connector.mbeanName, true, false);
        }
    }
    entaxyConnectorsWithFlowController.$inject = ['workspace'];

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