/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.platform.base.objects.factory.configuration;

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

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.reflect.TypeToken;

import ru.entaxy.platform.base.objects.factory.EntaxyFactory;
import ru.entaxy.platform.base.support.CommonUtils;

public class OutputsElement extends AbstractElement<OutputsElement> {
	
	public static final String ELEMENT_NAME = EntaxyFactory.CONFIGURATION.OUTPUTS_SECTION_NAME;
	
	public static final String DEFAULT_OUTPUT_TYPE = "init";
	
	// protected List<OutputElement> outputs = new ArrayList<>();
	
	protected Map<String, OutputElement> outputsMap = new HashMap<>();
	
	protected Map<String, FieldElement> commonFields = new HashMap<>();
	
	protected String defaultOutputType = "";
	
	@Override
	public OutputsElement fromJson(JsonElement jsonElement) {
		Gson gson = new Gson(); 
		/*
		 * this.outputs = gson.fromJson( jsonElement,
		 * TypeToken.getParameterized(ArrayList.class, OutputElement.class).getType() );
		 * processOutputs();
		 */		
		
		if (jsonElement.isJsonObject()) {
			for (Entry<String, JsonElement> entry: jsonElement.getAsJsonObject().entrySet()) {
				OutputElement oe = gson.fromJson(entry.getValue(), OutputElement.class);
				oe.setType(entry.getKey());
				oe.setCommonFields(commonFields);
				if (entry.getValue().isJsonObject())
					oe.setOrigin(entry.getValue().getAsJsonObject());
				oe.updateFromOrigin();
				if (oe.isDefault)
					this.defaultOutputType = oe.getType();
				addOutput(oe);
			}
		}

		if (!CommonUtils.isValid(defaultOutputType))
			if (outputsMap.containsKey(DEFAULT_OUTPUT_TYPE)) {
				this.defaultOutputType = DEFAULT_OUTPUT_TYPE;
				this.outputsMap.get(DEFAULT_OUTPUT_TYPE).setDefault(true);
			}
		
		return this;
	}

	protected void addOutput(OutputElement oe) {
		this.outputsMap.put(oe.getType(), oe);
	}
	
	public List<OutputElement> getOutputs() {
		return new ArrayList<>(outputsMap.values());
	}

	public String getDefaultOutputType() {
		return defaultOutputType;
	}

	public void setDefaultOutputType(String defaultOutputType) {
		this.defaultOutputType = defaultOutputType;
	}

	public boolean hasOutput(String type) {
		return this.outputsMap.containsKey(type);
	}
	
	public OutputElement getOutput(String type) {
		return this.outputsMap.get(type);
	}
	
	public void setCommonFields(Map<String, FieldElement> commonFields) {
		this.commonFields = commonFields;
		for (OutputElement oe: outputsMap.values())
			oe.setCommonFields(commonFields);
	}
	
}
