/*-
 * ~~~~~~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 org.osgi.service.component.annotations.Component;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

import ru.entaxy.platform.base.objects.EntaxyObject;
import ru.entaxy.platform.base.objects.EntaxyObject.FIELDS;
import ru.entaxy.platform.core.producer.executor.generationmodel.DefaultObjectDataResolver;
import ru.entaxy.platform.core.producer.executor.generationmodel.ObjectDataResolver;

@Component(service = ObjectDataResolver.class, immediate = true)
public class ConfigObjectDataResolver extends DefaultObjectDataResolver {

	@Override
	public String getSupportedObjectType() {
		return ObjectConfig.CONFIG_OBJECT_TYPE;
	}
	
	@Override
	protected Object resolveNotRoot(EntaxyObject sourceObject, String propertyName, JsonObject jsonRoot) {
		/* System.out.println("\n\n\tRESOLVE - ConfigObjectDataResolver :: " + propertyName
				+ "\n" + jsonRoot); */
		if (jsonRoot.has(FIELDS.PROPERTIES)) {
			JsonElement propertiesElement = jsonRoot.get(FIELDS.PROPERTIES);
			if (propertiesElement.isJsonObject()) {
				JsonObject properties = propertiesElement.getAsJsonObject();
				/* System.out.println("\n\n\tCONFIGURED DATA:"
						+ "\nconfigured: " + properties.get(ObjectConfig.CONFIG_ID_FIELD_NAME)
						+ "\nconfiguredType: " + properties.get(ObjectConfig.CONFIG_TYPE_FIELD_NAME)
						+ "\nconfiguredProperties: " + properties.get(ObjectConfig.CONFIG_PROPERTIES_FIELD_NAME)
						); */
				if (properties.has(ObjectConfig.CONFIG_ID_FIELD_NAME) 
						&& properties.has(ObjectConfig.CONFIG_TYPE_FIELD_NAME) 
						&& properties.has(ObjectConfig.CONFIG_PROPERTIES_FIELD_NAME)) {
					
					String targetId = properties.get(ObjectConfig.CONFIG_ID_FIELD_NAME).getAsString();
					String targetType = properties.get(ObjectConfig.CONFIG_TYPE_FIELD_NAME).getAsString();
					JsonElement configuredPropertiesElement = properties.get(ObjectConfig.CONFIG_PROPERTIES_FIELD_NAME);
					if (configuredPropertiesElement.isJsonObject()) {
						JsonObject configuredProperties = configuredPropertiesElement.getAsJsonObject();
						String configPrefix = ObjectConfig.getConfigurationPrefix(targetId, targetType);
						
						if (configuredProperties.has(propertyName))
							return configPrefix + propertyName + "}";
						
					}
					
				}
				
			}
			
		}
		return super.resolveNotRoot(sourceObject, propertyName, jsonRoot);
	}
}
