package com.dua3.utility.text;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.junit.Assert;
import org.junit.Test;

import com.dua3.utility.io.IOUtil;

public class MarkDownTest {

    public static void main(String[] args) throws Exception {
        Charset cs = StandardCharsets.UTF_8;

        if (args.length == 0) {
        	System.out.println(getAnsi());
        }

        if (args.length== 1 && args[0].equals("-update")) {
	        System.err.println("WARNING! This will overwrite expected unit test results!!!\nEnter 'YES' to continue.");
	        if (!"YES".equals(new BufferedReader(new InputStreamReader(System.in, cs)).readLine())) {
	            System.err.println("aborted.");
	            System.exit(1);
	        }

	        String html = getHtml();
	        Path htmlPath = Paths.get(MarkDownTest.class.getResource("syntax.html").toURI());
	        try (PrintStream out = new PrintStream(htmlPath.toFile(), cs.name())) {
	            out.print(html);
	            System.out.println("Wrote new expected unit test result.");
	            System.err.println("Copy " + htmlPath + " to resources folder to permanently update tests.");
	        }
        }
    }

    static String getHtml() throws Exception {
        String mdText = getTestDataSource();
        RichText richText = MarkDownUtil.convert(mdText);
        return HtmlBuilder.toHtml(richText, MarkDownStyle::getAttributes);
    }

    static String getAnsi() throws Exception {
        String mdText = getTestDataSource();
        RichText richText = MarkDownUtil.convert(mdText);
        return AnsiBuilder.toAnsi(richText, MarkDownStyle::getAttributes);
    }

    @Test
    public void testMarkDown() throws Exception {
        String htmlActual = getHtml();
        String htmlExpected = getTestDataExpectedHtml();

        Assert.assertEquals(htmlExpected, htmlActual);
    }

    public static String getTestData(String filename) throws Exception {
        return IOUtil.read(Paths.get(MarkDownTest.class.getResource(filename).toURI()), StandardCharsets.UTF_8);
    }

    public static String getTestDataSource() throws Exception {
        return getTestData("syntax.md");
    }

    public static String getTestDataExpectedHtml() throws Exception {
        return getTestData("syntax.html");
    }
}

@@ -48,32 +48,60 @@
 BufferedReader;%0A
+import java.io.IOException;%0A
 import java.io.I
@@ -146,16 +146,89 @@
 Stream;%0A
+import java.io.UncheckedIOException;%0Aimport java.net.URISyntaxException;%0A
 import j
@@ -1488,33 +1488,16 @@
 etHtml()
- throws Exception
  %7B%0A     
@@ -1703,33 +1703,16 @@
 etAnsi()
- throws Exception
  %7B%0A     
@@ -1931,33 +1931,16 @@
 rkDown()
- throws Exception
  %7B%0A     
@@ -2147,35 +2147,36 @@
 lename) 
-throws Exception %7B%0A
+%7B%0A        try %7B%0A    
         
@@ -2280,16 +2280,195 @@
 UTF_8);%0A
+        %7D catch (IOException e) %7B%0A            throw new UncheckedIOException(e);%0A        %7D catch (URISyntaxException e) %7B%0A            throw new IllegalStateException();%0A        %7D%0A
     %7D%0A%0A 
@@ -2510,33 +2510,16 @@
 Source()
- throws Exception
  %7B%0A     
@@ -2611,33 +2611,16 @@
 edHtml()
- throws Exception
  %7B%0A     

remove exception declarations