local wezterm = require 'wezterm'
local config = {}

if wezterm.config_builder then
  config = wezterm.config_builder()
end

local HOME = '~'

-- config.color_scheme = 'Catpuccin Mocha'
config.font = wezterm.font 'JetBrains Mono'

config.window_padding = {
  left = '0.5cell',
  right = '0.5cell',
  top = '0',
  bottom = '0',
}

config.use_fancy_tab_bar = false
config.show_new_tab_button_in_tab_bar = false
-- config.adjust_window_size_when_changing_font_size = false

config.inactive_pane_hsb = {
  saturation = 0.7,
  brightness = 0.6,
}

config.scrollback_lines = 5000
config.default_cwd = HOME

function tab_title(tab_info)
  local title = tab_info.tab_title
  -- if the tab title is explicitly set, take that
  if title and #title > 0 then
    return title
  end
  -- Otherwise, use the title from the active pane
  -- in that tab
  return tab_info.active_pane.title
end

wezterm.on('format-tab-title', function(tab, tabs, panes, config, hover, max_width)
  local title = tab_title(tab)
  if tab.is_active then
    return {
      { Background = { Color = 'blue' } },
      { Text = ' ' .. title .. ' ' },
    }
  end
  return title
end)

wezterm.on('update-right-status', function(window, pane)
  local cells = {}

  -- Figure out the cwd and host of the current pane.
  -- This will pick up the hostname for the remote host if your
  -- shell is using OSC 7 on the remote host.
  local cwd_uri = pane:get_current_working_dir()
  if cwd_uri then
    local cwd = ''

    if type(cwd_uri) == 'userdata' then
      -- Running on a newer version of wezterm and we have
      -- a URL object here, making this simple!

      cwd = cwd_uri.file_path
    else
      -- an older version of wezterm, 20230712-072601-f4abf8fd or earlier,
      -- which doesn't have the Url object
      cwd_uri = cwd_uri:sub(8)
      local slash = cwd_uri:find '/'
      if slash then
        -- and extract the cwd from the uri, decoding %-encoding
        cwd = cwd_uri:sub(slash):gsub('%%(%x%x)', function(hex)
          return string.char(tonumber(hex, 16))
        end)
      end
    end
    table.insert(cells, cwd)
  end

  -- An entry for each battery (typically 0 or 1 battery)
  for _, b in ipairs(wezterm.battery_info()) do
    table.insert(cells, string.format('%.0f%%', b.state_of_charge * 100))
  end

  local date = wezterm.strftime '%F %R'
  table.insert(cells, date)

  -- The powerline < symbol
  local LEFT_ARROW = utf8.char(0xe0b3)
  -- The filled in variant of the < symbol
  local SOLID_LEFT_ARROW = utf8.char(0xe0b2)

  -- Color palette for the backgrounds of each cell
  local colors = {
    '#3c1361',
    '#52307c',
    '#663a82',
    '#7c5295',
    '#b491c8',
  }

  -- Foreground color for the text across the fade
  local text_fg = '#c0c0c0'

  -- The elements to be formatted
  local elements = {}
  -- How many cells have been formatted
  local num_cells = 0

  -- Translate a cell into elements
  function push(text, is_last)
    local cell_no = num_cells + 1
    table.insert(elements, { Foreground = { Color = text_fg } })
    table.insert(elements, { Background = { Color = colors[cell_no] } })
    table.insert(elements, { Text = ' ' .. text .. ' ' })
    if not is_last then
      table.insert(elements, { Foreground = { Color = colors[cell_no + 1] } })
      table.insert(elements, { Text = SOLID_LEFT_ARROW })
    end
    num_cells = num_cells + 1
  end

  while #cells > 0 do
    local cell = table.remove(cells, 1)
    push(cell, #cells == 0)
  end

  window:set_right_status(wezterm.format(elements))
end)

config.use_dead_keys = false
config.disable_default_key_bindings = true

config.leader = { key = 'a', mods = 'ALT' }

require('wez-tmux').apply_to_config(config, {})
-- config.keys = {
--   {
--     mods = 'LEADER|SHIFT',
--     key = 'R',
--     action = wezterm.action.ReloadConfiguration,
--   },
--   {
--     mods = 'LEADER',
--     key = 'P',
--     action = wezterm.action.ActivateCommandPalette,
--   },
--   {
--     mods = 'LEADER',
--     key = '}',
--     action = wezterm.action.ActivateCopyMode,
--   },
--   {
--     mods = 'LEADER',
--     key = '{',
--     action = wezterm.action.QuickSelect,
--   },
--   {
--     mods = 'LEADER',
--     key = 'q',
--     action = wezterm.action.ShowDebugOverlay,
--   },
--   {
--     mods = 'LEADER',
--     key = "'",
--     action = wezterm.action.IncreaseFontSize,
--   },
--   {
--     mods = 'LEADER',
--     key = '¿',
--     action = wezterm.action.DecreaseFontSize,
--   },
--   {
--     mods = 'LEADER',
--     key = '0',
--     action = wezterm.action.ResetFontSize,
--   },
--
--   -- panes
--   {
--     mods = 'LEADER',
--     key = '-',
--     action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' },
--   },
--   {
--     mods = 'LEADER',
--     key = '|',
--     action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' },
--   },
--   {
--     mods = 'LEADER',
--     key = 'z',
--     action = wezterm.action.TogglePaneZoomState,
--   },
--   {
--     mods = 'LEADER',
--     key = 'LeftArrow',
--     action = wezterm.action.ActivatePaneDirection 'Left',
--   },
--   {
--     mods = 'LEADER',
--     key = 'RightArrow',
--     action = wezterm.action.ActivatePaneDirection 'Right',
--   },
--   {
--     mods = 'LEADER',
--     key = 'UpArrow',
--     action = wezterm.action.ActivatePaneDirection 'Up',
--   },
--   {
--     mods = 'LEADER',
--     key = 'DownArrow',
--     action = wezterm.action.ActivatePaneDirection 'Down',
--   },
--   {
--     mods = 'LEADER',
--     key = 'x',
--     action = wezterm.action.CloseCurrentPane { confirm = true },
--   },
--   {
--     mods = 'LEADER',
--     key = ')',
--     action = wezterm.action.RotatePanes 'Clockwise',
--   },
--   {
--     mods = 'LEADER',
--     key = '<',
--     action = wezterm.action.PaneSelect { mode = 'SwapWithActive' },
--   },
--   {
--     mods = 'LEADER|ALT',
--     key = 'LeftArrow',
--     action = wezterm.action.AdjustPaneSize { 'Left', 1 },
--   },
--   {
--     mods = 'LEADER|ALT',
--     key = 'RightArrow',
--     action = wezterm.action.AdjustPaneSize { 'Right', 1 },
--   },
--   {
--     mods = 'LEADER|ALT',
--     key = 'UpArrow',
--     action = wezterm.action.AdjustPaneSize { 'Up', 1 },
--   },
--   {
--     mods = 'LEADER|ALT',
--     key = 'DownArrow',
--     action = wezterm.action.AdjustPaneSize { 'Down', 1 },
--   },
--
--   -- tabs
--   {
--     mods = 'LEADER',
--     key = 'Enter',
--     action = wezterm.action.SpawnTab 'DefaultDomain',
--   },
--   {
--     mods = 'LEADER|SHIFT',
--     key = 'x',
--     action = wezterm.action.CloseCurrentTab { confirm = true },
--   },
--   {
--     mods = 'LEADER|CTRL',
--     key = 'p',
--     action = wezterm.action.ActivateTabRelative(-1),
--   },
--   {
--     mods = 'LEADER|CTRL',
--     key = 'n',
--     action = wezterm.action.ActivateTabRelative(1),
--   },
-- }
--
-- for i = 1, 9 do
--   table.insert(config.keys, {
--     key = tostring(i),
--     mods = 'LEADER',
--     action = wezterm.action.ActivateTab(i - 1),
--   })
-- end

return config
