/*-
 * ~~~~~~licensing~~~~~~
 * object-producing-config-support
 * ==========
 * 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.core.producer.config;

import java.util.Map;

import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ru.entaxy.esb.platform.runtime.base.connecting.generator.Generated;
import ru.entaxy.platform.base.objects.EntaxyObject;
import ru.entaxy.platform.base.objects.factory.EntaxyFactoryException;
import ru.entaxy.platform.core.producer.api.EntaxyWrappedFactory;
import ru.entaxy.platform.core.producer.wrapper.AbstractFactoryWrapper.GenerationProcessor;

@Component (service = GenerationProcessor.class
	, property = {
			"generation.plugin.id=config-support"
			, "generation.factory.type=entaxy.runtime.connection"
			, "generation.factory.type=entaxy.runtime.profile"
		}
	, immediate = true)
public class ConfigProcessor implements GenerationProcessor {

	private static final Logger log = LoggerFactory.getLogger(ConfigProcessor.class);
	
	@Override
	public void preGenerate(EntaxyWrappedFactory wrappedFactory, String outputType, String scope, String objectType,
			Map<String, Object> parameters) throws EntaxyFactoryException {
		
		if (!parameters.containsKey(EntaxyObject.FIELDS.PROPERTIES))
			return;

		@SuppressWarnings("unchecked")
		Map<String, Object> properties = (Map<String, Object>)parameters.get(EntaxyObject.FIELDS.PROPERTIES);

		if (!properties.containsKey(ObjectConfig.CONFIG_FIELD_NAME))
			return;
		
		ObjectConfig.backupProperties(parameters);
		
		String objectId = parameters.get(EntaxyObject.FIELDS.OBJECT_ID).toString();
		
		for (Map.Entry<String, Object> entry: properties.entrySet()) {
			
			if (entry.getKey().startsWith("#"))
				continue;
			
			if (!ObjectConfig.isConfigurable(wrappedFactory, outputType, entry.getKey()))
				continue;
			
			properties.put(entry.getKey(), ObjectConfig.getConfigurationPrefix(objectId, objectType) + entry.getKey() + "}");
			
		}
		
		ObjectConfig.removeConfigData(parameters);
		
		log.debug("preGenerate for {}:{}", wrappedFactory.getFactoryId(), wrappedFactory.getFactoryType());
	}
	
	@Override
	public void postGenerate(EntaxyWrappedFactory wrappedFactory, String outputType, String scope, String objectType,
			Map<String, Object> parameters, Generated generated) throws EntaxyFactoryException {
		log.debug("postGenerate for {}:{}", wrappedFactory.getFactoryId(), wrappedFactory.getFactoryType());
		// restore initial properties
		ObjectConfig.restoreBackupProperties(parameters);
	}
	
}
