/*
 * Examples for Java
 *
 * Copyright (c) 2021. Elex. All Rights Reserved.
 * https://www.elex-project.com/
 */

package kr.pe.elex.examples;

import com.vladsch.flexmark.ext.abbreviation.AbbreviationExtension;
import com.vladsch.flexmark.ext.admonition.AdmonitionExtension;
import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension;
import com.vladsch.flexmark.ext.aside.AsideExtension;
import com.vladsch.flexmark.ext.attributes.AttributesExtension;
import com.vladsch.flexmark.ext.autolink.AutolinkExtension;
import com.vladsch.flexmark.ext.definition.DefinitionExtension;
import com.vladsch.flexmark.ext.emoji.EmojiExtension;
import com.vladsch.flexmark.ext.enumerated.reference.EnumeratedReferenceExtension;
import com.vladsch.flexmark.ext.escaped.character.EscapedCharacterExtension;
import com.vladsch.flexmark.ext.footnotes.FootnoteExtension;
import com.vladsch.flexmark.ext.gfm.issues.GfmIssuesExtension;
import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension;
import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension;
import com.vladsch.flexmark.ext.gfm.users.GfmUsersExtension;
import com.vladsch.flexmark.ext.gitlab.GitLabExtension;
import com.vladsch.flexmark.ext.ins.InsExtension;
import com.vladsch.flexmark.ext.jekyll.front.matter.JekyllFrontMatterExtension;
import com.vladsch.flexmark.ext.jekyll.tag.JekyllTagExtension;
import com.vladsch.flexmark.ext.macros.MacrosExtension;
import com.vladsch.flexmark.ext.media.tags.MediaTagsExtension;
import com.vladsch.flexmark.ext.resizable.image.ResizableImageExtension;
import com.vladsch.flexmark.ext.superscript.SuperscriptExtension;
import com.vladsch.flexmark.ext.tables.TablesExtension;
import com.vladsch.flexmark.ext.toc.TocExtension;
import com.vladsch.flexmark.ext.typographic.TypographicExtension;
import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension;
import com.vladsch.flexmark.ext.yaml.front.matter.AbstractYamlFrontMatterVisitor;
import com.vladsch.flexmark.ext.yaml.front.matter.YamlFrontMatterExtension;
import com.vladsch.flexmark.ext.youtube.embedded.YouTubeLinkExtension;
import com.vladsch.flexmark.html.HtmlRenderer;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.ast.Node;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import com.vladsch.flexmark.util.data.MutableDataSet;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
public class FlexMarkdownParser {
	private static final Parser PARSER;
	private static final HtmlRenderer RENDERER;

	@Getter
	private final Node document;

	static {
		final MutableDataHolder options = new MutableDataSet();
		options.set(
				Parser.EXTENSIONS,
				Arrays.asList(
						AbbreviationExtension.create(),
						AdmonitionExtension.create(),
						AnchorLinkExtension.create(),
						AsideExtension.create(),
						AttributesExtension.create(),
						AutolinkExtension.create(),
						DefinitionExtension.create(),
						EmojiExtension.create(),
						EnumeratedReferenceExtension.create(),
						EscapedCharacterExtension.create(),
						FootnoteExtension.create(),
						GfmIssuesExtension.create(),
						StrikethroughSubscriptExtension.create(),
						TaskListExtension.create(),
						GfmUsersExtension.create(),
						GitLabExtension.create(),
						InsExtension.create(),
						JekyllFrontMatterExtension.create(),
						JekyllTagExtension.create(),
						MacrosExtension.create(),
						MediaTagsExtension.create(),
						ResizableImageExtension.create(),
						SuperscriptExtension.create(),
						TablesExtension.create(),
						TocExtension.create(),
						TypographicExtension.create(),
						WikiLinkExtension.create(),
						YamlFrontMatterExtension.create(),
						YouTubeLinkExtension.create()
				));
		options.set(HtmlRenderer.SOFT_BREAK, "<br />\n");
		options.set(HtmlRenderer.GENERATE_HEADER_ID, true);
		//options.setFrom(ParserEmulationProfile.GITHUB);
		PARSER = Parser.builder(options).build();
		RENDERER = HtmlRenderer.builder(options).build();


	}

	public FlexMarkdownParser(final String md) {
		document = PARSER.parse(md);
	}

	public Map<String, List<String>> getYaml() {
		final AbstractYamlFrontMatterVisitor visitor = new AbstractYamlFrontMatterVisitor() {
		};
		visitor.visit(document);
		return visitor.getData();
	}

	public String getHtml() {
		return RENDERER.render(document);
	}

}
