package me.qwq.doghouse.flexmark.ext.fancyquote;

import com.vladsch.flexmark.html.renderer.NodeRenderer;
import com.vladsch.flexmark.html.renderer.NodeRendererContext;
import com.vladsch.flexmark.html.renderer.NodeRenderingHandler;
import java.util.HashSet;
import java.util.Set;
import org.jetbrains.annotations.NotNull;

import com.vladsch.flexmark.html.HtmlWriter;
import com.vladsch.flexmark.html.renderer.NodeRendererFactory;
import com.vladsch.flexmark.util.data.DataHolder;
import com.vladsch.flexmark.util.sequence.BasedSequence;

public class FancyQuoteRenderer implements NodeRenderer{
    
    FancyQuoteOptions options;
    
    public FancyQuoteRenderer(DataHolder options) {
        this.options = new FancyQuoteOptions(options);
    }

    @Override
    public Set<NodeRenderingHandler<?>> getNodeRenderingHandlers() {
        HashSet<NodeRenderingHandler<?>> set = new HashSet<>();
        set.add(new NodeRenderingHandler<>(FancyQuote.class, this::render));
        return set;
    }

    private void render(FancyQuote node, NodeRendererContext context, HtmlWriter html) {
        CharSequence tag = node.getTag(options.fancyQuoteDefaultType);
        node.addClass(options.fancyQuoteDefaultClass);
        BasedSequence classes = node.getClasses();
        
        if (!classes.isBlank()) {
            html.attr("class", classes);
        }
        html.withAttr().tagLineIndent(tag, () -> context.renderChildren(node));
    }

    public static class Factory implements NodeRendererFactory {

        @Override
        public @NotNull NodeRenderer apply(@NotNull DataHolder options) {
            return new FancyQuoteRenderer(options);
        }
    }
}
