/*-
 * ~~~~~~licensing~~~~~~
 * configuration-test-1
 * ==========
 * 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.config;

import java.util.Map;

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

public class DefaultPropertiesProviderWithImmutables extends DefaultPropertiesProvider implements Immutables {

	private static final Logger log = LoggerFactory.getLogger(DefaultPropertiesProviderWithImmutables.class);
	
	protected Map<String, Object> immutables;
	
	public DefaultPropertiesProviderWithImmutables(String objectId
			, String objectType
			, String configurationPid
			, BundleContext bundleContext
			, Map<String, Object> configurables) {
		super(objectId, objectType, configurationPid, bundleContext, configurables);
	}

	public DefaultPropertiesProviderWithImmutables(String configurationPid
			, BundleContext bundleContext
			, Map<String, Object> configurables) {
		super(configurationPid, bundleContext, configurables);
	}

	public DefaultPropertiesProviderWithImmutables(String configurationPid
			, BundleContext bundleContext
			, Map<String, Object> configurables
			, Map<String, Object> immutables) {
		super(configurationPid, bundleContext, configurables);
		this.immutables = immutables;
		registerImmutables();
	}

	public DefaultPropertiesProviderWithImmutables(String objectId
			, String objectType
			, String configurationPid
			, BundleContext bundleContext
			, Map<String, Object> configurables
			, Map<String, Object> immutables) {
		super(objectId, objectType, configurationPid, bundleContext, configurables);
		this.immutables = immutables;
		registerImmutables();
	}
	
	protected void registerImmutables() {
		if ((this.configurationPid != null) && (this.bundleContext != null)) {
			
			ServiceReference<ImmutablesCollector> collectorRef = this.bundleContext.getServiceReference(ImmutablesCollector.class);
			if (collectorRef != null) {
				ImmutablesCollector collector = this.bundleContext.getService(collectorRef);
				if (collector != null)
					collector.add(this);
				else
					log.error("registerImmutables :: COLLECTOR IS NULL");
				this.bundleContext.ungetService(collectorRef);
			} else {
				log.error("registerImmutables :: REF IS NULL");
			}
		}
		
	}

	@Override
	public String getPid() {
		return this.configurationPid;
	}

	@Override
	public Map<String, Object> getProperties() {
		return this.immutables;
	};

}
