package io.vrap.rmf.raml.generic.generator.postman;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.vrap.rmf.raml.generic.generator.Helper;
import io.vrap.rmf.raml.model.resources.Method;
import io.vrap.rmf.raml.model.resources.Resource;
import io.vrap.rmf.raml.model.types.*;
import org.apache.commons.lang3.StringEscapeUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

public class ActionGenModel extends ItemGenModel{
    private final ObjectType type;
    private final String example;
    private final List<String> testScript;

    public ActionGenModel(final ObjectType type, final Resource resource, final String template, final Method method) {
        super(resource, template, method);
        this.type = type;
        String example = null;

        if (type.getExamples().size() > 0) {
            final Instance instance = type.getExamples().get(0).getValue();
            example = Helper.toJson(instance);
            try {
                ObjectMapper mapper = new ObjectMapper();
                ObjectNode nodes = (ObjectNode)mapper.readTree(example);
                nodes.put("action", type.getDiscriminatorValue());

                example = Arrays.stream(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(nodes)
                        .split("\n")).map(s -> "  " + s)
                        .collect(Collectors.joining("\n"))
                        .trim();
            } catch (IOException e) {}
        }

        this.example = example;
        final Annotation t = type.getAnnotation("postman-test-script");
        if (t != null)  {
            this.testScript = Arrays.stream(((StringInstance) t.getValue()).getValue().split("\n")).collect(Collectors.toList());
        } else {
            this.testScript = null;
        }
    }

    public ObjectType getType() {
        return type;
    }

    public String getDiscriminatorValue() {
        return type.getDiscriminatorValue();
    }

    public List<String> getTestScript() {
        return testScript;
    }

    public String getExample() {
        return StringEscapeUtils.escapeJson(example);
    }

    @Override
    public String getDescription() {
        String description = Optional.ofNullable(type.getDescription()).map(StringInstance::getValue).orElse(type.getName());
        return StringEscapeUtils.escapeJson(description);
    }
}

@@ -1033,24 +1033,198 @@
 = null;%0A
-%0A
        
+ Instance instance = null;%0A%0A        if (type.getAnnotation(%22postman-example%22) != null)%7B%0A            instance = type.getAnnotation(%22postman-example%22).getValue();%0A        %7D else
  if (typ
@@ -1269,23 +1269,8 @@
     
-final Instance 
 inst
@@ -1306,32 +1306,75 @@
 (0).getValue();%0A
+        %7D%0A%0A        if (instance != null) %7B%0A
             exam

use examples from postman annotation in the collection