#include "MarkdownParser.h"

#include "File.h"

#include "HtmlDocument.h"
#include "MarkdownDocument.h"
#include "MarkdownConverter.h"
#include "MarkdownElement.h"
#include "HtmlWriter.h"

#include "TestFramework.h"
#include "TestUtils.h"

#include <iostream>

TEST_CASE(TestMarkdownParser, "[web]")
{
    File md_file(TestUtils::getTestDataDir() / "sample_markdown.md");
    const auto md_content = md_file.readText();

    MarkdownParser parser;
    auto md_doc = parser.run(md_content);

    std::vector<MarkdownElement::Type> expected_top_level = {
            MarkdownElement::Type::HEADING,
            MarkdownElement::Type::PARAGRAPH,
            MarkdownElement::Type::HEADING,
            MarkdownElement::Type::PARAGRAPH,
            MarkdownElement::Type::MULTILINE_QUOTE,
            MarkdownElement::Type::PARAGRAPH,
            MarkdownElement::Type::HEADING,
            MarkdownElement::Type::PARAGRAPH
            };

    REQUIRE(expected_top_level.size() <= md_doc->getNumElements());
    for(unsigned idx=0; idx<expected_top_level.size(); idx++)
    {
        REQUIRE(md_doc->getElement(idx)->getType() == expected_top_level[idx]);
    }

    MarkdownConverter converter;
    auto html = converter.convert(md_doc.get());

    HtmlWriter writer;
    const auto html_string = writer.toString(html.get());

    File html_file(TestUtils::getTestOutputDir(__FILE__)  / "TestMarkdownParser.html");
    html_file.writeText(html_string);
}

TEST_CASE(TestMarkdownParser_Simple, "[web]")
{
    File md_file(TestUtils::getTestDataDir() / "simple_markdown.md");
    const auto md_content = md_file.readText();

    REQUIRE(!md_content.empty());

    MarkdownParser parser;
    auto md_doc = parser.run(md_content);

    std::vector<MarkdownElement::Type> expected_top_level = {
            MarkdownElement::Type::PARAGRAPH,
            MarkdownElement::Type::BULLET_LIST};

    //REQUIRE(expected_top_level.size() <= md_doc->getNumElements());
    for(unsigned idx=0; idx<expected_top_level.size(); idx++)
    {
        //REQUIRE(md_doc->getElement(idx)->getType() == expected_top_level[idx]);
    }

    MarkdownConverter converter;
    auto html = converter.convert(md_doc.get());

    HtmlWriter writer;
    const auto html_string = writer.toString(html.get());

    File html_file(TestUtils::getTestOutputDir(__FILE__) / "TestMarkdownParser_simple.html");
    html_file.writeText(html_string);
}
