package com.dua3.utility.text;

import java.util.Map;
import java.util.function.Function;

public abstract class AbstractStringBasedBuilder extends RichTextConverterBase<String> {

    /*
     * Definition of option names
     */

    /** Header */
    public static final String TAG_DOC_START = "TAG_DOC_START";
    /** Header */
    public static final String TAG_DOC_END = "TAG_DOC_END";
    /** Header */
    public static final String TAG_TEXT_START = "TAG_TEXT_START";
    /** Header */
    public static final String TAG_TEXT_END="TAG_TEXT_END";
    /** Where to open external links */
    public static final String TARGET_FOR_EXTERNAL_LINKS = "TARGET_FOR_EXTERN_LINKS";
    /** Replace '.md' file extension in local links (i.e. with ".html") */
    public static final String REPLACEMENT_FOR_MD_EXTENSION_IN_LINK = "REPLACEMENT_FOR_MD_EXTENSION_IN_LINK";

    /** The internal buffer that stores the HTML data. */
    protected StringBuilder buffer = new StringBuilder();
    /** Text to place at document start (i.e. document type declaration in HTML). */
    protected final String docStart;
    /** Text to place at document end. */
    protected final String docEnd;
    /** The text that starts HTML code (something like "{@code <html>}"). */
    protected final String textStart;
    /** The text that ends HTML code (something like "{@code </html>}"). */
    protected final String textEnd;
    /** The extension to use for MD-files (i.e. so that links point to the translated HTML). */
    protected final String replaceMdExtensionWith;
    /** Target for external links (i.e. in HTML). */
    protected final String targetForExternalLinks;

    protected AbstractStringBasedBuilder(Function<Style, TextAttributes> styleTraits, Map<String, String> options) {
        super(styleTraits);

        this.docStart = options.getOrDefault(TAG_DOC_START, "");
        this.docEnd = options.getOrDefault(TAG_DOC_END, "");
        this.textStart = options.getOrDefault(TAG_TEXT_START, "");
        this.textEnd = options.getOrDefault(TAG_TEXT_END, "");
        this.replaceMdExtensionWith = options.getOrDefault(REPLACEMENT_FOR_MD_EXTENSION_IN_LINK, "");
        this.targetForExternalLinks = options.getOrDefault(TARGET_FOR_EXTERNAL_LINKS, "_blank");

        buffer.append(docStart);
        buffer.append(textStart);
    }

    @Override
    protected void appendUnquoted(CharSequence chars) {
        buffer.append(chars);
    }

    @Override
    public String get() {
        buffer.append(textEnd);
        String text = buffer.toString();
        buffer = null;
        return text;
    }

    @Override
    protected boolean isValid() {
        return buffer != null;
    }

    @Override
    public String toString() {
        return buffer.toString();
    }

}

@@ -902,21 +902,16 @@
 res the 
-HTML 
 data. */
@@ -1190,44 +1190,41 @@
 xt t
-hat starts HTML code (something like
+o be prefixed to the buffer (i.e.
  %22%7B@
@@ -1232,24 +1232,32 @@
 ode %3Chtml%3E%7D%22
+ in HTML
 ). */%0A    pr
@@ -1310,42 +1310,41 @@
 xt t
-hat ends HTML code (something like
+o be appended to the buffer (i.e.
  %22%7B@
@@ -1357,16 +1357,24 @@
 /html%3E%7D%22
+ in HTML
 ). */%0A  

change javadoc comments