local cmp = require('cmp')
-- local luasnip = require("luasnip")

local has_words_before = function()
  unpack = unpack or table.unpack
  local line, col = unpack(vim.api.nvim_win_get_cursor(0))
  return col ~= 0 and vim.api.nvim_buf_get_lines(0, line - 1, line, true)[1]:sub(col, col):match("%s") == nil
end

local next_comp = function(fallback)
  if cmp.visible() then
    cmp.select_next_item()
  -- elseif luasnip.expand_or_jumpable() then
  --   luasnip.expand_or_jump()
  elseif has_words_before() then
    cmp.complete()
  else
    fallback()
  end
end

local prev_comp = function(fallback)
  if cmp.visible() then
    cmp.select_prev_item()
  -- elseif luasnip.jumpable(-1) then
  --   luasnip.jump(-1)
  else
    fallback()
  end
end

local icons = {
    Text = '',
    Method = '',
    Function = '',
    Constructor = '',
    Field = 'ﰠ',
    Variable = '',
    Class = 'ﴯ',
    Interface = '',
    Module = '',
    Property = 'ﰠ',
    Unit = '塞',
    Value = '',
    Enum = '',
    Keyword = '',
    Snippet = '',
    Color = '',
    File = '',
    Reference = '',
    Folder = '',
    EnumMember = '',
    Constant = '',
    Struct = 'פּ',
    Event = '',
    Operator = '',
    TypeParameter = '',
}

-- fzf match
vim.cmd('highlight! CmpItemAbbrMatch guibg=NONE guifg=#fc99cd')
vim.cmd('highlight! link CmpItemAbbrMatchFuzzy CmpItemAbbrMatch')
-- Variable
vim.cmd('highlight! CmpItemKindVariable guibg=NONE guifg=#9CDCFE')
vim.cmd('highlight! link CmpItemKindInterface CmpItemKindVariable')
vim.cmd('highlight! link CmpItemKindText CmpItemKindVariable')
-- Functions
vim.cmd('highlight! CmpItemKindFunction guibg=NONE guifg=#C586C0')
vim.cmd('highlight! link CmpItemKindMethod CmpItemKindFunction')
-- Keyword
vim.cmd('highlight! CmpItemKindKeyword guibg=NONE guifg=#D4D4D4')
vim.cmd('highlight! link CmpItemKindProperty CmpItemKindKeyword')
vim.cmd('highlight! link CmpItemKindUnit CmpItemKindKeyword')

cmp.setup({
    mapping = cmp.mapping.preset.insert({
      ['<C-e>'] = cmp.config.disable,
      ['<C-d>'] = cmp.mapping.scroll_docs(-4),
      ['<C-f>'] = cmp.mapping.scroll_docs(4),
      ['<CR>'] = cmp.mapping.confirm({ select = true }),
      ['<C-Space>'] = cmp.mapping.complete(),
      --['<Tab>'] = cmp.mapping(next_comp, { "i", "s" }),
      --['<S-Tab>'] = cmp.mapping(prev_comp, { "i", "s" }),
      ['<M-j>'] = cmp.mapping(next_comp, { "i", "s" }),
      ['<M-k>'] = cmp.mapping(prev_comp, { "i", "s" }),
    }),
    sources = cmp.config.sources({
        { name = 'nvim_lsp', max_item_count = 10 },
        -- { name = 'luasnip', max_item_count = 10 },
        { name = 'path', max_item_count = 10 },
        { name = 'buffer', max_item_count = 10 },
    }),
    snippet = {
        expand = function(args)
            require('luasnip').lsp_expand(args.body)
        end,
    },
    formatting = {
        format = function(entry, item)
            -- Kind icons
            item.kind = string.format('%s %s', icons[item.kind], item.kind)
            return item
        end,
    },
})
