/*-
 * ~~~~~~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 com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

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

public class FieldElement implements EntaxyFactory.FieldInfo {

	JsonObject jsonOrigin = null;
	
	String name;
	String type;
	String displayName;
	String description = "";
	
	boolean immutable = false;
	boolean required = false;
	Object defaultValue = "";

	boolean conditional = false;
	String condition = null;
	
	boolean isRef = false;
	
	public static FieldElement merge(FieldElement...elements) {
		
		JsonObject result = new JsonObject();
		
		for (int i=0; i<elements.length; i++) {
			JsonElement jsonElement = elements[i].jsonOrigin;
			if (jsonElement == null)
				continue;
			if (!jsonElement.isJsonObject())
				continue;
			JsonObject jsonObject = jsonElement.getAsJsonObject();
			JSONUtils.mergeObjects(jsonObject, result);
		}
		
		FieldElement fe = (new Gson()).fromJson(result, FieldElement.class);
		fe.setJsonOrigin(result.deepCopy());
		return fe;
	}
	
	/* EntaxyFactory.FieldInfo implementation */
	
	@Override
	public String getName() {
		return name;
	}

	@Override
	public String getDisplayName() {
		return displayName;
	}

	@Override
	public String getDescription() {
		return description;
	}

	@Override
	public String getType() {
		return type;
	}

	@Override
	public Object getDefaultValue() {
		return defaultValue;
	}
	
	@Override
	public boolean isRequired() {
		return required;
	}
	
	@Override
	public boolean isImmutable() {
		return immutable;
	}
	
	@Override
	public JsonObject getJsonOrigin() {
		return jsonOrigin;
	}
	
	@Override
	public boolean isConditional() {
		return conditional;
	}

	@Override
	public String getCondition() {
		return condition;
	}

	@Override
	public boolean isRef() {
		return isRef;
	}
	
	/* other */
	
	public void setName(String name) {
		this.name = name;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}

	public void setType(String type) {
		this.type = type;
	}

	public void setDefaultValue(Object defaultValue) {
		this.defaultValue = defaultValue;
	}

	public void setRequired(boolean isRequired) {
		this.required = isRequired;
	}

	public void setImmutable(boolean isImmuable) {
		this.immutable = isImmuable;
	}
	public void setJsonOrigin(JsonObject jsonOrigin) {
		this.jsonOrigin = jsonOrigin;
	}

	public void setConditional(boolean conditional) {
		this.conditional = conditional;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public void setRef(boolean isRef) {
		this.isRef = isRef;
	}
	
}
