/*-
 * ~~~~~~licensing~~~~~~
 * entaxy-management-plugin
 * ==========
 * Copyright (C) 2020 - 2022 EmDev LLC
 * ==========
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ~~~~~~/licensing~~~~~~
 */

var Entaxy;
(function (Entaxy) {
    Entaxy._module.factory('entaxyConnectorsService', ['jolokia', 'jolokiaService', '$q',
        function(jolokia, jolokiaService, $q) {
            return {
                getConnectors: function (profileName) {
                    return getConnectors(profileName);
                }
            };

//            let mbeanName = ctrl.workspace.getSelectedMBeanName();
//
//            let parsedMbean = Core.parseMBean(mbeanName);
//            let profileName = parsedMbean.attributes.profile;
//
//            entaxyConnectorsService.getConnectors(profileName).then(connectors => {
//                ctrl.connectors = connectors;
//                ctrl.viewedConnectors = connectors;
//                ctrl.toolbarConfig.filterConfig.resultsCount = connectors.length;
//            });

            function getConnectors(profileName) {
                var entaxyJmxDomain = localStorage['entaxyJmxDomain'] || "ru.entaxy.esb";

                return $q((resolve, reject) => {
                    jolokia.list(entaxyJmxDomain,
                        Core.onSuccess((response) => {
                            let connectors = [];
                            let promises = [];
                            let entaxyDomain = response;

                            angular.forEach(entaxyDomain, function (mbean, mbeanNameWithoutDomain) {
                                let mbeanName = entaxyJmxDomain + ':' + mbeanNameWithoutDomain;
                                let parsedMbean = Core.parseMBean(mbeanName);

                                if (parsedMbean.attributes.connector && parsedMbean.attributes.profile === profileName) {

                                    let connectorName = parsedMbean.attributes.connector;

                                    let promise = jolokiaService.getAttribute(mbeanName, 'BundleState');
                                    promise.then((response) =>
                                        connectors.push({name: connectorName, status: response, mbeanName: mbeanName}));
                                    promises.push(promise);
                                }
                            });

                            $q.all(promises).then(() => resolve(connectors));
                        },
                        {
                            error: (response) => Entaxy.log.info('ERROR ' + JSON.stringify(response))
                        })
                    );
                });
            }
        }
    ])
})(Entaxy || (Entaxy = {}));
