vim.o.termguicolors = true

vim.g.sonokai_transparent_background = true
vim.g.sonokai_disable_terminal_colors = true

vim.o.laststatus = 3

-- neovim has a NormalNC highlight group that applies to inactive windows but
-- we want to apply different styles to *active* windows. The Normal highlight
-- group is special and setting a background there applies to everything and
-- cannot be "unset" from other groups like NormalNC.
--
-- What we do here is create a new highlight namespace that we apply to active
-- windows only. We have to copy all the original highlight groups and then
-- apply our changes. We then change the window's highlight namespace on
-- WinEnter/WinLeave.
local active_ns = vim.api.nvim_create_namespace('active')

function init_active_ns()
  local hls = vim.api.nvim_get_hl(0, {})
  for k,v in ipairs(hls) do
    vim.api.nvim_set_hl(active_ns, k, v)
  end

  local normal = vim.api.nvim_get_hl(0, { name = 'Normal' })
  normal.ctermbg = 235
  normal.bg = '#2c2c2c'

  vim.api.nvim_set_hl(active_ns, 'Normal', normal)

  vim.api.nvim_win_set_hl_ns(0, active_ns)
end

vim.api.nvim_create_autocmd('WinEnter', {
  callback = function()
    vim.api.nvim_win_set_hl_ns(0, active_ns)
  end
})

vim.api.nvim_create_autocmd('WinLeave', {
  callback = function()
    vim.api.nvim_win_set_hl_ns(0, 0)
  end
})

vim.api.nvim_create_autocmd('ColorScheme', {
  pattern = 'sonokai',
  callback = function()
    vim.api.nvim_set_hl(0, 'MiniIndentscopeSymbol', { link = 'Whitespace' })

    local configuration = vim.fn['sonokai#get_configuration']()
    local palette = vim.fn['sonokai#get_palette'](configuration.style, {[vim.type_idx]=vim.types.dictionary})

    vim.fn['sonokai#highlight']('MiniStatuslineModeNormal', palette.black, palette.blue)
    vim.fn['sonokai#highlight']('MiniStatuslineModeCommand', palette.black, palette.yellow)
    vim.fn['sonokai#highlight']('MiniStatuslineModeInsert', palette.black, palette.green)
    vim.fn['sonokai#highlight']('MiniStatuslineModeVisual', palette.black, palette.orange)
    vim.fn['sonokai#highlight']('MiniStatuslineModeReplace', palette.black, palette.red)
    vim.fn['sonokai#highlight']('MiniStatuslineModeOther', palette.black, palette.green)

    init_active_ns()
  end,
})

vim.cmd('colorscheme sonokai')