/*-
 * ~~~~~~licensing~~~~~~
 * test-producers
 * ==========
 * 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.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);
	}
	
}
