package de.jalin.jspwiki.filter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import com.ecyrd.jspwiki.WikiContext;
import com.ecyrd.jspwiki.filters.BasicPageFilter;
import com.ecyrd.jspwiki.filters.FilterException;

public class UseModFilter extends BasicPageFilter
{

    private String lookForward = null;

    public String preTranslate(WikiContext wikiContext, String content) throws FilterException
    {
        try
        {
            BufferedReader reader = new BufferedReader(new StringReader(content));
            String currentLine = readLine(reader);
            StringBuffer translationBuffer = new StringBuffer();
            while (currentLine != null) {
                if (currentLine.startsWith(":")) {
                    currentLine = translateIndent(reader, currentLine);
                }
                if (currentLine.startsWith("=")) {
                    currentLine = translateTitle(reader, currentLine);
                }
                if (currentLine.contains("'''''")) {
                    currentLine = currentLine.replaceFirst("'''''", "__''");
                    currentLine = currentLine.replaceFirst("'''''", "''__");
                }
                if (currentLine.contains("'''")) {
                    currentLine = currentLine.replaceAll("'''", "__");
                }
                if (currentLine.contains("<pre>") || currentLine.startsWith(" ")) {
                    currentLine = translatePreElement(reader, currentLine);
                }
                translationBuffer.append(currentLine);
                translationBuffer.append("\n");
                currentLine = readLine(reader);
            }
            return new String(translationBuffer);
        }
        catch (IOException e)
        {
            throw new FilterException("IOException im Filter\n" + e.getMessage());
        }
    }

    private String translateIndent(BufferedReader reader, String currentLine) throws IOException
    {
        int level = 0;
        while (currentLine.startsWith(":")) {
            level++;
            currentLine = currentLine.substring(1);
        }
        String nextLine = readLine(reader);
        if (nextLine != null && nextLine.length() > 0) {
            char firstChar = nextLine.charAt(0);
            while (nextLine != null && firstChar != ' ' && firstChar != '-' && firstChar != '!' && firstChar != '=' && firstChar != '*' && firstChar != '#' && firstChar != ':') {
                currentLine += "\n" + nextLine;
                nextLine = readLine(reader);
                if (nextLine != null && nextLine.length() > 0) {
                    firstChar = nextLine.charAt(0);
                }
            }
            if (nextLine != null) {
                lookForward = nextLine;
            }
        }
        currentLine = "%%(margin-bottom: 12pt; margin-top: 12pt; margin-left: " + (level * 24) + "pt)\n" + currentLine +"\n%%";
        return currentLine;
    }

    private String readLine(BufferedReader reader) throws IOException
    {
        if (lookForward != null) {
            String tmp = lookForward ;
            lookForward = null;
            return tmp;
        }
        return reader.readLine();
    }

    private String translatePreElement(BufferedReader reader, String currentLine) throws IOException
    {
        if (currentLine.startsWith(" ")) {
            if (currentLine.trim().length() == 0) {
                currentLine = "";
            } else {
                currentLine = "{{{\n" + currentLine;
                String nextLine = readLine(reader);
                while (nextLine != null && nextLine.startsWith(" ")) {
                    currentLine += "\n" + nextLine;
                    nextLine = readLine(reader);
                }
                currentLine += "\n}}}";
            }
        } else
        {
            currentLine = currentLine.replaceFirst("<pre>", "{{{");
            String nextLine = readLine(reader);
            while (!currentLine.contains("</pre>") && nextLine != null) {
                currentLine += "\n" + nextLine;
                nextLine = readLine(reader);
            }
            if (currentLine.contains("</pre>")) {
                currentLine = currentLine.replaceFirst("</pre>", "}}}");
            } else {
                currentLine += "\n}}}";
            }
        }
        return currentLine;
    }

    private String translateTitle(BufferedReader reader, String currentLine) throws IOException
    {
        String nextLine = null;
        while (!currentLine.trim().endsWith("=")) {
            nextLine = readLine(reader);
            if (nextLine != null) {
                currentLine += nextLine;
            } else {
                break;
            }
        }
        currentLine = currentLine.trim();
        if (currentLine.startsWith("====") && currentLine.endsWith("====")) {
            currentLine = "__" + currentLine.substring(4, currentLine.length() - 4) + "__";
        }
        if (!currentLine.startsWith("====") && currentLine.startsWith("===") && currentLine.endsWith("===")) {
            currentLine = "!" + currentLine.substring(3, currentLine.length() - 3);
        }
        if (!currentLine.startsWith("===") && currentLine.startsWith("==") && currentLine.endsWith("==")) {
            currentLine = "!!" + currentLine.substring(2, currentLine.length() - 2);
        }
        if (!currentLine.startsWith("==") && currentLine.startsWith("=") && currentLine.endsWith("=")) {
            currentLine = "!!!" + currentLine.substring(1, currentLine.length() - 1);
        }
        return currentLine;
    }

}
