package com.github.difflib.examples;

import com.github.difflib.UnifiedDiffUtils;
import org.junit.jupiter.api.Test;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;

// According to the original text, an html will be generated by comparing the text.
public class generateDiffHtmlTest {

    /**
     * Here's a simple example of getting a nice html page based on the original text and the contrasted text,
     * Read n1.txt and n2.txt of D disk, and finally generate an html file
     *
     */
    @Test
    public static void generateOriginalAndDiffDemo(){
        List<String> origLines = getFileContent("D:\\n1.txt");
        List<String> revLines = getFileContent("D:\\n2.txt");
        List<String> originalAndDiff =  UnifiedDiffUtils.generateOriginalAndDiff(origLines, revLines);

        //generateDiffHtml
        generateDiffHtml(originalAndDiff,"D:\\diff.html");
    }

    /**
     * get file content
     *
     * @param filePath file path
     */
    public static List<String> getFileContent(String filePath) {
        //原始文件
        List<String> fileContent = null;
        File file = new File(filePath);
        try {
            fileContent = Files.readAllLines(file.toPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileContent;
    }

    /**
     * The html file is generated by the difference diff between the two files,
     * and the detailed content of the file comparison can be seen by opening the html file
     *
     * @param diffString The comparison result obtained by calling the above diffString method
     * @param htmlPath   Generated html path，e.g:/user/var/mbos/ent/21231/diff.html
     */
    public static void generateDiffHtml(List<String> diffString, String htmlPath) {
        StringBuilder builder = new StringBuilder();
        for (String line : diffString) {
            builder.append(line);
            builder.append("\n");
        }
        String githubCss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.7.1/styles/github.min.css";
        String diff2htmlCss = "https://cdn.jsdelivr.net/npm/diff2html/bundles/css/diff2html.min.css";
        String diff2htmlJs = "https://cdn.jsdelivr.net/npm/diff2html/bundles/js/diff2html-ui.min.js";

        String template = "<!DOCTYPE html>\n" +
                "<html lang=\"en-us\">\n" +
                "  <head>\n" +
                "    <meta charset=\"utf-8\" />\n" +
                "    <link rel=\"stylesheet\" href=\"" + githubCss + "\" />\n" +
                "     <link rel=\"stylesheet\" type=\"text/css\" href=\"" + diff2htmlCss + "\" />\n" +
                "    <script type=\"text/javascript\" src=\"" + diff2htmlJs + "\"></script>\n" +
                "  </head>\n" +
                "  <script>\n" +
                "    const diffString = `\n" +
                "temp\n" +
                "`;\n" +
                "\n" +
                "\n" +
                "     document.addEventListener('DOMContentLoaded', function () {\n" +
                "      var targetElement = document.getElementById('myDiffElement');\n" +
                "      var configuration = {\n" +
                "        drawFileList: true,\n" +
                "        fileListToggle: true,\n" +
                "        fileListStartVisible: true,\n" +
                "        fileContentToggle: true,\n" +
                "        matching: 'lines',\n" +
                "        outputFormat: 'side-by-side',\n" +
                "        synchronisedScroll: true,\n" +
                "        highlight: true,\n" +
                "        renderNothingWhenEmpty: true,\n" +
                "      };\n" +
                "      var diff2htmlUi = new Diff2HtmlUI(targetElement, diffString, configuration);\n" +
                "      diff2htmlUi.draw();\n" +
                "      diff2htmlUi.highlightCode();\n" +
                "    });\n" +
                "  </script>\n" +
                "  <body>\n" +
                "    <div id=\"myDiffElement\"></div>\n" +
                "  </body>\n" +
                "</html>";
        template = template.replace("temp", builder.toString());
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(htmlPath);
            BufferedWriter buf = new BufferedWriter(fileWriter);
            buf.write(template);
            buf.close();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

