local home = os.getenv("HOME")
local jdtls_path = home .. "/.local/share/nvim/mason/packages/jdtls"
local workspace_dir = home .. "/.cache/jdtls/" .. vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local equinox_path = vim.fn.glob(jdtls_path .. "/plugins/org.eclipse.equinox.launcher_*.jar")

local on_attach = require('lsp-options').on_attach
local capabilities = require('lsp-options').capabilities

local keymap = vim.keymap

local opts = { silent = true }
-- ┌                                               ┐
-- │                     Java                      │
-- └                                               ┘
local java_on_attach = function(client, bufnr)
    on_attach(client, bufnr)

    opts.desc = "Organize Imports"
    keymap.set("n", "<leader>jo", "<cmd>lua require('jdtls').organize_imports()<CR>", opts)

    opts.desc = "Extract variable"
    keymap.set("n", "<leader>je", "<cmd>lua require('jdtls').extract_variable()<CR>", opts)

    opts.desc = "Extract constant"
    keymap.set("n", "<leader>je", "<cmd>lua require('jdtls').extract_constant()<CR>", opts)

    opts.desc = "Test current method"
    keymap.set("n", "<leader>jtm", "<cmd>lua require('jdtls').test_nearest_method()<CR>", opts)

    opts.desc = "Test current class"
    keymap.set("n", "<leader>jtc", "<cmd>lua require('jdtls').test_class()<CR>", opts)
end


require("jdtls").start_or_attach({
    cmd = {
        -- 💀
        "java", -- or '/path/to/java21_or_newer/bin/java'

        "-Declipse.application=org.eclipse.jdt.ls.core.id1",
        "-Dosgi.bundles.defaultStartLevel=4",
        "-Declipse.product=org.eclipse.jdt.ls.core.product",
        "-Dlog.protocol=true",
        "-Dlog.level=ALL",
        "-Xmx1g",
        "--add-modules=ALL-SYSTEM",
        "--add-opens",
        "java.base/java.util=ALL-UNNAMED",
        "--add-opens",
        "java.base/java.lang=ALL-UNNAMED",

        -- 💀
        "-jar",
        equinox_path,

        -- 💀
        "-configuration",
        jdtls_path .. "/config_linux",

        -- 💀
        -- See `data directory configuration` section in the README
        "-data",
        workspace_dir,
    },

    -- 💀
    -- This is the default if not provided, you can remove it. Or adjust as needed.
    -- One dedicated LSP server & client will be started per unique root_dir
    --
    -- vim.fs.root requires Neovim 0.10.
    -- If you're using an earlier version, use: require('jdtls.setup').find_root({'.git', 'mvnw', 'gradlew'}),
    root_dir = vim.fs.root(0, { ".git", "mvnw", "gradlew", "build.xml" }),

    -- Here you can configure eclipse.jdt.ls specific settings
    -- See https://github.com/eclipse/eclipse.jdt.ls/wiki/Running-the-JAVA-LS-server-from-the-command-line#initialize-request
    -- for a list of options
    settings = {
        java = {},
    },

    -- Language server `initializationOptions`
    -- You need to extend the `bundles` with paths to jar files
    -- if you want to use additional eclipse.jdt.ls plugins.
    --
    -- See https://github.com/mfussenegger/nvim-jdtls#java-debug-installation
    --
    -- If you don't plan on using the debugger or other eclipse.jdt.ls plugins you can remove this
    init_options = {
        bundles = vim.split(
            vim.fn.glob("$HOME/.local/share/nvim/mason/packages/java-*/extension/server/*.jar", true),
            "\n"
        ),
    },
    on_attach = java_on_attach,
    capabilities = capabilities,
})
