/*-
 * ~~~~~~licensing~~~~~~
 * artifact-management
 * ==========
 * Copyright (C) 2020 - 2021 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.impl;

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

import org.osgi.resource.Resource;

import ru.entaxy.platform.core.artifact.capabilities.CapabilityDescriptor;

@Deprecated(forRemoval = true, since = "1.8.2")
public class CapabilityDescriptorImpl implements CapabilityDescriptor {

	protected String namespace;
	
	protected Map<String, String> directives = new HashMap<>();
	protected Map<String, AttributeDescriptor> attributes = new HashMap<>(); 
	
	private static class AttributeDescriptor {
		String type;
		String value;
		Object typedValue;
		
		public AttributeDescriptor(String value) {
			this(value, "String");
		}
		
		public AttributeDescriptor(String value, String type) {
			this.type = type;
			this.value = value;
			
			// TODO convert types if needed
			this.typedValue = value;
		}
	}		
			
	public CapabilityDescriptorImpl() {
		// TODO Auto-generated constructor stub
	}

	public CapabilityDescriptorImpl(String namespace) {
		this();
		namespace(namespace);
	}
	
	public String getAttributesAsString() {
		String result = "";
		for (Map.Entry<String, AttributeDescriptor> entry: this.attributes.entrySet()) {
			if (!result.isEmpty())
				result += ";";
			result += entry.getKey();
			if (!"String".equals(entry.getValue().type))
				result += ":" + entry.getValue().type;
			result += "=" + entry.getValue().value;
		}
		return result;
	}
	
	@Override
	public String getNamespace() {
		return this.namespace;
	}

	@Override
	public Map<String, String> getDirectives() {
		return this.directives;
	}

	@Override
	public Map<String, Object> getAttributes() {
		Map<String, Object> result = new HashMap<>();
		for (Map.Entry<String, AttributeDescriptor> entry: this.attributes.entrySet())
			result.put(entry.getKey(), entry.getValue().typedValue);
		return result;
	}

	@Override
	public Resource getResource() {
		// not implemented
		return null;
	}

	@Override
	public CapabilityDescriptor namespace(String namespace) {
		this.namespace = namespace;
		return this;
	}



	@Override
	public CapabilityDescriptor attributes(Map<String, Object> attributes) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CapabilityDescriptor attribute(String name, Object value) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public CapabilityDescriptor attribute(String name, Object value, String type) {
		// TODO Auto-generated method stub
		return null;
	}

}
