local teaching = {}

local function ends_with(path, extension)
    return string.sub(path, -#extension) == extension
end

local function should_load(path)
    local ignore = {
        [1] = "rapidjson",
        [2] = "json.hpp",
        [3] = "build",
        [4] = "third-party",
        [5] = "sfml/",
        [6] = "SFML/",
        [7] = "TinyXML"
    }
    local match = {
        [1] = ".hpp",
        [2] = ".h",
        [3] = ".cpp",
        [4] = ".c",
    }
    for _, pattern in ipairs(ignore) do
        if string.find(path, pattern, 0, true) then
            return false
        end
    end
    for _, pattern in ipairs(match) do
        if ends_with(path, pattern) then
            return true
        end
    end
    return false
end

function teaching.load()
    -- vim.cmd("NvimTreeFindFileToggle")
    require("trouble").toggle({ mode = "diagnostics" })
    local scan = require("plenary.scandir").scan_dir('.')
    local cwd = vim.fn.getcwd()
    for _, v in ipairs(scan) do
        local filename = vim.fs.normalize(v)
        if should_load(v) then
            vim.api.nvim_set_current_dir(cwd)
            local buf_id = vim.fn.bufadd(filename)
            vim.api.nvim_set_current_buf(buf_id)
            vim.api.nvim_buf_set_option(buf_id, "buflisted", true)
        end
    end
    vim.api.nvim_set_current_dir(cwd)
end

vim.api.nvim_create_user_command("AP", teaching.load, {})

return teaching
