package seaclouds.utils.toscamodel.impl;
import seaclouds.utils.toscamodel.*;

/**
 * Created by pq on 05/04/15.
 */

import java.io.Reader;
import java.io.Writer;
import java.util.Collections;

public class ToscaEnvironment implements  IToscaEnvironment {

    private final TypeManager typeManager = new TypeManager(this);
    //private final ToscaTopology topology = new ToscaTopology(this);


    @Override
    public void readFile(Reader input, boolean sharedTypes) {
        final Parser parser = new Parser(this,sharedTypes);
        parser.Parse(input);
    }

    @Override
    public void writeFile(Writer output) {
        // todo
    }

    @Override
    public INamedEntity getNamedEntity(String entityName) {
        INamedEntity ret = null;
        // todo
//        if (ret == null)
//            ret = topology.getNodeTemplate(entityName);
        if (ret == null)
            ret = (INamedEntity)typeManager.getNodeTemplate(entityName);
        if (ret == null)
            ret = (INamedEntity)typeManager.getNodeType(entityName);
        if (ret == null)
            ret = (INamedEntity)typeManager.getType(entityName);

        return ret;
    }


    @Override
    public Iterable<INodeTemplate> getNodeTemplatesOfType(INodeType rootType) {
        //return topology.getNodeTemplatesOfType(INodeType rootType);
        return typeManager.getNodeTemplatesOfType(rootType);
    }

    @Override
    public Iterable<INodeType> getNodeTypesDerivingFrom(INodeType rootType) {
        return typeManager.getNodeTypesDerivingFrom(rootType);
    }

    @Override
    public Iterable<ITypeStruct> getTypesDerivingFrom(ITypeStruct rootType) {

        return typeManager.getTypesDerivingFrom(rootType);
    }

    @Override
    public INamedEntity registerType(String entityName, IType t) {
        return typeManager.registerType(entityName,t);
    }

    @Override
    public INamedEntity registerNodeType(String entityName, INodeType t) {
        return typeManager.registerNodeType(entityName,t);
    }

    @Override
    public INamedEntity registerNodeTemplate(String entityName, INodeTemplate t) {
        return typeManager.registerNodeTemplate(entityName, t);
    }

    @Override
    public INodeTemplate newTemplate(INodeType type) {
        return new NodeTemplate((NamedNodeType)type,"", Collections.emptyMap(),Collections.emptyMap());
    }
};

@@ -111,16 +111,44 @@
 5.%0A */%0A%0A
+import java.io.IOException;%0A
 import j
@@ -655,23 +655,215 @@
         
-// todo
+ToscaEmitter emitter = new ToscaEmitter();%0A        try %7B%0A            emitter.WriteDocument(output, this);%0A        %7D catch (IOException e) %7B%0A            throw new RuntimeException(%22zomg!%22);%0A        %7D%0A
 %0A    %7D%0A%0A

add binding to tosca environment for writer