/*-
 * ~~~~~~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.support.osgi.bundle;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.base.support.osgi.bundle.CapabilityDescriptor.ATTRIBUTE_TYPES;

public class CapabilityTypeHelper {

	public static String getTypeName(Object object) {
		
		if (object == null)
			return ATTRIBUTE_TYPES.STRING;
		
		if (object instanceof List<?>) {
			return ATTRIBUTE_TYPES.TYPED_LIST(getListTypeName((List<?>)object));
		}
		return getScalarTypeName(object);
		
	}
	
	public static String getListTypeName(List<?> list) {
		if (list.size() == 0)
			return ATTRIBUTE_TYPES.STRING;
		else
			return getScalarTypeName(list.get(0));
	}
	
	public static String getScalarTypeName(Object object) {
		if ((object instanceof Integer) || (object instanceof Long))
			return ATTRIBUTE_TYPES.LONG;
		if (object instanceof Number)
			return ATTRIBUTE_TYPES.DOUBLE;
		return ATTRIBUTE_TYPES.STRING;
	}
	
	public static String getListAsString(List<?> list) {

		String result = "\""; 
		if (list != null)
			result += list.stream().map(item->(item==null)?"":item.toString())
				.collect(Collectors.joining(","));
			
		return result + "\"";
	}
	
	public static Object getTypedValue(String type, String value) {
		if (CapabilityDescriptor.ATTRIBUTE_TYPES.STRING.equals(type))
			return value;
		if (CapabilityDescriptor.ATTRIBUTE_TYPES.VERSION.equals(type))
			return value;
		if (CapabilityDescriptor.ATTRIBUTE_TYPES.LONG.equals(type))
			try {
				return Long.parseLong(value);
			} catch (Exception e) {
				return Long.valueOf(-1);
			}
		if (CapabilityDescriptor.ATTRIBUTE_TYPES.DOUBLE.equals(type))
			try {
				return Double.parseDouble(value);
			} catch (Exception e) {
				return Double.valueOf(-1);
			}
		if (CapabilityDescriptor.ATTRIBUTE_TYPES.isList(type)) {
			String itemType = CapabilityDescriptor.ATTRIBUTE_TYPES.itemType(type);
			if (!CommonUtils.isValid(itemType))
				return value;
			List<Object> result = new ArrayList<>();
			String val = value;
			if (val.startsWith("\""))
				val = val.substring(1);
			if (val.endsWith("\""))
				val = val.substring(0, val.length()-1);
			if (!CommonUtils.isValid(val))
				return result;
			
			String[] vals = val.split(",");
			for (int i=0; i< vals.length; i++)
				if (vals[i] == null)
					continue;
				else
					result.add(getTypedValue(itemType, vals[i]));
			return result;
		}
		
		return value;
	}
}
