package tzatziki.pdf.emitter;

import com.google.common.base.Strings;
import com.google.common.base.Supplier;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Paragraph;
import gutenberg.itext.Emitter;
import gutenberg.itext.ITextContext;
import gutenberg.itext.ITextUtils;
import gutenberg.itext.Styles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tzatziki.analysis.java.ClassEntry;
import tzatziki.analysis.java.Grammar;
import tzatziki.analysis.java.GrammarVisitor;
import tzatziki.analysis.java.KeywordBasedPattern;
import tzatziki.analysis.java.MethodEntry;
import tzatziki.analysis.java.PackageEntry;

/**
 * @author <a href="http://twitter.com/aloyer">@aloyer</a>
 */
public class GrammarEmitter implements Emitter<Grammar> {

    private Logger log = LoggerFactory.getLogger(GrammarEmitter.class);

    @Override
    public void emit(Grammar grammar, final ITextContext context) {
        grammar.traverse(new GrammarVisitor() {
            @Override
            public void visit(PackageEntry packageEntry, ClassEntry classEntry, MethodEntry methodEntry) {
                Styles styles = context.styles();

                Font keywordFont = styles.getFontOrDefault(StepsEmitter.STEP_KEYWORD_FONT);
                Font regexFont = styles.getFont(Styles.INLINE_CODE_FONT).or(inlineCodeFont(styles));
                BaseColor regexBg = styles.getColor(Styles.INLINE_CODE_BACKGROUND).or(BaseColor.LIGHT_GRAY);

                Paragraph p = new Paragraph();
                p.setSpacingBefore(5f);
                if(!Strings.isNullOrEmpty(methodEntry.comment())) {
                    p.add(new Chunk(methodEntry.comment(), styles.defaultFont()));
                    p.add(Chunk.NEWLINE);
                }

                for (KeywordBasedPattern kwPattern : methodEntry.keywordBasedPatterns()) {
                    Chunk keywordChunk = new Chunk(kwPattern.getKeyword(), keywordFont);
                    Chunk regexChunk = new Chunk(kwPattern.getPattern(), regexFont);
                    regexChunk.setBackground(regexBg);

                    p.add(keywordChunk);
                    p.add(new Chunk(" "));
                    p.add(regexChunk);
                    p.add(Chunk.NEWLINE);
                }

                context.append(p);
            }
        });
    }

    private Supplier<? extends Font> inlineCodeFont(final Styles styles) {
        return new Supplier<Font>() {
            @Override
            public Font get() {
                try {
                    return new Font(ITextUtils.inconsolata(), styles.defaultFontSize());
                } catch (Exception e) {
                    log.warn("Fail to retrieve font", e);
                    return FontFactory.getFont(FontFactory.COURIER, styles.defaultFontSize());
                }
            }
        };
    }
}

@@ -171,16 +171,50 @@
 .Chunk;%0A
+import com.itextpdf.text.Element;%0A
 import c
@@ -232,24 +232,24 @@
 .text.Font;%0A
-
 import com.i
@@ -310,16 +310,96 @@
 agraph;%0A
+import com.itextpdf.text.pdf.draw.LineSeparator;%0Aimport gutenberg.itext.Colors;%0A
 import g
@@ -525,16 +525,55 @@
 Styles;%0A
+import gutenberg.itext.model.Markdown;%0A
 import o
@@ -887,16 +887,67 @@
 Entry;%0A%0A
+import java.util.Iterator;%0Aimport java.util.List;%0A%0A
 /**%0A * @
@@ -1855,16 +1855,17 @@
       if
+ 
 (!String
@@ -1930,68 +1930,132 @@
     
-p.add(new Chunk(methodEntry.comment(), styles.defaultFont())
+List%3CElement%3E subs = context.emitButCollectElements(Markdown.from(methodEntry.comment()));%0A                    p.addAll(subs
 );%0A 
@@ -2106,32 +2106,60 @@
              %7D%0A%0A
+                int nb = 0;%0A
                 
@@ -2229,24 +2229,105 @@
 tterns()) %7B%0A
+                    if(nb++ %3E  0)%0A                        p.add(Chunk.NEWLINE);%0A%0A
             
@@ -2675,32 +2675,243 @@
                 
+%7D%0A%0A                LineSeparator lineSeparator = new LineSeparator(1f, 50f, Colors.LIGHT_GRAY, Element.ALIGN_BASELINE, 0);%0A                p.add(Chunk.NEWLINE);%0A                p.add(lineSeparator);%0A            
     p.add(Chunk.
@@ -2919,34 +2919,16 @@
 EWLINE);
-%0A                %7D
 %0A%0A      

Improve Grammar rendering...