/*-
 * ~~~~~~licensing~~~~~~
 * generator-api
 * ==========
 * 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.base.generator.template.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.base.generator.template.Template;
import ru.entaxy.base.generator.template.TemplateService;

@Component(service = TemplateService.class, immediate = true)
public class TemplateServiceImpl implements TemplateService {

	private static final Logger log = LoggerFactory.getLogger(TemplateServiceImpl.class);
	
	private BundleContext bundleContext = null;
	
	protected final Map<String, Template> templates = new HashMap<>();
	
	protected List<ServiceReference<Template>> delayed = new ArrayList<>();
	
	@Reference(cardinality = ReferenceCardinality.MULTIPLE, collectionType = CollectionType.REFERENCE
	, policy = ReferencePolicy.DYNAMIC, unbind = "removeTemplate")
	public void addTemplate(ServiceReference<Template> templateService) {
		log.info("Added template service with id [" + templateService.getProperty(PROP_ID) + "]");
		if (this.bundleContext == null)
			this.delayed.add(templateService);
		else
			processTemplate(templateService);
	}
	
	public void removeTemplate(ServiceReference<Template> templateService) {
		String id = templateService.getProperty(PROP_ID).toString();
		this.bundleContext.ungetService(templateService);
		this.templates.remove(id);
	}

	@Activate
	public void activate(BundleContext context) {
		this.bundleContext = context;
		processDelayed();
	}
	
	protected void processDelayed() {
		for (ServiceReference<Template> ref: delayed)
			processTemplate(ref);
		delayed.clear();
	}
	
	protected void processTemplate(ServiceReference<Template> reference) {
		Template t = this.bundleContext.getService(reference);
		this.templates.put(t.getId(), t);
	}
	
	@Override
	public List<Template> getAllTemplates() {
		return new ArrayList<>(this.templates.values());
	}
	
	@Override
	public Template getTemplateById(String id) {
		return this.templates.get(id);
	}
}
