/*-
 * ~~~~~~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.core.artifact.capabilities;

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

import ru.entaxy.platform.base.support.CommonUtils;
import ru.entaxy.platform.core.artifact.capabilities.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;
	}
}
