-- Load plugins
-- Bootstrap packer if needed {{
local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
   fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})
   vim.cmd 'packadd packer.nvim'
end
-- }}

local use = require('packer').use

return require('packer').startup({function()
   -- TODO: Find a better way to organize this

   -- impatient (needs to go early)
   use { -- lewis6991: impatient: Make nvim load faster by caching them
      'lewis6991/impatient.nvim',
      config = function()
         require('impatient')
      end,
   }

   -- Packer
   use { -- packer
      'wbthomason/packer.nvim',
      config = function()
         -- Map :PS to :PackerSync
         vim.cmd 'command! PS PackerSync'
      end
   }

   -- Colors (decorations)
   use { -- treesitter: syntax highlighting and more
      'nvim-treesitter/nvim-treesitter', run = ':TSUpdate',
      config = function()
         require('blake.lsp').treesitter()
      end,
   }
   use { -- onedark theme
      'navarasu/onedark.nvim',
      config = function()
         -- vim.g.onedark_transparent_background = true
         vim.cmd [[
            "au ColorScheme onedark hi TabLine gui=none guibg='#282C34' guifg='#5C6370'  " all tabs color
            au ColorScheme onedark hi TabLineSel       guibg='#282C34' guifg='#B5BBC7'  " Highlighted tab color
            "au ColorScheme onedark hi TabLineFill      guibg='#282C34'                  " tabline portion without tabs (right-hand side)
         ]]
      end
   }
   use { -- tokyonight theme
      'folke/tokyonight.nvim',
      config = function()
         -- vim.g.onedark_transparent_background = true
         vim.cmd [[
            "au ColorScheme tokyonight hi TabLine gui=none guibg='#24283B' guifg='#5C6370'  " all tabs color
            au ColorScheme tokyonight hi TabLineSel       guibg='#24283B' guifg='#C0CAF5'  " Highlighted tab color
            "au ColorScheme tokyonight hi TabLineFill      guibg='#24283B'                  " tabline portion without tabs (right-hand side)
         ]]
      end,
   }
   use { -- newspring: Good light theme for writing
      'NLKNguyen/papercolor-theme',
   }
   use { -- statusline: lualine
      'nvim-lualine/lualine.nvim',
      requires = {'kyazdani42/nvim-web-devicons', opt = true},
      config = function()
         require('blake.other').lualine()
      end,
   }
   use { -- nvim-tabline
      'seblj/nvim-tabline',
      requires = 'kyazdani42/nvim-web-devicons',
      config = function()
         require('tabline').setup {
            -- Disable icons (and clickable buttons)
            modified_icon = '',
            close_icon = '',
         }
      end
   }
   use { -- css color tag highlighter (ex: #FFB13B)
      'norcalli/nvim-colorizer.lua',
      config = function()
         vim.cmd 'command! COL ColorizerToggle'
      end,
   }

   -- IDE features
   ---- LSP
   use { -- lspinstall + lspconfig
      'williamboman/nvim-lsp-installer',
      { -- lspconfig
         'neovim/nvim-lspconfig',
         config = function()
            require('blake.lsp').lspinstall()
            require('blake.lsp').lspconfig()
         end
      }
   }
   use { -- commentstring based on context from treesitter
      'JoosepAlviste/nvim-ts-context-commentstring',
      requires = 'nvim-treesitter/nvim-treesitter',
      config = function()
         require'nvim-treesitter.configs'.setup {
            context_commentstring = {
               enable = true,
               enable_autocmd = false,
            },
         }
      end,
   }
   use { -- compe
      'hrsh7th/nvim-cmp',
      config = function()
         require('blake.lsp').cmp()
      end,
      requires = { -- (mostly) nvim-cmp sources (not required by cmp, but reqired by my configuration)
         'onsails/lspkind-nvim', -- icons for entries of the completion menu
         'hrsh7th/cmp-path',
         'hrsh7th/cmp-nvim-lua',
         'hrsh7th/cmp-nvim-lsp',
         'saadparwaiz1/cmp_luasnip',
         'ray-x/cmp-treesitter',
         'hrsh7th/cmp-calc',
         'f3fora/cmp-spell',
         'hrsh7th/cmp-buffer',
         'hrsh7th/cmp-cmdline',
         'hrsh7th/cmp-emoji',
      },
   }
   use { -- luasnip (snippits)
      'L3MON4D3/LuaSnip',
   }
   use { -- function parameter previews
      'ray-x/lsp_signature.nvim',
      after = 'nvim-lspconfig',
      config = function()
         require('blake.lsp').signature()
      end,
   }
   use { -- symbols-outline: treesitter-based document outline (:SO)
      'simrat39/symbols-outline.nvim',
      config = function()
         vim.cmd 'command! SO SymbolsOutline'
         vim.cmd 'nnoremap <Leader>so <cmd>SymbolsOutline<CR>'
      end,
   }
   -- use { -- ALE: Support for lots of linters, etc
   --    'dense-analysis/ale',
   --    ft = {'sh', 'zsh', 'zshrc', 'bashrc', 'bash'},
   --    config = function()
   --       vim.g.ale_disable_lsp = 1
   --    end
   -- }
   ---- DAP (Debug Adapter Protocol)
   use { -- nvim-dap: DAP support
      'mfussenegger/nvim-dap',
      config = function()
         require('blake.dap').nvim_dap()
      end
   }
   -- use { -- DAP adapter installer
   --    'Pocco81/dap-buddy.nvim',
   --    config = function()
   --       require('blake.dap').dap_buddy()
   --    end
   -- }
   use { -- virtual-text: Print Variable names while debugging
      'theHamsta/nvim-dap-virtual-text',
      config = function()
         require("nvim-dap-virtual-text").setup()
      end
   }
   use { -- a UI for nvim-dap (easy access to info)
      'rcarriga/nvim-dap-ui',
      config = function()
         require('blake.dap').dap_ui()
      end,
   }

   ---- Other IDE features
   use { -- telescope: fuzzy finder for finding fuzzy things
      'nvim-telescope/telescope.nvim',
      requires = { {'nvim-lua/plenary.nvim'} },
      config = function()
         require('blake.other').telescope()
      end
   }
   use { -- git integration
      'lewis6991/gitsigns.nvim',
      requires = {
         'nvim-lua/plenary.nvim'
      },
      config = function()
         require('blake.other').gitsigns()
      end,
   }
   -- use { -- file manager
   --    'kyazdani42/nvim-tree.lua',
   --    requires = 'kyazdani42/nvim-web-devicons',
   --    config = function()
   --       require('blake.other').nvimtree()
   --    end
   -- }
   use { -- terminal
      'akinsho/toggleterm.nvim',
      tag = 'v1.*',
      config = function()
         require('blake.other').toggleterm()
      end
   }
   use { -- Smooth Scrolling
      'karb94/neoscroll.nvim',
      config = function()
         require('blake.other').neoscroll()
      end,
   }
   use { -- automatic session management
      'rmagatti/auto-session',
      config = function()
         require('blake.other').autosession()
      end
   }
   use { -- Markdown preview
      'ellisonleao/glow.nvim',
      ft = { 'md', 'markdown', }
   }
   use { -- Zen mode (:ZenMode or :ZM)
      "folke/zen-mode.nvim",
      config = function()
         require('blake.other').zenmode()
         vim.cmd 'nnoremap <F10> <cmd>ZenMode<CR>'
         vim.cmd 'inoremap <F10> <cmd>ZenMode<CR>'
      end,
   }
   -- use { -- dim text outside paragraph or function
   --    'folke/twilight.nvim',
   --    config = function()
   --       vim.cmd 'command! TW Twilight'
   --       require("twilight").setup()
   --    end,
   -- }
   use { -- matchup: Use the percent (%) key for more things
      'andymass/vim-matchup',
   }
   -- use { -- lightspeed: Jump around in files quickly
   --    'ggandor/lightspeed.nvim',
   -- }
   use { -- indentline: Line indent indicators
      'lukas-reineke/indent-blankline.nvim',
      config = function()
         require('blake.other').indent_blankline()
      end
   }
   -- use { -- targets.vim: fix ci' and other things to work right
   --    'wellle/targets.vim',
   -- }

   -- Conveniences
   use { -- Ghost: Web browser integration
      'raghur/vim-ghost',
      opt = true,
      cmd = {'GhostStart', 'GhostInstall', 'GhostStop', 'GhostSync', 'GhostToggleSync',},
   }
   use { -- Undo tree
      'mbbill/undotree',
      config = function()
         vim.cmd 'nnoremap <F5> <cmd>UndotreeToggle<CR>'
         vim.api.nvim_set_keymap('n', '<F5>', '<cmd>UndotreeToggle<CR>', { noremap = true, silent = true, })
      end
   }
  --  use { -- Dim inactive window
  --    'sunjon/shade.nvim',
  --    config = function()
  --       require'shade'.setup({
  --          overlay_opacity = 50,
  --          opacity_step = 1,
  --          keys = {
  --             brightness_up    = '<C-Up>',
  --             brightness_down  = '<C-Down>',
  --             toggle           = '<Leader>s',
  --          }
  --       })
  --    end
  -- }
   use { -- Quote pairing
      'windwp/nvim-autopairs',
      config = function()
         require('nvim-autopairs').setup()
      end
   }
   use { -- accelerated jk movement
      'rhysd/accelerated-jk'
   }
   use { -- cheat.sh integration
      'dbeniamine/cheat.sh-vim',
   }
   use { -- nvim-align: Align text
      'RRethy/nvim-align'
   }
   use { -- Comments (gb and gc)
       'numToStr/Comment.nvim',
       config = function()
          require('Comment').setup {
            pre_hook = function(ctx)
               -- Only calculate commentstring for tsx filetypes
               if vim.bo.filetype == 'typescriptreact' then
                  local U = require('Comment.utils')
                  -- Detemine whether to use linewise or blockwise commentstring
                  local type = ctx.ctype == U.ctype.line and '__default' or '__multiline'
                  -- Determine the location where to calculate commentstring from
                  local location = nil
                  if ctx.ctype == U.ctype.block then
                     location = require('ts_context_commentstring.utils').get_cursor_location()
                  elseif ctx.cmotion == U.cmotion.v or ctx.cmotion == U.cmotion.V then
                     location = require('ts_context_commentstring.utils').get_visual_start_location()
                  end
                  return require('ts_context_commentstring.internal').calculate_commentstring({
                     key = type,
                     location = location,
                  })
               end
            end,
          }
       end
    }
   use { -- rhysd: committia: better commit editing window
      'rhysd/committia.vim',
   }
   use { -- rhysd: conflict-marker: mark git conflicts - [x ]x
      'rhysd/conflict-marker.vim',
   }
   use { -- lewis6991: spellsitter: Spell checking in treesitter files
      'lewis6991/spellsitter.nvim',
      config = function()
         require('spellsitter').setup()
      end,
   }
   use { -- lewis6991: spaceless: Strip trailing whitespace as you are editing
      'lewis6991/spaceless.nvim',
      config = function()
         require'spaceless'.setup()
      end,
   }
   use { -- lewis6991: foldsigns: signcolumn signs on folded lines
      'lewis6991/foldsigns.nvim',
      config = function()
         require('foldsigns').setup()
      end,
   }
   use { -- lewis6991: cleanfold: folds have never looked better
      'lewis6991/cleanfold.nvim',
      config = function()
         require('cleanfold').setup()
      end,
   }
   use { -- lewis6991: sattelite: small scroll bar
      'lewis6991/satellite.nvim',
      config = function()
         require('satellite').setup()
         vim.cmd [[
            nnoremap <C-l> <cmd>nohlsearch\|diffupdate\|SatelliteRefresh<CR>
            inoremap <C-l> <cmd>nohlsearch\|diffupdate\|SatelliteRefresh<CR>
         ]]
      end,
   }
   use { -- tpope: surround
      'tpope/vim-surround'
   }
   use { -- tpope: git integration
      'tpope/vim-fugitive'
   }
   use { -- tpope: Repeatability for various tpope plugins
      'tpope/vim-repeat',
   }
   use { -- tpope: automatically get info about files and apply it to vim (tabs vs spaces, etc)
      'tpope/vim-sleuth',
   }


   -- Bootstrap packer if needed {
   if (packer_bootstrap) then
      require('packer').sync()
   end -- }
end,
config = {
  display = { open_fn = require('packer.util').float },
  -- Move to lua dir so impatient.nvim can cache it
  compile_path = vim.fn.stdpath('config')..'/lua/packer_compiled.lua',
}})

-- vim:fdm=marker:fmr={,}:fdl=1:expandtab:tabstop=3:sw=3