/*-
 * ~~~~~~licensing~~~~~~
 * adapters-core
 * ==========
 * Copyright (C) 2020 - 2021 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~~~~~~
 */
package ru.entaxy.esb.platform.runtime.base.connecting.adapter.core.management;

import java.util.HashMap;
import java.util.Hashtable;
import java.util.Map;

import javax.management.DynamicMBean;
import javax.management.MBeanRegistration;
import javax.management.NotCompliantMBeanException;
import javax.management.StandardMBean;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.CollectionType;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.osgi.service.component.annotations.ReferenceCardinality;
import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.component.annotations.ServiceScope;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.esb.platform.base.management.core.ManagementCore;
import ru.entaxy.esb.platform.runtime.base.connecting.adapter.core.api.Adapter;

@Component(
		service = {AdaptersMBean.class, DynamicMBean.class, MBeanRegistration.class},
		// property = {"jmx.objectname=ru.entaxy.esb:group=platform,category=adapters"},
		property = {ManagementCore.ENTAXY_JMX_DOMAIN + "=" + AdaptersMBean.Q_ADAPTERS_S},
		scope = ServiceScope.SINGLETON,
		immediate = true
		)
public class AdaptersMBeanImpl extends StandardMBean implements AdaptersMBean {

	private static final Logger log = LoggerFactory.getLogger(AdaptersMBeanImpl.class);
	
	protected Map<Adapter, ServiceRegistration<AdapterMBean>> mbeans = new HashMap<>();
	
	// @Reference (service = BundleContext.class)
	protected BundleContext bundleContext;
	
	public AdaptersMBeanImpl() throws NotCompliantMBeanException {
		super(AdaptersMBean.class);
	}
	
	
	@Activate
	public void activate(ComponentContext componentContext) {
		log.debug(" >> ACTIVATE <<");
		this.bundleContext = componentContext.getBundleContext();
	}
	
	
	@Reference (service = Adapter.class, cardinality = ReferenceCardinality.MULTIPLE, 
			unbind = "unbindAdapter", collectionType = CollectionType.SERVICE, 
			policy = ReferencePolicy.DYNAMIC)
	public void bindAdapter(Adapter adapter) {
		
		try {
			AdapterMBeanImpl mbean = new AdapterMBeanImpl(adapter);
			Hashtable props = new Hashtable<>();
			// props.put("jmx.objectname", "ru.entaxy.esb:group=platform,category=adapters,id=" + adapter.getId());
			props.put(ManagementCore.JMX_OBJECTNAME, Q_ADAPTERS.qualifier("id", adapter.getId()).getValue());
			ServiceRegistration<AdapterMBean> reg = bundleContext.registerService(
					new String[] {
							AdapterMBean.class.getName(),
							DynamicMBean.class.getName(),
							MBeanRegistration.class.getName()
						}
					, mbean
					, props);
			this.mbeans.put(adapter, reg);
			
		} catch (NotCompliantMBeanException e) {
			log.error("Error creating MBean for adapter: ", e);
		}
		
	}
	
	public void unbindAdapter(Adapter adapter) {
		ServiceRegistration<AdapterMBean> reg = this.mbeans.get(adapter);
		if (reg != null)
			reg.unregister();
		this.mbeans.remove(adapter);
	}
	
	@Override
	public void execute(String value) {
		log.info(">> EXECUTED");
	}
	
}
