/*-
 * ~~~~~~licensing~~~~~~
 * object-producing-resources-support
 * ==========
 * 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.core.producer.resources;

import java.util.HashMap;
import java.util.Map;

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

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.base.support.JSONUtils;

public class ResourceDescriptor {

	static public final String RESOURCE_TYPE_PREFIX = "resource:";
	static public final String PROP_METADATA = "##meta";

	static public Gson GSON = (new GsonBuilder()).create(); 
	
	public static ResourceDescriptor fromJson(JsonElement element) {
		if (element == null)
			return null;
		if (!element.isJsonObject())
			return null;
		return new ResourceDescriptor(element.getAsJsonObject());
	}

	public static ResourceDescriptor fromMap(Map<String, Object> properties) {
		return new ResourceDescriptor(properties);
	}
	
	
	protected JsonObject origin;
	
	public String provider;
	public String location;
	public String endType;
	public String format;
	
	public String url;
	
	public Map<String, Object> allProperties;
	
	public ResourceDescriptor(JsonObject jsonObject) {
		this(JSONUtils.element2map(jsonObject));
		this.origin = jsonObject;
		// loadData();
	}

	public ResourceDescriptor(Map<String, Object> properties) {
		this.origin = new JsonObject();
		loadDataFromMap(properties);
		this.allProperties = new HashMap<>(properties);
	}
	
	protected void loadData() {
		if (this.origin.has(ResourceDataProcessor.PROP_URL))
			this.url = this.origin.get(ResourceDataProcessor.PROP_URL).getAsString();
		if (this.origin.has(ResourceDataProcessor.PROP_PROVIDER))
			this.provider = this.origin.get(ResourceDataProcessor.PROP_PROVIDER).getAsString();
		if (this.origin.has(ResourceDataProcessor.PROP_LOCATION))
			if (this.origin.get(ResourceDataProcessor.PROP_LOCATION).isJsonPrimitive())
				this.location = this.origin.get(ResourceDataProcessor.PROP_LOCATION).getAsString();
		if (this.origin.has(ResourceDataProcessor.PROP_END_TYPE))
			this.endType = this.origin.get(ResourceDataProcessor.PROP_END_TYPE).getAsString();
		if (this.origin.has(ResourceDataProcessor.PROP_FORMAT))
			this.format = this.origin.get(ResourceDataProcessor.PROP_FORMAT).getAsString();

		checkUrl();
		
	}
	
	protected void loadDataFromMap(Map<String, Object> properties) {
		this.url = properties.getOrDefault(ResourceDataProcessor.PROP_URL, "").toString();
		this.provider = properties.getOrDefault(ResourceDataProcessor.PROP_PROVIDER, "default").toString();
		this.location = properties.getOrDefault(ResourceDataProcessor.PROP_LOCATION, "").toString();
		this.endType = properties.getOrDefault(ResourceDataProcessor.PROP_END_TYPE, "").toString();
		this.format = properties.getOrDefault(ResourceDataProcessor.PROP_FORMAT, "").toString();
		
		checkUrl();
	};

	protected void checkUrl() {
		if (CommonUtils.isValid(this.url)) {
			int index = this.url.indexOf(':'); 
			if (index > 0) {
				this.provider = this.url.substring(0, index);
				this.location = this.url.substring(index + 1);
			}
		}
	}
	
	public String getResourceUrl() {
		return (CommonUtils.isValid(provider)?provider+":":"") + location;
	}
	
	public boolean isReturnResource() {
		return !CommonUtils.isValid(endType) || "resource".equalsIgnoreCase(endType);
	}
	
	public void returnResource() {
		this.endType = "resource";
	}
	
	public Map<String, Object> getMetadata(){
		if (!this.allProperties.containsKey(PROP_METADATA))
			return new HashMap<>();
		Object obj = this.allProperties.get(PROP_METADATA);
		if (obj instanceof Map) 
			return (Map<String, Object>)obj;
		return new HashMap<>();
	}

	public JsonObject getMetadataAsJson() {
		return GSON.toJsonTree(getMetadata()).getAsJsonObject();
	}
	
}
