local status, jdtls = pcall(require, 'jdtls')
if not status then
	return
end
vim.cmd "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_compile JdtCompile lua require('jdtls').compile(<f-args>)"
vim.cmd "command! -buffer -nargs=? -complete=custom,v:lua.require'jdtls'._complete_set_runtime JdtSetRuntime lua require('jdtls').set_runtime(<f-args>)"
vim.cmd "command! -buffer JdtUpdateConfig lua require('jdtls').update_project_config()"
-- vim.cmd "command! -buffer JdtJol lua require('jdtls').jol()"
vim.cmd "command! -buffer JdtBytecode lua require('jdtls').javap()"

local keymap = vim.keymap.set
-- Silent keymap option
local opts = { silent = true }

keymap('n', '<leader>jo', "<Cmd>lua require'jdtls'.organize_imports()<CR>", opts)
keymap('n', '<leader>jv', "<Cmd>lua require('jdtls').extract_variable()<CR>", opts)
keymap('n', '<leader>jc', "<Cmd>lua require('jdtls').extract_constant()<CR>", opts)
keymap('n', '<leader>jt', "<Cmd>lua require'jdtls'.test_nearest_method()<CR>", opts)
keymap('n', '<leader>jT', "<Cmd>lua require'jdtls'.test_class()<CR>", opts)
keymap('n', '<leader>ju', '<Cmd>JdtUpdateConfig<CR>', opts)

keymap('v', '<leader>jv', "<Esc><Cmd>lua require('jdtls').extract_variable(true)<CR>", opts)
keymap('v', '<leader>jc', "<Esc><Cmd>lua require('jdtls').extract_constant(true)<CR>", opts)
keymap('v', '<leader>jm', "<Esc><Cmd>lua require('jdtls').extract_method(true)<CR>", opts)

vim.opt_local.shiftwidth = 2
vim.opt_local.tabstop = 2

local function on_attach(client, bufnr)
	--[[ vim.api.nvim_buf_set_option(bufnr, 'omnifunc', 'v:lua.vim.lsp.omnifunc') ]]
	if client.name == 'jdtls' or client.name == 'jdt.ls' then
		-- client.server_capabilities.documentFormattingProvider = false
		vim.lsp.codelens.refresh()
		if JAVA_DAP_ACTIVE then
			require('jdtls').setup_dap { hotcodereplace = 'auto' }
			require('jdtls.setup').add_commands()
			require('jdtls.dap').setup_dap_main_class_configs()
		end
	end
	-- -- util.info(client.name .. " " .. (enable and "yes" or "no"), "format")
	client.server_capabilities.documentFormattingProvider = true
	-- client.server_capabilities.documentFormattingProvider = true
	client.server_capabilities.semanticTokensProvider = nil
end
-- vim.opt_local.cmdheight = 2 -- more space in the neovim command line for displaying messages

-- Determine OS
local home = os.getenv 'HOME'
if vim.fn.has 'mac' == 1 then
	WORKSPACE_PATH = home .. '/.workspace/'
	CONFIG = 'mac'
elseif vim.fn.has 'unix' == 1 then
	WORKSPACE_PATH = home .. '/.workspace/'
	CONFIG = 'linux'
else
	print 'Unsupported system'
end

-- Find root of project
local root_markers = { '.git', 'mvnw', 'gradlew', 'pom.xml', 'build.gradle' }
local root_dir = require('jdtls.setup').find_root(root_markers)
if root_dir == '' then
	return
end

local extendedClientCapabilities = jdtls.extendedClientCapabilities
extendedClientCapabilities.resolveAdditionalTextEditsSupport = true

local project_name = vim.fn.fnamemodify(vim.fn.getcwd(), ':p:h:t')

local workspace_dir = WORKSPACE_PATH .. project_name

JAVA_DAP_ACTIVE = true

local bundles = {
	vim.fn.glob(
		home
			.. '/.local/share/nvim/mason/packages/java-debug-adapter/extension/server/com.microsoft.java.debug.plugin-*.jar'
	),
}

vim.list_extend(
	bundles,
	vim.split(vim.fn.glob(home .. '/.local/share/nvim/mason/packages/java-test/extension/server/*.jar'), '\n')
)

local config = {
	-- The command that starts the language server
	-- See: https://github.com/eclipse/eclipse.jdt.ls#running-from-the-command-line
	cmd = {

		-- 💀
		'java', -- or '/path/to/java11_or_newer/bin/java'
		-- depends on if `java` is in your $PATH env variable and if it points to the right version.

		'-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:' .. home .. '/.local/share/nvim/mason/packages/jdtls/lombok.jar',
		'-Xmx512m',
		'-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(home .. '/.local/share/nvim/mason/packages/jdtls/plugins/org.eclipse.equinox.launcher_*.jar'),
		-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                       ^^^^^^^^^^^^^^
		-- Must point to the                                                     Change this to
		-- eclipse.jdt.ls installation                                           the actual version

		-- 💀
		'-configuration',
		home .. '/.local/share/nvim/mason/packages/jdtls/config_' .. CONFIG,
		-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        ^^^^^^
		-- Must point to the                      Change to one of `linux`, `win` or `mac`
		-- eclipse.jdt.ls installation            Depending on your system.

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

	on_attach = on_attach,
	capabilities = capabilities,

	-- 💀
	-- 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
	root_dir = root_dir,

	-- 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
	-- or https://github.com/redhat-developer/vscode-java#supported-vs-code-settings
	-- for a list of options
	settings = {
		java = {
			eclipse = { downloadSources = true },
			configuration = { updateBuildConfiguration = 'interactive' },
			maven = { downloadSources = true },
			implementationsCodeLens = { enabled = true },
			referencesCodeLens = { enabled = true },
			references = { includeDecompiledSources = true },
			-- Set this to true to use jdtls as your formatter
			format = { enabled = true },
			signatureHelp = { enabled = true },
		},
		-- signatureHelp = { enabled = true },
		completion = {
			favoriteStaticMembers = {
				'org.hamcrest.MatcherAssert.assertThat',
				'org.hamcrest.Matchers.*',
				'org.hamcrest.CoreMatchers.*',
				'org.junit.jupiter.api.Assertions.*',
				'java.util.Objects.requireNonNull',
				'java.util.Objects.requireNonNullElse',
				'org.mockito.Mockito.*',
			},
			filteredTypes = {
				'com.sun.*',
				'io.micrometer.shaded.*',
				'java.awt.*',
				'jdk.*',
				'sun.*',
			},
		},
		contentProvider = { preferred = 'fernflower' },
		extendedClientCapabilities = extendedClientCapabilities,
		sources = {
			organizeImports = {
				starThreshold = 9999,
				staticStarThreshold = 9999,
			},
		},
		codeGeneration = {
			toString = {
				template = '${object.className}{${member.name()}=${member.value}, ${otherMembers}}',
			},
			useBlocks = true,
		},
	},

	flags = {
		allow_incremental_sync = true,
	},

	-- 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 = {},
		bundles = bundles,
	},
}

require('jdtls').start_or_attach(config)
