local M = {}

function M.setup()
  -- {{{ LAYA

  local aug = vim.api.nvim_create_augroup('laya', {})

  vim.api.nvim_create_autocmd({ 'BufRead', 'BufNewFile' }, {
    group = aug,
    pattern = '**/jopixel/**/*.php',
    callback = function()
      vim.cmd.iabbrev({ args = { '<buffer>', 'TR', '$this->obtainer()->translator()->t();<Left><Left>' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'TI', '$this->obtainer()->translator()->t()<Left>' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'LOG', '$this->obtainer()->logger()->log();<Left><Left>' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'DBG', "$this->obtainer()->logger()->log('debug', 'debug',);<Left><Left>" } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'PROD', '$this->producer()' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'FILE', '$this->producer()->file()<Left>' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'LINK', "$this->producer()->link($this, '')<Left><Left>" } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'NW', '$this->obtainer()->notifications()->warning();<Left><Left>' } })
      vim.cmd.iabbrev({ args = { '<buffer>', 'NS', '$this->obtainer()->notifications()->success();<Left><Left>' } })
    end,
  })

  -- }}} LAYA

  --- terminal {{{
  local termgroup = vim.api.nvim_create_augroup('terminal', { clear = true })
  vim.api.nvim_create_autocmd('TermOpen', {
    group = termgroup,
    callback = function()
      -- local buf = args.buf
      vim.api.nvim_set_option_value('number', false, { win = 0 })
      vim.api.nvim_set_option_value('signcolumn', 'no', { win = 0 })

      local winid = vim.api.nvim_get_current_win();
      vim.schedule(function()
        if vim.api.nvim_get_current_win() == winid then
          vim.cmd.startinsert()
        end
      end) -- without schedule it lock screen refresh
    end,
  })
  --}}}

  -- Buffer {{{
  vim.api.nvim_create_autocmd({ 'FocusGained', 'TermClose', 'TermLeave' }, { command = 'checktime' })

  -- go to last loc when opening a buffer
  vim.api.nvim_create_autocmd('BufReadPost', {
    callback = function()
      local mark = vim.api.nvim_buf_get_mark(0, '"')
      local lcount = vim.api.nvim_buf_line_count(0)
      if mark[1] > 0 and mark[1] <= lcount then
        pcall(vim.api.nvim_win_set_cursor, 0, mark)
      end
    end,
  })

  -- }}}

  -- qf {{{
  -- local pad = function (str, n)
  --   if #str > n then
  --     return str
  --   else
  --     return str .. string.rep(' ', n - #str)
  --   end
  -- end

  -- QuickfixFunc = function(info)
  --   local items
  --   if info.quickfix then
  --     items = vim.fn.getqflist({ id = info.id, items = 1 }).items
  --   else
  --     items = vim.fn.getloclist(info.id, { items = 1 }).items
  --   end

  --   local res = {};
  --   for i = info.start_idx, info.end_idx, 1 do
  --     local item = items[i]
  --     table.insert(res, pad
  --   end
  -- end

  -- }}}

  -- abbrevs {{{
  vim.cmd.iabbrev('anisble', 'ansible')
  -- }}}
end

return M
