local M = {}

function M.setup()
  vim.g.mapleader = ' '
  vim.g.maplocalleader = ' '
  vim.g.loaded_netrwPlugin = true

  --vim.o.colorcolumn=0
  vim.o.autoindent = true
  vim.o.autoread = true
  vim.o.completeopt = 'menuone,noinsert,noselect'
  vim.o.expandtab = true
  vim.o.fileencodings = 'ucs-bom,utf-8,default,windows-1250,cp852'
  vim.o.foldopen = 'hor,mark,percent,quickfix,search,tag,undo' -- removed block so { and friends don't open fold
  vim.o.foldexpr = 'nvim_treesitter#foldexpr()'
  vim.o.hidden = true
  vim.o.ignorecase = true
  vim.o.inccommand = 'split'
  vim.o.incsearch = true
  vim.o.mouse = ''
  vim.o.path = '**'
  vim.o.ruler = true
  vim.o.scrolloff = 3
  vim.o.shiftwidth = 4
  vim.o.showcmd = true
  vim.o.sidescrolloff = 5
  vim.o.smartcase = true
  vim.o.smartindent = true
  vim.o.softtabstop = 4
  vim.o.swapfile = true
  vim.o.tabstop = 4
  vim.o.undofile = true
  vim.o.splitbelow = true
  vim.o.splitright = true
  --vim.o.clipboard = "unnamedplus"
  vim.o.guifont = 'Hack:12h'

  vim.o.listchars = 'tab:❮⋯❯,trail:⸬,nbsp:·'

  vim.o.grepprg = 'rg --vimgrep --no-ignore --smart-case $*'
  vim.o.grepformat = '%f:%l:%c:%m'

  vim.o.linebreak = true
  vim.o.breakindent = true
  vim.o.breakindentopt = 'shift:4'
  vim.o.showbreak = '…'

  local isfname = {}
  for _, ch in ipairs(vim.opt.isfname:get()) do
    if ch ~= "=" then
      table.insert(isfname, ch)
    end
  end
  vim.opt.isfname = isfname

  local function set_winopts()
    vim.o.cursorline = true
    vim.o.foldcolumn = 'auto:1'
    vim.o.foldenable = true
    vim.o.foldmethod = 'marker'
    vim.o.number = true
    vim.o.signcolumn = 'yes'
    vim.o.relativenumber = false
    vim.o.list = false
    vim.o.statusline = ''
      .. ' %{fnamemodify(getcwd(),":t")}'         -- basename of cwd
      .. '%( │  %{get(b:,"gitsigns_head","")}%)'  -- branch
      .. ' │  %<%f%( [%M%R%W]%)'                  -- filename and modification flags
      .. '%( [%{&ff!="unix" ? &ff:""}]%)'          -- show non standard line endings
      .. '%='                                      -- rest is right
      .. '%( [%{get(b:,"attached_lsps","")}] | %)' -- lsp
      .. '%y '                                     -- filetype
      .. '%-7.(%3.l:%-3.(%c%V%)%) %P'              -- ruler

    vim.o.statuscolumn = '%l %C%s'
  end
  set_winopts()

  -- vim.api.nvim_create_autocmd({ 'BufEnter', 'BufWinEnter', 'FileType' }, {
  --   pattern = '*',
  --   callback = function(args)
  --     local buf = args.buf
  --     local buftype = vim.bo[buf].buftype
  --     if buftype == 'prompt' then
  --       return -- plugins handle this usually themselves
  --     end
  --     set_winopts()
  --     if buftype == 'nofile' or buftype == 'help' then
  --       vim.o.list = false
  --       vim.bo[buf].buflisted = false
  --       vim.wo.number = false
  --       vim.wo.signcolumn = 'auto'
  --       -- vim.wo.foldcolumn = 0
  --       vim.wo.statuscolumn = ''
  --       vim.keymap.set('n', 'q', '<Cmd>:q<CR>', { buffer = buf })
  --       if vim.bo[buf].filetype ~= 'qf' then
  --         vim.wo.statusline = '%f'
  --       end
  --     end
  --   end,
  -- })
  --

  vim.api.nvim_create_autocmd('FileType', {
    pattern = { 'qf', 'man', 'help', 'tsplayground', 'checkhealth', 'Overseer*' },
    callback = function(args)
      local buf = args.buf
      local bo = vim.bo[buf]
      if bo.filetype == 'help' and bo.buftype ~= 'help' then
        return
      end
      bo.buflisted = false
      vim.opt_local.number = false
      vim.opt_local.signcolumn = 'no'
      vim.keymap.set('n', 'q', '<Cmd>:q<CR>', { buffer = buf })

      if bo.filetype ~= 'qf' then
        vim.opt_local.statusline = '%Y | %f%=%l:%v %P'
      end

      vim.o.statuscolumn = ''
    end,
  })

  vim.diagnostic.config({
    virtual_text = false
    -- virtual_text = {
    --   format = function(diag)
    --     local s = vim.diagnostic.severity
    --     if diag.severity == s.E or diag.severity == s.W then
    --       return diag.message
    --     end
    --     return nil
    --   end,
    -- },
  })

  if vim.g.neoray then -- {{{
    vim.cmd.NeoraySet('CursorAnimTime', 0)
    vim.cmd.NeoraySet('Transparency', '0.95')
    vim.cmd.NeoraySet('TargetTPS', 120)
    vim.cmd.NeoraySet('ContextMenuOn', false)
    vim.cmd.NeoraySet('BoxDrawingOn', true)
    vim.cmd.NeoraySet('WindowSize', '100x40')
    vim.cmd.NeoraySet('WindowState', 'none')
    vim.cmd.NeoraySet('KeyZoomIn', '<>')
    vim.cmd.NeoraySet('KeyZoomOut', '<>')
  end -- }}}
end

function M.reset(winids)
  local api = vim.api
  if winids == nil then
    winids = api.nvim_tabpage_list_wins(0)
  end

  if type(winids) == number then
    winids = { winids }
  end

  if type(winids) ~= 'table' then
    vim.notify('reset winopts has to be called with wither nil, table or number', vim.log.levels.WARN)
    return
  end

  for _, win in pairs(winids) do
    local buf = api.nvim_win_get_buf(win)
    local ft = api.nvim_get_option_value('filetype', { buf = buf})
  end
end

return M
