local Path = require("plenary.path")
local root_dir = require("jdtls.setup").find_root({ ".git", "mvnw", "gradlew", "pom.xml", "build.gradle" })
local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ":p:h:t")
local workspace_dir = vim.fn.stdpath("cache") .. "/jdtls/workspace-root/" .. project_name
if vim.loop.fs_stat(workspace_dir) == nil then
	Path:new(workspace_dir):mkdir({ parents = true })
end

local jdtls_path = vim.fn.expand("$MASON/packages/jdtls")
local java_debug_path = vim.fn.expand("$MASON/packages/java-debug-adapter")
local java_test_path = vim.fn.expand("$MASON/packages/java-test")

local os
if vim.fn.has("macunix") then
	os = "mac"
else
	os = "linux"
end

local bundles = {
	vim.fn.glob(java_debug_path .. "/extension/server/com.microsoft.java.debug.plugin-*.jar", true),
}
vim.list_extend(bundles, vim.split(vim.fn.glob(java_test_path .. "/extension/server/*.jar", true), "\n"))

local opts = {
	cmd = {
		"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",
		"-javaagent:" .. jdtls_path .. "/lombok.jar",
		"-XX:+UseParallelGC",
		"-XX:GCTimeRatio=4",
		"-XX:AdaptiveSizePolicyWeight=90",
		"-Dsun.zip.disableMemoryMapping=true",
		"-Xmx4G",
		"-Xms100m",
		"--add-modules=ALL-SYSTEM",
		"--add-opens",
		"java.base/java.util=ALL-UNNAMED",
		"--add-opens",
		"java.base/java.lang=ALL-UNNAMED",
		"-jar",
		vim.fn.glob(jdtls_path .. "/plugins/org.eclipse.equinox.launcher_*.jar"),
		"-configuration",
		jdtls_path .. "/config_" .. os,
		"-data",
		workspace_dir,
	},
	root_dir = root_dir,
	init_options = {
		bundles = bundles,
	},
	settings = {
		java = {
			inlayHints = {
				parameterNames = {
					enabled = "all",
					exclusions = { "this" },
				},
			},
		},
	},
}

vim.api.nvim_create_autocmd("Filetype", {
	pattern = "java",
	callback = function()
		require("jdtls").start_or_attach(opts)

		-- local timer = 5000
		-- for i = 0, 12, 1 do
		-- 	vim.defer_fn(function()
		-- 		require("jdtls.dap").setup_dap_main_class_configs()
		-- 	end, timer)
		-- 	timer = timer + 2500
		-- end
	end,
})
