/*-
 * ~~~~~~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.factory('entaxyProfileDiagramService', ['entaxyAttributesCacheService',
        function(entaxyAttributesCacheService) {
            let changes = {};
            let isDirty = false;
            let isProfileChanged = false;
            let isDefaultRouteChanged = false;

            return {
                createChangesConfig: function (mbeanName, objectId, args, configurableOnly, connector) {
                    return createChangesConfig(mbeanName, objectId, args, configurableOnly, connector);
                },
                setChanges: function (mbeanName, changesConfig) {
                    return setChanges(mbeanName, changesConfig)
                },
                hasChanges: function (mbeanName) {
                    return hasChanges(mbeanName);
                },
                getChanges: function (mbeanName) {
                    return getChanges(mbeanName);
                },
                getArgs: function (mbeanName) {
                    return getArgs(mbeanName);
                },
                hasType: function (mbeanName, type) {
                    return hasType(mbeanName, type);
                },
                setProfileChanged: function () {
                    isProfileChanged = true;
                },
                isProfileChanged: function () {
                    return isProfileChanged;
                },
                setDefaultRouteChanged: function () {
                    isDefaultRouteChanged = true;
                },
                isDefaultRouteChanged: function () {
                    return isDefaultRouteChanged;
                },
                setConnectorChanged: function (mbeanName, isChanged) {
                    return setConnectorChanged(mbeanName, isChanged);
                },
                isConnectorChanged: function (mbeanName) {
                    return isConnectorChanged(mbeanName);
                },
                resetChanges: function (objectId) {
                    return resetChanges(objectId);
                },
                isDirty: function () {
                    return isDirty;
                },
                setDirty: function () {
                    isDirty = true;
                },
                clear: function () {
                    changes = {};
                    isDirty = false;
                    isProfileChanged = false;
                    isDefaultRouteChanged = false;
                }
            };

            function createChangesConfig(mbeanName, objectId, args, configurableOnly, connector) {
                let attributes = entaxyAttributesCacheService.getAttributes(mbeanName);

                return {
                    objectId: objectId,
                    type: attributes.RuntimeType,
                    args: args,
                    configurableOnly: configurableOnly,
                    connector: connector
                };
            }

            function setChanges(mbeanName, changesConfig) {
                changes[mbeanName] = changesConfig;
            }

            function hasChanges(mbeanName) {
                return changes[mbeanName] ? true : false;
            }

            function getChanges(mbeanName) {
                return changes[mbeanName];
            }

            function getArgs(mbeanName) {
                return hasChanges(mbeanName) ? changes[mbeanName].args : null;
            }

            function hasType(mbeanName, type) {
                return hasChanges(mbeanName) && changes[mbeanName].type === type;
            }

            function setConnectorChanged(mbeanName, isChanged) {
                if (hasChanges(mbeanName) && changes[mbeanName].connector) {
                    changes[mbeanName].connector.isChanged = isChanged;
                }
            }

            function isConnectorChanged(mbeanName) {
                return changes[mbeanName] && changes[mbeanName].connector && changes[mbeanName].connector.isChanged;
            }

            function resetChanges(objectId) {
                let hasChangedConnectors = false;
                let mbeanNameToRemove;
                Object.keys(changes).forEach(mbeanName => {
                    if (changes[mbeanName].objectId === objectId) {
                        if (changes[mbeanName].type === Entaxy.RUNTIME_TYPE.PROFILE) {
                            isProfileChanged = false;
                        } else if (changes[mbeanName].type === Entaxy.RUNTIME_TYPE.DEFAULT_ROUTE) {
                            isDefaultRouteChanged = false;
                        } else if (changes[mbeanName].type === Entaxy.RUNTIME_TYPE.CONNECTOR) {
                            setConnectorChanged(mbeanName, false);
                        }
                        mbeanNameToRemove = mbeanName;
                    } else {
                        if (changes[mbeanName].type === Entaxy.RUNTIME_TYPE.CONNECTOR &&
                                changes[mbeanName].connector && changes[mbeanName].connector.isChanged) {
                            hasChangedConnectors = true;
                        }
                    }
                });
                isDirty = hasChangedConnectors || isProfileChanged || isDefaultRouteChanged;
                delete changes[mbeanNameToRemove];
            }
        }
    ])
})(Entaxy || (Entaxy = {}));
