#pragma once

#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <variant>
#include <utility>


using strings_t = std::vector<std::string>;
using attr_t = std::variant<int, float, std::string>;

class HTML;
using html_element_t = std::variant<std::string,HTML>;
using elements_t = std::vector<html_element_t>;

const std::string preamble_tag = "!DOCTYPE html";
const std::vector<std::string> block_elements {
    "!DOCTYPE", "html", "head", "body",
    "address", "article", "aside", "blockquote", "details", "dialog",
    "div", "dl", "dt", "dd", "fieldset", "figcaption", "figure", "footer", "form",
    "kt-part", "kt-chapter", "h1", "h2", "h3", "h4", "h5", "h6", "header", "hgroup", "hr", "img", "li",
    "main", "nav", "ol", "p", "section", "table", "ul", "td", "tr", "pre" };

std::ostream& operator<<(std::ostream& os, const elements_t& elements);
std::string to_string(elements_t e);


class HTML {
public:
    operator std::string() {
        std::stringstream ss {}; ss << *this; return ss.str(); };

    HTML(const std::string& tag) :
        m_tag(tag) {};

    HTML(const std::string& tag, const std::string& text) :
        m_tag(tag), m_text({text}) {};

    HTML(const std::string& tag, elements_t elements) :
        m_tag(tag), m_elements(elements) {};

    HTML& attr(const std::string& name, attr_t value);
    HTML& attrs(const std::string& s);

    HTML& cls(const std::string& name) { return attr("class", name); };
    HTML& sty(const std::string& css) { return attr("style", css); };
    HTML& id(const std::string& id) { return attr("id", id); };

    bool void_element(const std::string& tag) const;
    bool empty_element(const std::string& tag) const;
    friend std::ostream& operator<<(std::ostream& os, const HTML& h);
    friend std::ostream& operator<<(std::ostream& os, const std::vector<HTML>& hv);

private:
    std::string m_tag {};
    std::vector<std::pair<std::string, attr_t>> m_attrs {};
    std::string m_text {};
    elements_t m_elements {};

    inline static const std::vector<std::string> m_void_tags {
        "!DOCTYPE html", "area", "base", "br", "col", "command", "embed", "hr","img",
            "input", "keygen", "link", "meta", "param", "source", "track", "wbr" };

    inline static const std::vector<std::string> m_empty_tags { "script" };

};

namespace html {
    HTML elt(const std::string& tag);
    HTML elt(const std::string& tag, const std::string& text);
    HTML elt(const std::string& tag, elements_t elements);
    HTML elt(const std::string& tag, HTML e, elements_t elts);

    //HTML preamble();
    //HTML head(strings_t css_filenames, strings_t js_filenames);

    elements_t page(
        const std::string& title,
        std::string page_title,
        const std::string& output_dir,
        const std::string& nav,
        int max_level,
        const std::string& toc,
        const std::string& text,
        const std::string& date,
        const std::string& version,
        const std::string& copyright,
        const std::string& css,
        const strings_t& css_filenames = {},
        const strings_t& js_filenames = {},
        const strings_t& local_fonts = {},
        const std::string& logo = {});

    void add_title(elements_t& body, const std::string& title, const std::string& logo="");
    void add_title(elements_t& body, const std::string& title, const std::string& logo);
    void add_nav(elements_t& body, int max_level);
    void add_text(elements_t& body, const std::string& text, const std::string& toc_text, bool text_only);
    void add_bottom_spacer(elements_t& body);
    void add_status_bar(elements_t& body, const std::string& date, const std::string& version, const std::string& copyright);
    void add_page_cache(elements_t& body);
    void add_js_links(elements_t& body, const std::vector<std::string>& js_filenames, const std::string& output_dir);
    elements_t make_page(
        elements_t body,
        std::string page_title, const std::string& css,
        const std::vector<std::string>& css_filenames,
        const std::vector<std::string>& local_fonts);

    bool tag_is_block_element(const std::string& tag);
    std::string make_paragraphs(const std::string& html_text);
    std::string minimize_css(const std::string& src);
    std::string minimize_js(const std::string& src);

}
