--options--
vim.opt.termguicolors = true

vim.opt.relativenumber = true
vim.opt.number = true

vim.opt.cursorline = true
vim.opt.wrap = false

vim.opt.scrolloff = 8
vim.opt.autoindent = false
vim.opt.tabstop = 8

local transparent_background = false

require("ayu").setup({
    mirage = false,
    overrides = transparent_background and {
        Normal = { bg = "none" },
        NormalNC = { bg = "none" },
        NormalFloat = { bg = "none" },
        SignColumn = { bg = "none" },
        VertSplit = { bg = "none" },
        StatusLine = { bg = "none" },
        LineNr = { bg = "none" },
        WinSeparator = { bg = "none" },
    } or {},
})

--vim.cmd("colorscheme vhs")
vim.cmd("colorscheme ayu")

require("lualine").setup({
    options = {
        theme = "ayu_dark",
        section_separators = "",
        component_separators = "|",
        globalstatus = true,
        disabled_filetypes = { "NvimTree", "packer" },
    },
    sections = {
        lualine_a = { "mode" },
        lualine_b = { "branch" },
        lualine_c = { "filename" },
        lualine_x = { "filetype" },
        lualine_y = { "progress" },
        lualine_z = { "location" },
    },
})


--bindings--
vim.g.mapleader = " "

local builtin = require('telescope.builtin')

vim.keymap.set('n', '<leader>ff', builtin.find_files , { desc = 'Telescope find files' })
vim.keymap.set('n', '<leader>fg', builtin.live_grep , { desc = 'Telescope live grep' })
vim.keymap.set('n', '<leader>fb', builtin.buffers , { desc = 'Telescope buffers' })
vim.keymap.set('n', '<leader>fh', builtin.help_tags , { desc = 'Telescope help tags' })
vim.keymap.set('n', '<leader>fd', function()
	builtin.file_browser({ path = "%:p:h", select_buffer = true})
end, { desc = 'File browser' })


require'nvim-treesitter.configs'.setup {
	ensure_installed = {"c", "vim", "vimdoc", "lua", "markdown", "css", "html", "javascript"},
	sync_install = false,
	auto_install = false,


	highlight = {
		enable = true,
		disable = function (lang, buf)
			local max_filesize = 100 * 1024
			local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
			if ok and stats and stats.size > max_filesize then
				return true
			end
		end
	},
	additional_vim_regex_highlighting = false,
}

local function makefile_utils()
	local pickers = require('telescope.pickers')
	local finders = require('telescope.finders')
	local conf = require('telescope.config').values
	local actions = require('telescope.actions')
	local action_state = require('telescope.actions.state')

	local make_targets = vim.fn.systemlist("awk -F':' '/^[a-zA-Z0-9][^$#\\t=]*:([^=]|$)/ {print $1}' Makefile | sort -u")

	pickers.new({}, {
		prompt_title = "Make Targets",
		finder = finders.new_table { result = make_targets },
		sorter = conf.generic_sorter({}),
		atatch_mappings = function(prompt_bufnr)
			actions.select_default:replace(function()
				local selection = action_state.get_selected_entry()
				actions.close(prompt_bufnr)
				vim.cmd("split | terminal make" .. selection [1])
			end)
		return true
		end
	}):find()
end

vim.keymap.set('n', '<leader>fm', makefile_utils, { desc = 'Makefile targets' })
vim.keymap.set('n', '<leader>s', ':w<CR>', { desc = 'Save file' })

