#include <string>
#include <map>
#include "util.h"
#include "eval.h"
#include "machine.h"
#include "error.h"

    //Machine M {};
    //Text T(true);

using namespace std;


string single_page(Strings pages)
{
    // Combine files, surround with top-level 
    stringstream body {};
    for (string page : pages) {
	body << page << "\n\n";
    }
    stringstream html {};
    html << "<html>\n<body>\n" << body.str() << "</body></html>";
    return html.str();
}

string multi_page(Strings pages)
{
    // Write chapter files and index.html; return empty chapter HTML filenames
    return "MULTI_PAGE";
}

	    
string chapterbook_html(
    string title, string subtitle, Strings chapters, Strings chapter_texts, bool directory_output)
{
    SHOW;
    debug = xdebug = true;
    Text T(true);
    T.parse();
    Machine M {};
    M.state.statevars.set("_program_name", "book");
    M.state.statevars.set("_sks_enabled", "true");
    M.extract_definitions(T);

    Strings pages {};
    for (unsigned int i = 0; i < chapters.size(); i++) {
	cout << "Processing chapter: " << chapters[i] << "\n";
       M.preserve_parse(T);
	//cout << chapter_texts[i] << "\n";
        Strings ss { chapter_texts[i] };
        Text T(ss, {}, false);
        T.parse();
        M.extract_definitions(T);
        M.apply("html", T);
	//cout << M.to_string("html", T, true, false);
	pages.push_back(M.to_string("html", T, false, false));
        M.restore_parse(T);
    }
    if (directory_output)
	return multi_page(pages);
    else
	return single_page(pages);
}

string chapterbook_tex(
    string title, string subtitle, Strings chapters, Strings chapter_texts, int leading)
{
    SHOW;
    stringstream ss {};    
    ss << "@document :title " << title <<  " :subtitle " << subtitle << " :leading " << leading << " |\n ";
    for (string s : chapter_texts)
	ss << s << "\n\n";
    ss << "@\n";
    //cout << ss.str() << "\n";
    return ss.str();
}

bool parse_output_filename(string output_filename, string target)
{
    return (extension(output_filename) != target) and (output_filename != "-");
}


extern "C" VISIBLE string chapterbook(std::map<std::string, std::string> args)
{
    SHOW;
    show_args(args);

    string title = args["title"];
    string subtitle = args["subtitle"];

    Strings chapters = word_split(args["chapters"]);
    Strings chapter_texts {};
    for (auto c : chapters) {
	string filename { "kt/" + c + ".kt" };
	cout << "Reading " << filename << "\n";
	string chapter = string_from_file(filename);
	chapter = regex_split(chapter, regex("@###"))[0];
	//cout << chapter << "\n\n";
	chapter_texts.push_back(chapter);
    }
    string target = args["_target"];
    if (target == "html") {
	bool directory_output = parse_output_filename(args["_output_filename"], target);
        return chapterbook_html(title, subtitle, chapters, chapter_texts, directory_output);
    } else if (target == "tex") {
	int leading = stoi(args["leading"]);
        return chapterbook_tex(title, subtitle, chapters, chapter_texts, leading);
    } else {
	stringstream msg {};
	msg << "The \"book\" klammer is not defined for target \"" << target << "\".";
	throw Definition_error(msg.str(), Locator(__FILE__, __LINE__, 0));
    }

}
