local function handle_enter()
    local util = require("obsidian.util")
    if util.cursor_on_markdown_link(nil, nil, true) then
        vim.cmd("ObsidianFollowLink")
    else
        local line = vim.api.nvim_get_current_line()
        if string.match(line, "^%s*- %[[x ]%].*") then
            util.toggle_checkbox()
        else
            vim.cmd("normal! viw:'<,'>ObsidianLink")
        end
    end
end

return {
    "epwalsh/obsidian.nvim",
    version = "*",  -- recommended, use latest release instead of latest commit
    lazy = false,
    dependencies = { "nvim-lua/plenary.nvim" },
    config = function(_, opts)
        local notes_path = (os.getenv "HOME") .. "/Workspace/Notes"
        if vim.fn.isdirectory(notes_path) == 0 then
            return
        end
        local scan = require("plenary.scandir").scan_dir(notes_path, {add_dirs = true,  depth = 1 })
        local found = false
        for _, v in ipairs(scan) do
            local subdir = v:match("([^/]+)$")
            found = true
            if vim.fn.isdirectory(notes_path .. "/" .. subdir .. "/.obsidian") == 1 then
                table.insert(opts.workspaces, {
                    name = subdir,
                    path = notes_path .. "/" .. subdir,
                    overrides = {
                        daily_notes = {
                            folder = "Daily",
                            template = "Daily"
                        },
                    },
                })
            end
        end
        if found then
            require("obsidian").setup(opts)
        end
    end,
    opts = {
        workspaces = {},
        note_id_func = function(title) return title end,
        daily_notes = {
            folder = "."
        },
        templates = {
            folder = (os.getenv "HOME") .. "/Workspace/Notes/Templates",
            -- date_format = "%Y-%m-%d",
            date_format = "%B %-d, %Y",
            time_format = "%H:%M",
            substitutions = {},
        },
        disable_frontmatter = true,
        ui = {
            hl_groups = {
                ObsidianRefText = { fg = "#61afef" },
            },
            checkboxes = {
                [" "] = { char = "󰄱", hl_group = "ObsidianTodo" },
                ["x"] = { char = "", hl_group = "ObsidianDone" },
            }
        }
    },

    keys = {
        {"<cr>", handle_enter, ft = "markdown" },
        {"<cr>", "<cmd>ObsidianLink<cr>", ft = "markdown", mode = "v"},
        {"<leader>rn", "<cmd>ObsidianRename<cr>", ft = "markdown" },
        {"<leader>l", "i- [ ] ", ft = "markdown"},
        {"<leader>o", "o- [ ] ", ft = "markdown"},
        {"<leader>l", "- [ ] ", ft = "markdown", mode = "i" },
        {"<leader>jt", "<cmd>ObsidianToday<cr>"},
        {"<leader>jy", "<cmd>ObsidianYesterday<cr>"},
        {"<leader>ju", "<cmd>ObsidianTomorrow<cr>"},
        {"<tab>", ":set nohlsearch<cr>/\\[[^\\[\\]]*\\]([^()]*)<cr>:let @/ = \"\"<cr>:set hlsearch<cr>", ft = "markdown", silent = true },
    },
}
