/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * 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~~~~~~
 */
package ru.entaxy.platform.base.objects.factory.tracker;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.List;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.base.generator.template.TemplateService;
import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
import ru.entaxy.platform.base.objects.factory.impl.DefaultFactory;
import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.base.support.osgi.tracker.BundleTrackerCustomizerListener;

public class TrackedFactoryCustomizerListener implements BundleTrackerCustomizerListener<List<TrackedFactory>> {
	
	private static final Logger log = LoggerFactory.getLogger(TrackedFactoryCustomizerListener.class);
	
	protected BundleContext bundleContext;
	
	public TrackedFactoryCustomizerListener(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}

	@Override
	public void added(List<TrackedFactory> managedObject) {
		if (managedObject == null) {
			log.debug("managedObject is null");
			return;
		}
		for (TrackedFactory tf: managedObject) {
			log.debug("Added factory: " + tf.getId());
			if (TrackedFactory.trackedFactoriesMap.containsKey(tf.getId())) {
				TrackedFactory.trackedFactoriesMap.get(tf.getId()).unregister();
			}
			DefaultFactory defaultFactory = new DefaultFactory();
			defaultFactory.setFactoryId(tf.getId());
			defaultFactory.configure(tf.getConfigString());
			if (defaultFactory.isValid()) {

				tf.setId(defaultFactory.getFactoryId());
				
				Dictionary<String, String> props = new Hashtable<String, String>();
				props.put(EntaxyFactory.SERVICE.PROP_ID, defaultFactory.getFactoryId());
				props.put(EntaxyFactory.SERVICE.PROP_TYPE, defaultFactory.getFactoryType());
				props.put(EntaxyFactory.SERVICE.PROP_ORIGIN_BUNDLE, tf.getBundle().getBundleId()+"");				
				
				tf.setServiceRegistration(
						this.bundleContext.registerService(EntaxyFactory.class, defaultFactory, props)
				);
				
				TrackedFactory.trackedFactoriesMap.put(tf.getId(), tf);
			}
		}
	}

	@Override
	public void modified(List<TrackedFactory> managedObject) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void removed(List<TrackedFactory> managedObject) {
		if (managedObject == null)
			return;
		for (TrackedFactory tf: managedObject) {
			try {
				tf.getServiceRegistration().unregister();
			} catch (Exception e) {
				// do nothing
			}
			TrackedFactory.trackedFactoriesMap.remove(tf.getId());
		}
		
	}

	
	
}
