/*-
 * ~~~~~~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.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.util.tracker.BundleTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.platform.base.objects.EntaxyObject;
import ru.entaxy.platform.base.objects.EntaxyObjectService;
import ru.entaxy.platform.base.objects.service.tracker.TrackedEntaxyObjectCustomizer;
import ru.entaxy.platform.base.objects.service.tracker.TrackedEntaxyObjectCustomizerListener;
import ru.entaxy.platform.base.support.osgi.tracker.BundleTrackerUtils;
import ru.entaxy.platform.base.support.osgi.tracker.filter.BundleHeaderFilter;

@Component(service = {EntaxyObjectService.class}, immediate = true)
public class EntaxyObjectServiceImpl implements EntaxyObjectService {

	private static final Logger log = LoggerFactory.getLogger(EntaxyObjectServiceImpl.class);
	
	protected BundleContext bundleContext;
	
	protected BundleTracker<List<TrackedEntaxyObject>> objectTracker;
	
	protected Map<String, TrackedEntaxyObject> trackedObjects = new HashMap<>();
	
	@Activate
	public void activate(ComponentContext componentContext) {
		this.bundleContext = componentContext.getBundleContext();
		openTracker();
	}
	
	@Deactivate
	public void deactivate(ComponentContext componentContext) {
		this.objectTracker.close();
	}
	
	
	@Override
	public List<EntaxyObject> getObjects() {
		return trackedObjects.values().stream().collect(Collectors.toList());
	}

	protected void openTracker() {
		objectTracker = BundleTrackerUtils.<List<TrackedEntaxyObject>>createBuilder()
				.addFilter(
					(new BundleHeaderFilter()).header(EntaxyObject.HEADERS.GENERATED_OBJECTS)	
				)
				.customizer(
					(new TrackedEntaxyObjectCustomizer(this))
					.listener(new TrackedEntaxyObjectCustomizerListener(this))
				)
				.bundleState(Bundle.ACTIVE | Bundle.INSTALLED | Bundle.RESOLVED)
				.get();
		objectTracker.open();		
	}

	public BundleContext getBundleContext() {
		return bundleContext;
	}
	
	public void addObjects(List<TrackedEntaxyObject> objects) {
		for (TrackedEntaxyObject object: objects)
			this.trackedObjects.put(object.getObjectId() + ":" + object.getObjectType(), object);
	}

	public void removeObjects(List<TrackedEntaxyObject> objects) {
		for (TrackedEntaxyObject object: objects)
			this.trackedObjects.remove(object.getObjectId() + ":" + object.getObjectType());
	}
	
}
