local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
    vim.fn.system({
        "git",
        "clone",
        "--filter=blob:none",
        "https://github.com/folke/lazy.nvim.git",
        "--branch=stable",
        lazypath,
    })
end
vim.opt.rtp:prepend(lazypath)

require("lazy").setup({
    { "Shatur/neovim-ayu", lazy = false, enabled = true },
    "junegunn/fzf.vim",
    "yggdroot/indentline",
    "yuezk/vim-js",
    { "neoclide/coc.nvim", branch = "release" },
    { 
        "nvim-lualine/lualine.nvim",
        dependencies = {
           "nvim-tree/nvim-web-devicons"      
        }
    },
    { 
        "nvim-telescope/telescope.nvim", 
        branch = "0.1.x",
        dependencies = {
            "nvim-lua/plenary.nvim",
            "jremmen/vim-ripgrep",
            { "nvim-treesitter/nvim-treesitter", build = ":TSUpdate" },
           "nvim-tree/nvim-web-devicons"      
        }
    },
    "tpope/vim-fugitive",
    { "rrethy/vim-hexokinase", build = "make hexokinase" },
    "mbbill/undotree",
    "skanehira/docker-compose.vim",
    "chr4/nginx.vim",
    "machakann/vim-highlightedyank",
    "godlygeek/tabular",
    "preservim/vim-markdown",
    "tpope/vim-obsession",
    {
        "akinsho/bufferline.nvim",
        dependencies = {
            "nvim-tree/nvim-web-devicons"
        }
    }
})

--- THEMING ---

vim.opt.termguicolors = true

require('ayu').setup({
    mirage = true,
    overrides = function()
        if vim.o.background == "dark" then
            return { 
                Normal = {
                    bg = '#0f1419', 
                    fg = '#cbccc6'
                },
                Comment = { fg = "#5c6773" }
            }
        end
    end
})

require('ayu').colorscheme()

-- Change pane split styling
vim.api.nvim_set_hl(0, "VertSplit", { link = "Conceal"})

--- Change default behaviour ---
-- Disable integrated providers
vim.g.loaded_ruby_provider = 0
vim.g.loaded_perl_provider = 0
vim.g.loaded_node_provider = 0

-- Disable error bell
vim.opt.errorbells = false

-- Hide emptyline ~
vim.opt.fillchars = { eob = " " }

--- Tab configuration ---
-- Show tabs at 4 spaces
vim.opt.tabstop = 4
vim.opt.softtabstop = 4

-- When indenting with '>', use 4 spaces
vim.opt.shiftwidth = 4

-- Insert 4 spaces instead of tab
vim.opt.expandtab = true

--- Line formatting ---
vim.opt.nu = true
vim.opt.wrap = false

--- Search configuration ---
vim.opt.smartcase = true
vim.opt.ignorecase = true

--- Window configuration ---
vim.opt.splitright = true
vim.opt.splitbelow = true
-- Remove duplicate -- INSERT --
vim.opt.showmode = false

--- Operation configuration ---
vim.opt.swapfile = false
vim.opt.backup = false
vim.opt.undodir = vim.fn.expand('~').."/.config/nvim/undodir"
vim.opt.undofile = true

--- Folding behaviour configuration ---
vim.opt.foldmethod = "indent"
vim.opt.foldnestmax = 10
vim.opt.foldenable = false
vim.opt.foldlevel = 99
vim.opt.signcolumn = "no"

--- Miscellaneous ---
-- rg smart root finder and adds git ignore loading for faster execution
if vim.fn.executable("rg") == 1 then
    vim.g.rg_derive_root = true
end

-- Disable mouse
vim.opt.mouse = nil

-- Source CoC configuration
vim.cmd("source ~/.config/nvim/coc.vim")

-- IndentLine configuration
vim.g.indentLine_enabled = 1
vim.g.indentLine_char_list = {'|', '¦', '┆', '┊'}
vim.g.indentLine_setColors = 1
vim.g.indentLine_color_gui = "#5C6370"
vim.g.indentLine_showFirstIndentLevel = 1

-- Advertise 256 colors capability (for tmux)
vim.t_Co = 256

-- Undocumented ???
--vim.&t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
--vim.&t_8b = "\<Esc>[48;2;%lu;%lu;%lum"

--- Lualine ---
-- Setup
local custom_ayu_dark = require("lualine.themes.ayu_dark")

custom_ayu_dark.normal.c.bg = "#080b0d"

require("lualine").setup({
    options = {
        theme = custom_ayu_dark,
        section_separators = { left = '', right = '' },
        component_separators = { left = '', right = '' },
        globalstatus = true,
    },
    sections = {
        lualine_a = {'mode'},
        lualine_b = {'branch', 'diff', 'diagnostics'},
        lualine_c = {'filename'},
        lualine_x = {'encoding', 'fileformat', 'filetype'},
        lualine_y = {},
        lualine_z = {'location'}
    },
})

--- Bufferline (tabs) ---
-- Setup
require("bufferline").setup({
    options = {
        mode = "tabs",
        numbers = "ordinal",
        indicator = {
            style = "icon"
        },
        color_icons = true,
        show_close_icon = false,
        show_buffer_close_icons = false,
        show_buffer_default_icon = false,
        show_duplicate_prefix = false,
        hover = {
            enabled = false
        },
    },
    highlights = {
        buffer_selected = {
            fg = "fg",
            bg = "bg",
            italic = false,
        },
        numbers_selected = {
            fg = "bg",
            bg = "fg"
        },
        indicator_selected = {
            fg = {
                attribute = "fg",
                highlight = "Identifier"
            }
        },
    }
})

--- Treesitter ---
require("nvim-treesitter.configs").setup {
    highlight = {
        enable = true
    }
}

--- Telescope ---
require("telescope-config")
require("telescope-mappings")

require("keybindings")
