/*-
 * ~~~~~~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 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.objects.factory.EntaxyFactory.CONFIGURATION.FIELDS;
import ru.entaxy.platform.base.support.CommonUtils;
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 fromJson(String name, JsonElement element) {
		Class<? extends FieldElement> feClass = FieldElement.class;
		
		if (element.isJsonObject())
			if (element.getAsJsonObject().has(FIELDS.ATTRIBUTES.IS_REF)) {
				JsonElement val = element.getAsJsonObject().get(FIELDS.ATTRIBUTES.IS_REF);
				if (val.isJsonPrimitive() && val.getAsJsonPrimitive().isBoolean())
					if (val.getAsJsonPrimitive().getAsBoolean()) {
						feClass = RefFieldElement.class;
					}
			}
		
		FieldElement fe = (new Gson()).fromJson(element, feClass);
		fe.setName(name);
		fe.setJsonOrigin(element.isJsonObject()?element.getAsJsonObject():new JsonObject());
	
		return fe;
	}
	
	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 = FieldElement.fromJson("merged", result);
		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 CommonUtils.isValid(this.condition);
	}

	@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;
	}
	
}
