-- disable netrw
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- install Lazy
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", -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

-- define plugins
require("lazy").setup({
  "jdhao/whitespace.nvim",
  "lewis6991/gitsigns.nvim",
  "lukas-reineke/indent-blankline.nvim",
  "numToStr/Comment.nvim",
  "NvChad/nvim-colorizer.lua",
  "nvim-lualine/lualine.nvim",
  "nvim-treesitter/nvim-treesitter",
  "tanvirtin/monokai.nvim",
  { "nvim-telescope/telescope.nvim",
    dependencies = { 'nvim-lua/plenary.nvim' }
  },
  { "nvim-neo-tree/neo-tree.nvim",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons",
      "MunifTanjim/nui.nvim"
    },
  },
  {
    "folke/which-key.nvim",
    config = function()
      vim.o.timeout = true
      vim.o.timeoutlen = 300
      require("which-key").setup({
      })
    end,
  },
})

-- start & setup plugins
-- colorscheme has to be first
require("monokai").setup()
require("gitsigns").setup({
  signs = {
    add = { text = "▎" },
    change = { text = "▎" },
    delete = { text = "▎" },
    topdelete = { text = "契" },
    changedelete = { text = "▎" },
    untracked = { text = "▎" },
  },
})
require("indent_blankline").setup()
require("Comment").setup()
require("colorizer").setup()
require("lualine").setup({
  options = {
    component_separators = '',
    icons_enabled = true,
    section_separators = '',
    theme = "molokai",
  },
  sections = {
    lualine_a = { "mode" },
    lualine_b = { { "filename", file_status = true, path = 1 } },
    lualine_c = { "branch" },
    lualine_x = { "filetype" },
    lualine_y = { "progress" },
    lualine_z = { "location" },
  },
  tabline = {
    lualine_a = { "buffers" },
  },
})
require("nvim-treesitter.configs").setup({
  build = ":TSUpdate",
  ensure_installed = { "bash", "python", "terraform", "vim", "yaml" },
  highlight = {
    enable = true,
    use_languagetree = true,
  },
})

require("neo-tree").setup({
  close_if_last_window = false,
  sort_case_insensitive = true,
  window = {
    position = "left",
    auto_expand_width = true,
    -- width = 100,
    mappings = {
      ["s"] = "open_split",
      ["v"] = "open_vsplit",
    },
  },
  nesting_rules = {},
  filesystem = {
    filtered_items = {
      visible = true,
      hide_dotfiles = false,
      hide_gitignored = true,
    },
  },
})

-- modify colorcheme
vim.api.nvim_set_hl(0, "ColorColumn", { bg = "#770000" })
vim.api.nvim_set_hl(0, "DiffChange", { bg = NONE, fg = "#e7c547" })
vim.api.nvim_set_hl(0, "DiffText", { bg = NONE, fg = "#ff8700" })
vim.api.nvim_set_hl(0, "LineNr", { bg = NONE, fg = "#465457" })
vim.api.nvim_set_hl(0, "NonText", { bg = NONE })
vim.api.nvim_set_hl(0, "Normal", { bg = NONE })
vim.api.nvim_set_hl(0, "SignColumn", { bg = NONE })
vim.api.nvim_set_hl(0, "SpellBad", { bg = "#770000", fg = "#ffffff" })
vim.api.nvim_set_hl(0, "Visual", { bg = "#005577", fg = "#ffffff" })

-- global options
vim.opt.list = true
vim.opt.cursorline = true
vim.opt.expandtab = true
vim.opt.gdefault = true
vim.opt.hlsearch = false
vim.opt.ignorecase = true
vim.opt.number = true
vim.opt.shiftround = true
vim.opt.showmode = false
vim.opt.smartcase = true
vim.opt.splitbelow = true
vim.opt.splitright = true
vim.opt.termguicolors = true
vim.opt.writebackup = false
vim.opt.clipboard = ""
vim.opt.mouse = ""
vim.opt.tabstop = 2
vim.opt.scrolloff = 5
vim.opt.updatetime = 100
vim.opt.shiftwidth = 2
vim.opt.showtabline = 2
vim.opt.softtabstop = 2

-- configure mappings
vim.g.mapleader = " "
vim.keymap.set("i", "<C-a>", "<esc>I", { silent = true })
vim.keymap.set("i", "<C-e>", "<esc>A", { silent = true })
vim.keymap.set("n", "<esc><cr>", "<esc><cmd>wq!<cr>", { silent = true })
vim.keymap.set("n", "<leader>/", "<esc><cmd>lua require('Comment.api').toggle.linewise.count(vim.v.count > 0 and vim.v.count or 1)<cr>", { silent = true, desc = "toggle comment" })
vim.keymap.set("n", "<leader>c", '<cmd>execute "set colorcolumn=" . (&colorcolumn == "" ? "80,120" : "")<cr>', { silent = true, desc = "toggle column" })
vim.keymap.set("n", "<leader>fb", require("telescope.builtin").buffers, { desc = "find buffer" })
vim.keymap.set("n", "<leader>ff", require("telescope.builtin").find_files, { desc = "find file" })
vim.keymap.set("n", "<leader>fg", require("telescope.builtin").live_grep, { desc = "find text" })
vim.keymap.set("n", "<leader>h", "<cmd>set hlsearch!<cr>", { silent = true, desc = "toggle hlsearch" })
vim.keymap.set("n", "<leader>j", "<cmd>bprevious<cr>", { silent = true, desc = "previous buffer" })
vim.keymap.set("n", "<leader>k", "<cmd>bnext<cr>", { silent = true, desc = "next buffer" })
vim.keymap.set("n", "<leader>n", "<cmd>set nonumber!<cr><cmd>Gitsigns toggle_signs<cr><cmd>IndentBlanklineToggle<cr>", { silent = true, desc = "toggle decorations" })
vim.keymap.set("n", "<leader>s", "<cmd>set spell!<cr>", { silent = true, desc = "toggle spell" })
vim.keymap.set("n", "<leader>t", "<cmd>Neotree toggle<cr>", { silent = true, desc = "toggle tree" })
vim.keymap.set("n", "<leader>x", "<cmd>bd<cr>", { silent = true, desc = "close buffer" })
vim.keymap.set("n", "<tab>", "<C-w>w", { silent = true })
vim.keymap.set("v", "<leader>/", "<esc><cmd>lua require('Comment.api').toggle.linewise(vim.fn.visualmode())<cr>", { silent = true, desc = "toggle comment" })

-- autocmd
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = {"*.gotmpl"},
  callback = function() vim.bo.filetype = "yaml" end,
})
