package org.alien4cloud.tosca.editor.processors.substitution;

import java.util.Map;

import org.alien4cloud.tosca.editor.EditionContextManager;
import org.alien4cloud.tosca.editor.operations.substitution.AddCapabilitySubstitutionTypeOperation;
import org.alien4cloud.tosca.editor.processors.IEditorOperationProcessor;
import org.springframework.stereotype.Component;

import com.google.common.collect.Maps;

import alien4cloud.exception.AlreadyExistException;
import alien4cloud.exception.NotFoundException;
import org.alien4cloud.tosca.model.templates.NodeTemplate;
import org.alien4cloud.tosca.model.templates.SubstitutionTarget;
import org.alien4cloud.tosca.model.templates.Topology;

/**
 * Process given capability as a capability for the substitution type associated with this topology.
 */
@Component
public class AddCapabilitySubstitutionTypeProcessor implements IEditorOperationProcessor<AddCapabilitySubstitutionTypeOperation> {

    @Override
    public void process(AddCapabilitySubstitutionTypeOperation operation) {
        Topology topology = EditionContextManager.getTopology();
        if (topology.getNodeTemplates() == null || !topology.getNodeTemplates().containsKey(operation.getNodeTemplateName())) {
            throw new NotFoundException("Node " + operation.getNodeTemplateName() + " do not exist");
        }

        NodeTemplate nodeTemplate = topology.getNodeTemplates().get(operation.getNodeTemplateName());
        if (nodeTemplate.getCapabilities() == null || !nodeTemplate.getCapabilities().containsKey(operation.getCapabilityId())) {
            throw new NotFoundException("Capability " + operation.getCapabilityId() + " do not exist for node " + operation.getNodeTemplateName());
        }

        if (topology.getSubstitutionMapping() == null || topology.getSubstitutionMapping().getSubstitutionType() == null) {
            throw new NotFoundException("No substitution type has been found");
        }

        Map<String, SubstitutionTarget> substitutionCapabilities = topology.getSubstitutionMapping().getCapabilities();
        if (substitutionCapabilities == null) {
            substitutionCapabilities = Maps.newHashMap();
            topology.getSubstitutionMapping().setCapabilities(substitutionCapabilities);
        } else if (substitutionCapabilities.containsKey(operation.getSubstitutionCapabilityId())) {
            // ensure name unicity
            throw new AlreadyExistException(String.format("The substitution capability <%s> already exists", operation.getSubstitutionCapabilityId()));
        }
        substitutionCapabilities.put(operation.getSubstitutionCapabilityId(), new SubstitutionTarget(operation.getNodeTemplateName(), operation.getCapabilityId()));
    }
}
@@ -2449,11 +2449,9 @@
 at(%22
-The
+A
  sub
@@ -2460,16 +2460,21 @@
 itution 
+with 
 capabili
@@ -2476,16 +2476,19 @@
 ability 
+id 
 %3C%25s%3E alr

throw a better message exception