package alien4cloud.paas.wf;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import alien4cloud.model.components.IndexedRelationshipType;
import alien4cloud.model.topology.NodeTemplate;
import alien4cloud.model.topology.RelationshipTemplate;
import alien4cloud.paas.plan.ToscaNodeLifecycleConstants;
import alien4cloud.paas.wf.WorkflowsBuilderService.TopologyContext;
import alien4cloud.paas.wf.util.WorkflowUtils;
import alien4cloud.tosca.normative.NormativeRelationshipConstants;

@Component
@Slf4j
public class UninstallWorkflowBuilder extends StandardWorflowBuilder {

    @Override
    public void addNode(Workflow wf, String nodeId, TopologyContext toscaTypeFinder, boolean isCompute) {
        if (WorkflowUtils.isNativeOrSubstitutionNode(nodeId, toscaTypeFinder)) {
            // for a native node, we just add a subworkflow step
            WorkflowUtils.addDelegateWorkflowStep(wf, nodeId);
        } else {
            AbstractStep lastStep = null;
            // TODO: look for children ?
            lastStep = appendStateStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.STOPPING);
            lastStep = eventuallyAddStdOperationStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.STOP, toscaTypeFinder, false);
            lastStep = appendStateStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.STOPPED);
            lastStep = appendStateStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.DELETING);
            lastStep = eventuallyAddStdOperationStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.DELETE, toscaTypeFinder, false);
            lastStep = appendStateStep(wf, lastStep, nodeId, ToscaNodeLifecycleConstants.DELETED);
        }
    }

    @Override
    public void addRelationship(Workflow wf, String nodeId, NodeTemplate nodeTemplate, RelationshipTemplate relationshipTemplate,
            TopologyContext toscaTypeFinder) {
        IndexedRelationshipType indexedRelationshipType = toscaTypeFinder.findElement(IndexedRelationshipType.class, relationshipTemplate.getType());
        String targetId = relationshipTemplate.getTarget();
        boolean targetIsNative = WorkflowUtils.isNativeOrSubstitutionNode(targetId, toscaTypeFinder);
        if (targetIsNative || WorkflowUtils.isOfType(indexedRelationshipType, NormativeRelationshipConstants.HOSTED_ON)) {
            // now the node has a parent, let's sequence the deletion (children before parent)
            String parentId = WorkflowUtils.getParentId(wf, nodeId, toscaTypeFinder);
            NodeActivityStep deletedSourceStep = WorkflowUtils.getStateStepByNode(wf, nodeId, ToscaNodeLifecycleConstants.DELETED);
            AbstractStep targetStep = null;
            if (targetIsNative) {
                targetStep = WorkflowUtils.getDelegateWorkflowStepByNode(wf, targetId);
            } else {
                targetStep = WorkflowUtils.getStateStepByNode(wf, parentId, ToscaNodeLifecycleConstants.STOPPING);
            }
            WorkflowUtils.linkSteps(deletedSourceStep, targetStep);
        } /*
           * else if (WorkflowUtils.isOfType(indexedRelationshipType, NormativeRelationshipConstants.ATTACH_TO)) {
           * // in case of "Volume attached to Compute", we need to delete the compute before eventually delete the volume
           * String volumeId = nodeId;
           * String computeId = relationshipTemplate.getTarget();
           * NodeActivityStep deletedComputeStep = WorkflowUtils.getStateStepByNode(wf, computeId, ToscaNodeLifecycleConstants.DELETED);
           * NodeActivityStep stoppingVolumeStep = WorkflowUtils.getStateStepByNode(wf, volumeId, ToscaNodeLifecycleConstants.STOPPING);
           * WorkflowUtils.linkSteps(deletedComputeStep, stoppingVolumeStep);
           * }
           */
    }

}

@@ -3054,24 +3054,8 @@
    %7D
- /*%0A           *
  els
@@ -3143,14 +3143,16 @@
 nts.
-ATTACH
+CONNECTS
 _TO)
@@ -3166,17 +3166,16 @@
         
-*
  // in c
@@ -3280,33 +3280,32 @@
 lume%0A           
-*
  String volumeId
@@ -3330,75 +3330,8 @@
     
-* String computeId = relationshipTemplate.getTarget();%0A           *
  Nod
@@ -3344,30 +3344,22 @@
 ityStep 
-deletedCompute
+target
 Step = W
@@ -3394,23 +3394,22 @@
 ode(wf, 
-compute
+target
 Id, Tosc
@@ -3424,39 +3424,40 @@
 ecycleConstants.
-DELETED
+STOPPING
 );%0A           * 
@@ -3454,17 +3454,16 @@
         
-*
  NodeAct
@@ -3473,28 +3473,20 @@
 tyStep s
-toppingVolum
+ourc
 eStep = 
@@ -3522,21 +3522,19 @@
 ode(wf, 
-volum
+nod
 eId, Tos
@@ -3555,35 +3555,34 @@
 eConstants.STOPP
-ING
+ED
 );%0A           * 
@@ -3579,17 +3579,16 @@
         
-*
  Workflo
@@ -3608,77 +3608,42 @@
 eps(
-deletedComputeStep, stoppingVolumeStep);%0A           * %7D%0A           */
+sourceStep, targetStep);%0A        %7D
 %0A   

add a link between target.stopped and source.stopping