package liquidrods;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * A parsed template that can be rendered using {@link Template#render(Object, java.io.Writer)}
 */
public class Template {
    private List<LiquidrodsNode> rootNodes;
    private Config config;

    /**
     * Creates a template. You shouldn't be using this most of the time, but rather {@link Liquidrods#parse(java.io.Reader)} or {@link Liquidrods#parse(String)} to create a template.
     *
     * @param rootNodes the top level nodes of the template
     * @param config    the liquidrods instance to be used with this template.
     */
    public Template(List<LiquidrodsNode> rootNodes, Config config) {
        this.rootNodes = rootNodes;
        this.config = config;
        processIncludes();
        processExtends();
    }

    private void processIncludes() {
        List<LiquidrodsNode> mergedNodes = new ArrayList<LiquidrodsNode>(rootNodes.size());
        for (LiquidrodsNode node : rootNodes) {
            if (node instanceof LiquidrodsNode.Block) {
                LiquidrodsNode.Block block = (LiquidrodsNode.Block) node;
                if ("include".equals(block.getName())) {
                    final Template included = Liquidrods.parse(block.getArg(), config);
                    mergedNodes.addAll(included.getRootNodes());
                } else {
                    mergedNodes.add(block);
                }
            } else {
                mergedNodes.add(node);
            }
        }

        this.rootNodes = mergedNodes;
    }

    private void processExtends() {
        String parentTemplate = null;
        boolean nonText = false;
        Map<String, LiquidrodsNode.Block> blocks = new HashMap<String, LiquidrodsNode.Block>();
        for (LiquidrodsNode node : rootNodes) {
            if (node instanceof LiquidrodsNode.Variable) {
                nonText = true;
                if (parentTemplate != null) {
                    throw new RuntimeException("Invalid template: it extends another template yet it defines a top level variable (should be in a block tag)" + node);
                }
            } else if (node instanceof LiquidrodsNode.Block) {
                LiquidrodsNode.Block block = (LiquidrodsNode.Block) node;
                if ("extends".equals(block.getName())) {
                    if (parentTemplate != null) {
                        throw new RuntimeException("Invalid template: multiple extends directives: found " + node + " while and extend with" + parentTemplate + " was already defined");
                    } else if (nonText) {
                        throw new RuntimeException("Invalid template: it extends another template yet it defines a top level variable or block");
                    } else {
                        parentTemplate = block.getArg();
                    }
                } else if ("block".equals(block.getName())) {
                    blocks.put(block.getArg(), block);
                } else {
                    nonText = true;
                    if (parentTemplate != null) {
                        throw new RuntimeException("Invalid template: it extends another template yet it defines a top level block " + node);
                    }
                }
            }
        }

        if (parentTemplate != null) {
            Template parent = Liquidrods.parse(parentTemplate, config);
            List<LiquidrodsNode> mergedNodes = new ArrayList<LiquidrodsNode>(parent.rootNodes.size());
            for (LiquidrodsNode node : parent.rootNodes) {
                if (node instanceof LiquidrodsNode.Block) {
                    final LiquidrodsNode.Block block = (LiquidrodsNode.Block) node;
                    if ("block".equals(block.getName())) {
                        LiquidrodsNode.Block toInsert = blocks.get(block.getArg());
                        if (toInsert != null) {
                            mergedNodes.add(toInsert);
                        } else {
                            mergedNodes.add(block);
                        }
                    } else {
                        mergedNodes.add(block);
                    }
                } else {
                    mergedNodes.add(node);
                }
            }
            this.rootNodes = mergedNodes;
        }
    }

    private Map<String, List<LiquidrodsNode>> processDefines(LiquidrodsNode.Block extend) {
        Map<String, List<LiquidrodsNode>> defines = new HashMap<String, List<LiquidrodsNode>>();
        for (LiquidrodsNode node : extend.getChildren()) {
            if (node instanceof LiquidrodsNode.Variable) {
                throw new RuntimeException("Invalid template: the variable " + node + " must appear inside a define block");
            } else if (node instanceof LiquidrodsNode.Block) {
                LiquidrodsNode.Block block = (LiquidrodsNode.Block) node;
                if ("define".equals(block.getName())) {
                    defines.put(block.getArg(), block.getChildren());
                } else {
                    throw new RuntimeException("Invalid template: the block " + node + " must appear inside a define block");
                }
            }
        }
        return defines;
    }

    public List<LiquidrodsNode> getRootNodes() {
        return rootNodes;
    }

    /**
     * Render this template using the specified model into the specified writer
     *
     * @param model the model object to resolve properties against
     * @param out   where to write the result
     */
    public void render(Object model, Writer out) {
        Context context = new Context(null, model);
        try {
            for (LiquidrodsNode node : rootNodes) {
                config.defaultRenderer().render(node, context, config, out);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String toString() {
        return "Template " + rootNodes;
    }
}

@@ -4407,927 +4407,8 @@
  %7D%0A%0A
-    private Map%3CString, List%3CLiquidrodsNode%3E%3E processDefines(LiquidrodsNode.Block extend) %7B%0A        Map%3CString, List%3CLiquidrodsNode%3E%3E defines = new HashMap%3CString, List%3CLiquidrodsNode%3E%3E();%0A        for (LiquidrodsNode node : extend.getChildren()) %7B%0A            if (node instanceof LiquidrodsNode.Variable) %7B%0A                throw new RuntimeException(%22Invalid template: the variable %22 + node + %22 must appear inside a define block%22);%0A            %7D else if (node instanceof LiquidrodsNode.Block) %7B%0A                LiquidrodsNode.Block block = (LiquidrodsNode.Block) node;%0A                if (%22define%22.equals(block.getName())) %7B%0A                    defines.put(block.getArg(), block.getChildren());%0A                %7D else %7B%0A                    throw new RuntimeException(%22Invalid template: the block %22 + node + %22 must appear inside a define block%22);%0A                %7D%0A            %7D%0A        %7D%0A        return defines;%0A    %7D%0A%0A
     

Delete unused method