local function options2list(options)
    local options_list = {}
    for opt in options:gmatch("%S+") do
        table.insert(options_list, opt)
    end
    return options_list
end

local function markdown(options)
    local options_list = options2list(options or "")
    local cmd = vim.list_extend(
        { "pandoc" },
        options_list
    )
    table.insert(cmd, "-s")
    table.insert(cmd, vim.fn.expand("%:p"))
    table.insert(cmd, "-o")
    table.insert(cmd, vim.fn.expand("%:p:r") .. ".tex")
    local pandoc_process = vim.system(cmd)
    local pandoc_result = pandoc_process:wait()

    local pdflatex_process = vim.system({
        "pdflatex",
        vim.fn.expand("%:p:r") .. ".tex",
    })
    local pdflatex_result = pdflatex_process:wait()

    --vim.notify((pandoc_result.stdout .. "\n\n" .. pdflatex_result.stdout):gsub("^%s*", ""):gsub("%s*$", ""), vim.log.levels.INFO);
    vim.notify((pandoc_result.stderr .. "\n\n" .. pdflatex_result.stderr):gsub("^%s*", ""):gsub("%s*$", ""), vim.log.levels.ERROR);
end

local function arduino(options)
    local options_list = options2list(options or "")
    local options_string = ""
    for _, opt in pairs(options_list) do
        options_string = options_string .. " -D" .. opt
    end

    local process = vim.system({
        "arduino-cli",
        "compile",
        "-e",
        "--build-property",
        "compiler.cpp.extra_flags=" .. options_string,
         vim.fn.expand("%:p:h")
    })
    local result = process:wait()

    vim.notify(result.stdout, vim.log.levels.INFO)
    vim.notify(result.stderr, vim.log.levels.ERROR)
end

--local function arduino2()
--    local arduino_process = vim.system({
--        "arduino-cli",
--        "board",
--        "listall",
--        "--json"
--    })
--    local boards_json = arduino_process:wait()
--
--    local jq_process = vim.system({
--        "jq",
--        "[.boards.[] | {(.name): .fqbn}] | add",
--    }, { stdin = boards_json.stdout })
--    local boards_jq = jq_process:wait().stdout or {}
--    local boards_table = vim.json.decode(boards_jq)
--
--    local function table_keys(t)
--        local keys = {}
--        for k in pairs(t) do keys[#keys + 1] = k end
--        return keys
--    end
--
--    local keys = table_keys(boards_table)
--    table.sort(keys)
--    require("fzf-lua").fzf_exec(keys, {
--        actions = {
--            ['default'] = function(selected, opts)
--                local process = vim.system({
--                    "arduino-cli",
--                    "compile",
--                    "-e",
--                    "--fqbn",
--                    boards_table[selected[1]],
--                    vim.fn.expand("%:p:h")
--                })
--                local result = process:wait()
--
--                vim.notify(result.stdout, vim.log.levels.INFO)
--                vim.notify(result.stderr, vim.log.levels.ERROR)
--            end,
--        }
--    })
--end

local function tex()
    local pdflatex_process = vim.system({
        "pdflatex",
        vim.fn.expand("%:p"),
    })
    local pdflatex_result = pdflatex_process:wait()

    vim.notify(pdflatex_result.stdout, vim.log.levels.INFO);
    vim.notify(pdflatex_result.stderr, vim.log.levels.ERROR);
end

local default = {
    __index = function()
        return function() end
    end,
}

local fttable = {
    ["markdown"] = markdown,
    ["arduino"] = arduino,
    ["tex"] = tex,
}
setmetatable(fttable, default)

local fttable2 = {
    ["markdown"] = function() markdown(vim.fn.input("enter additional options")) end,
    ["arduino"] = function() arduino(vim.fn.input("enter preprocessor defines")) end,
}
setmetatable(fttable2, fttable)

vim.keymap.set("n", "<F5>", function() fttable[vim.bo.filetype]() end)
vim.keymap.set("n", "<F6>", function() fttable2[vim.bo.filetype]() end)

