local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"https://github.com/folke/lazy.nvim.git",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end
vim.opt.rtp:prepend(lazypath)
require("lazy").setup({
	spec = {
		-- import your plugins
		{ import = "plugins" },
	},
	-- Configure any other settings here. See the documentation for more details.
	-- colorscheme that will be used when installing plugins.
	install = { colorscheme = { "catppuccin" } },
	-- automatically check for plugin updates
	checker = { enabled = true },
})

if vim.fn.has("persistent_undo") then
	local target_path = vim.fn.stdpath("data") .. "/.nvimundodir"
	if not vim.fn.isdirectory(target_path) then
		vim.fn.mkpath(target_path, { mode = 0700 }) -- Use mkpath for parent creation
	end

	vim.opt.undodir = target_path
	vim.api.nvim_set_option("undofile", true)
end

vim.api.nvim_create_autocmd("StdinReadPre", {
	pattern = "*",
	command = "let s:std_in=1",
})
--vim.api.nvim_create_autocmd("VimEnter", {
--	pattern = "*",
--	command = "if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in') | execute 'Neotree current' | execute 'cd '.argv()[0] | endif"
--})

vim.g.airline_powerline_fonts = 1

--vim.g.coq_settings = {'clients.tabnine.enabled' = false}
--filetype plugin on
--vim.g.syntastic_python_python_exec = 'python3'
--vim.g.syntastic_python_checkers = ['bandit', 'python']

--" Keybinds
vim.keymap.set("n", "<F2>", ":Neotree toggle<CR>", { noremap = true })
vim.keymap.set("n", "<F3>", function() vim.lsp.buf.code_action() end, { noremap = true })
vim.keymap.set("n", "<F4>", ":ZenMode<CR>", { noremap = true })
vim.keymap.set("n", "<F5>", function() vim.lsp.buf.format() end, { noremap = true })
vim.keymap.set("n", "<F6>", ":MundoToggle<CR>", { noremap = true })

--nnoremap <F4> :Goyo<CR>
--nnoremap <F5> :MundoToggle<CR>
--nnoremap <F6> :lua require("dapui").toggle()<CR>
require("mason").setup()
require("mason-lspconfig").setup()
--require("mason-lspconfig").setup_handlers({
--     function (server_name) -- default handler (optional)
--       require("lspconfig")[server_name].setup(require('coq').lsp_ensure_capabilities({}))
--     end})
-- local lsp = vim.lsp.config()
local dap = require("dap")
dap.adapters.gdb = {
	type = "executable",
	command = "gdb",
	args = { "-i", "dap" },
}

dap.configurations.cpp = {
	{
		name = "Launch",
		type = "gdb",
		request = "launch",
		program = function()
			return vim.fn.input("Path to executable: ", vim.fn.getcwd() .. "/", "file")
		end,
		cwd = "${workspaceFolder}",
		stopAtBeginningOfMainSubprogram = false,
	},
}
-- Mappings.
-- See `:help vim.diagnostic.*` for documentation on any of the below functions
local opts = { noremap = true, silent = true }
vim.keymap.set("n", "<space>e", vim.diagnostic.open_float, opts)
vim.keymap.set("n", "<space>q", vim.diagnostic.setloclist, opts)

-- Use an on_attach function to only map the following keys
-- after the language server attaches to the current buffer
local on_attach = function(client, bufnr)
	-- Enable completion triggered by <c-x><c-o>
	vim.api.nvim_buf_set_option(bufnr, "omnifunc", "v:lua.vim.lsp.omnifunc")

	-- Mappings.
	-- See `:help vim.lsp.*` for documentation on any of the below functions
	local bufopts = { noremap = true, silent = true, buffer = bufnr }
	vim.keymap.set("n", "gD", vim.lsp.buf.declaration, bufopts)
	vim.keymap.set("n", "gd", vim.lsp.buf.definition, bufopts)
	vim.keymap.set("n", "K", vim.lsp.buf.hover, bufopts)
	vim.keymap.set("n", "gi", vim.lsp.buf.implementation, bufopts)
	vim.keymap.set("n", "<C-k>", vim.lsp.buf.signature_help, bufopts)
	vim.keymap.set("n", "<space>wa", vim.lsp.buf.add_workspace_folder, bufopts)
	vim.keymap.set("n", "<space>wr", vim.lsp.buf.remove_workspace_folder, bufopts)
	vim.keymap.set("n", "<space>wl", function() print(vim.inspect(vim.lsp.buf.list_workspace_folders())) end, bufopts)
	vim.keymap.set("n", "<space>D", vim.lsp.buf.type_definition, bufopts)
	vim.keymap.set("n", "<space>rn", vim.lsp.buf.rename, bufopts)
	vim.keymap.set("n", "<space>ca", vim.lsp.buf.code_action, bufopts)
	vim.keymap.set("n", "gr", vim.lsp.buf.references, bufopts)
	vim.keymap.set("n", "<space>f", function() vim.lsp.buf.format({ async = true }) end, bufopts)
end

local lsp_flags = {
	-- This is the default in Nvim 0.7+
	debounce_text_changes = 150,
}

--lsp.<server>.setup(<stuff...>)                              -- before
--lsp.<server>.setup(coq.lsp_ensure_capabilities(<stuff...>)) -- after

--lsp.pyright.setup(coq.lsp_ensure_capabilities({}))
--lsp.ccls.setup(coq.lsp_ensure_capabilities({}))
vim.lsp.config("ltex", { settings = { ltex = { language = "pl-PL" } } })
vim.lsp.config("rust_analyzer", {
	settings = {
		["rust-analyzer"] = {
			imports = {
				granularity = {
					group = "module",
				},
				prefix = "self",
			},
			cargo = {
				buildScripts = {
					enable = true,
				},
			},
			procMacro = {
				enable = true,
			},
		},
	},
})

require("dapui").setup()
require("catppuccin").setup({ flavour = "macchiato" })
require("colorizer").setup()
require("circles").setup()
vim.cmd.colorscheme("catppuccin")
-- committia_open_only_vim_starting
vim.g.committia_open_only_vim_starting = 1
