local lyaml = require "lyaml"

local config_file = io.open("document.yaml", 'r')
local yml_str = config_file:read('a')
local config = lyaml.load(yml_str)

local tex_file = io.open("main.tex", 'w+')

coverpage = '\\coverpage'
titleheading = '\\titleheading'
tableofcontents = '\\tableofcontents'
compiledtime = '\\compiledTime'
printbib = '\\printbibliography'
newpg = '\\newpage'
romannumbering = '\\pagenumbering{roman}'
arabicnumbering = '\\pagenumbering{arabic}'

function get_doc_class(cfg)
    return "\\documentclass{class/" .. cfg.doc_class .. "}"
end

function get_title(cfg)
    return "\\reporttitle{" .. cfg.title .. "}"
end

function get_author(cfg)
    return "\\reportauthor{" .. cfg.author.name .. "}"
end

function get_stu_id(cfg)
    return "\\studentid{" .. cfg.author.student_id .. "}"
end

function get_school_name(cfg)
    return "\\schoolname{" .. cfg.school_name .. "}"
end

function get_dep(cfg)
    return "\\department{" .. cfg.department .. "}"
end

function get_subject(cfg)
    return "\\subject{" .. cfg.subject .. "}"
end

function format_date(cfg)
    return "\\reportdate{" .. cfg.date.year .. "}{" .. cfg.date.month .. "}{" .. cfg.date.day .. "}"
end

function preamble(cfg)
    local doc_class = get_doc_class(cfg) .. "\n\n"
    local title = get_title(cfg) .. "\n"
    local name = get_author(cfg) .. "\n"
    local stu_id = get_stu_id(cfg) .. "\n"
    local date = format_date(cfg) .. "\n"
    local school = get_school_name(cfg) .. "\n"
    local dep = get_dep(cfg) .. "\n"
    local subject = get_subject(cfg) .. "\n"
    local pgnum = ""
    if cfg.page_config.include_cover_page == true and cfg.doc_class ~= "nitonepage" then
        pgnum = "\n" .. romannumbering .. "\n"
    end
    return doc_class .. title .. name .. stu_id .. date .. school .. dep .. subject .. pgnum .. "\n"
end

function report_content(cfg)
    local res = "\\begin{document}\n"
    if cfg.page_config.include_cover_page == true then
        res = res .. "    " .. coverpage .. "\n\n"
    end
    if cfg.page_config.include_table_of_contents == true then
        res = res .. "    " .. tableofcontents .. "\n    " .. newpg .. "\n"
    end
    res = res .. "    " .. arabicnumbering .. "\n\n"
    for i = 1,  #cfg.sections do
        res = res .. "    " .. "\\input{" .. cfg.sections[i].path .. "}\n"
        if cfg.sections[i].newpg == true then
            res = res .. "    " .. newpg .. "\n\n"
        else
            res = res .. "\n"
        end
    end
    if cfg.page_config.use_bib == true then
        res = res .. "    " .. printbib .. "\n\n"
    end
    if cfg.page_config.show_compiled_time == true then
        res = res .. "    " .. compiledtime .. "\n"
    end
    res = res .. "\\end{document}"
    return res
end

function onepage_content(cfg)
    local res = "\\begin{document}\n"
    res = res .. "    " .. titleheading .. "\n\n"
    res = res .. "    Content Here\n\n"
    res = res .. "\\end{document}"
    return res
end

function generate(cfg)
    local pre = preamble(cfg)
    local doc = ""
    local file_content = ""
    if cfg.doc_class == "nitreport" then
        doc = report_content(cfg)
    elseif cfg.doc_class == "nitonepage" then
        doc = onepage_content(cfg)
    else
        print("Error: Invalid document class option")
        os.exit(1)
    end
    file_content = pre .. doc
    return file_content
end

print(generate(config))

tex_file:write(generate(config))

config_file:close()
tex_file:close()
