require("vim._core.ui2").enable({
	enable = true,
})

vim.opt.cmdheight = 2 -- more space in the neovim command line for displaying messages

vim.pack.add({
	{ src = "https://github.com/rose-pine/neovim" },
	{ src = "https://github.com/brenoprata10/nvim-highlight-colors" },
})

-- Sync background with tmux rose-pine variant
if vim.env.TMUX then
	local variant = vim.fn.system("tmux show-option -gqv @rose_pine_variant"):gsub("%s+", "")
	vim.o.background = variant == "dawn" and "light" or "dark"
else
	vim.o.background = "dark"
end

vim.cmd.colorscheme("rose-pine")

-- Cursor: the default 'guicursor' names no highlight group, so every mode
-- inherits the terminal's cursor colour and the thin insert bar gets lost
-- between characters. Name a group per mode and let Neovim drive the colour
-- (OSC 12, passed through by tmux via the Cs/Cr capabilities).
vim.opt.guicursor = {
	"n-v-c-sm:block-Cursor",
	"i-ci-ve:ver25-CursorInsert-blinkwait300-blinkon500-blinkoff400",
	"r-cr-o:hor20-CursorReplace",
}

local function set_cursor_highlights()
	local palette = require("rose-pine.palette")
	vim.api.nvim_set_hl(0, "Cursor", { fg = palette.base, bg = palette.text })
	vim.api.nvim_set_hl(0, "CursorInsert", { fg = palette.base, bg = palette.rose })
	vim.api.nvim_set_hl(0, "CursorReplace", { fg = palette.base, bg = palette.love })
end

-- Re-applied on ColorScheme so the colours follow the light/dark toggle below.
vim.api.nvim_create_autocmd("ColorScheme", {
	pattern = "rose-pine",
	callback = set_cursor_highlights,
	desc = "Keep the cursor legible against the background in every mode",
})
set_cursor_highlights()

vim.keymap.set("n", "<leader>tt", function()
	vim.o.background = vim.o.background == "dark" and "light" or "dark"
end, { desc = "Alternate between light and dark mode" })

require("nvim-highlight-colors").setup()

vim.pack.add({
	{ src = "https://github.com/nvim-tree/nvim-web-devicons" },
	{ src = "https://github.com/nvim-lualine/lualine.nvim" },
	{ src = "https://github.com/lukas-reineke/indent-blankline.nvim" },
})

require("lualine").setup({
	options = {
		section_separators = "",
		component_separators = "",
		theme = "rose-pine",
	},
	extensions = { "fugitive" },
})

require("ibl").setup()

vim.pack.add({
	{ src = "https://github.com/lewis6991/gitsigns.nvim" },
	{ src = "https://github.com/f-person/git-blame.nvim" },
})

require("gitsigns").setup({
	signs = {
		add = { text = "+" },
		change = { text = "~" },
		delete = { text = "_" },
		topdelete = { text = "‾" },
		changedelete = { text = "~" },
	},
	signs_staged = {
		add = { text = "+" },
		change = { text = "~" },
		delete = { text = "_" },
		topdelete = { text = "‾" },
		changedelete = { text = "~" },
	},
})

vim.keymap.set("n", "<leader>gk", "<cmd>GitBlameToggle<cr>", { desc = "Toggle git-blame" })
vim.g.gitblame_display_virtual_text = false
--	schedule_event = "CursorHold"
--	clear_event = "CursorHoldI"
vim.g.gitblame_delay = 50 -- miliseconds
local git_blame = require("gitblame")
require("lualine").setup({
	sections = {
		lualine_c = {
			{ "filename" },
			{ git_blame.get_current_blame_text, cond = git_blame.is_blame_text_available },
		},
	},
})
