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)

coverpage = '\\coverpage'
titleheading = '\\titleheading'
tableofcontents = '\\tableofcontents'
compiledtime = '\\compiledTime'
printbib = '\\printbibliography[heading=bibintoc,title={参考文献}]'
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_stu_seating_num(cfg)
    return "\\seatingnum{" .. cfg.author.seating_number .. "}"
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 get_professor(cfg)
    return "\\professor{" .. cfg.professor .. "}"
end

function get_additional_packages(cfg)
    local res = ""
    for i = 1, #cfg.packages do
        local options = ""
        if cfg.packages[i].options ~= "" then
            options = "[" .. cfg.packages[i].options .. "]"
        end
        res = res .. "\\usepackage" .. options .. "{" .. cfg.packages[i].name .. "}\n"
    end

    return res
end

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

function format_turnin_date(cfg)
    return "\\turnindate{" .. cfg.turnin.year .. "}{" .. cfg.turnin.month .. "}{" .. cfg.turnin.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 seating = get_stu_seating_num(cfg) .. "\n"
    local date = format_date(cfg) .. "\n"
    local turnin = format_turnin_date(cfg) .. "\n"
    local school = get_school_name(cfg) .. "\n"
    local dep = get_dep(cfg) .. "\n"
    local subject = get_subject(cfg) .. "\n"
    local professor = get_professor(cfg) .. "\n"
    local additional_packages = ""
    local pgnum = ""
    if cfg.paper_config.include_cover_page == true and cfg.doc_class ~= "nitonepage" then
        pgnum = "\n" .. romannumbering .. "\n"
    end
    if cfg.paper_config.use_additional_packages == true then
        additional_packages = "\n" .. get_additional_packages(cfg)
    end
    return doc_class .. title .. name .. stu_id .. seating .. date .. turnin .. school .. dep .. subject .. professor .. additional_packages .. pgnum .. "\n"
end

function report_content(cfg)
    local res = "\\begin{document}\n"
    if cfg.paper_config.include_cover_page == true then
        res = res .. "    " .. coverpage .. "\n\n"
    end
    if cfg.paper_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.paper_config.use_bib == true then
        res = res .. "    " .. printbib .. "\n\n"
    end
    if cfg.paper_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))

config_file:close()
