local wezterm = require 'wezterm'
local config = wezterm.config_builder()

-- keybinds
config.leader = { key = 'a', mods = 'CTRL', timeout_milliseconds = 1000 }
config.keys = {
     {
      mods   = "LEADER|SHIFT",
      key    = '"',
      action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' }
    },
    {
      mods   = "LEADER|SHIFT",
      key    = "%",
      action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' }
    },
    {
      mods = 'LEADER',
      key = 'z',
      action = wezterm.action.TogglePaneZoomState
    },
    {
      mods = "LEADER",
      key = "Space",
      action = wezterm.action.RotatePanes "Clockwise"
    }, 
    {
      mods = 'LEADER',
      key = '0',
      action = wezterm.action.PaneSelect {
        mode = 'SwapWithActive',
      },
    },
    {
      key = 'v',
      mods = 'LEADER',
      action = wezterm.action.ActivateCopyMode
    },
    {
      key = 'a',
      mods = 'LEADER|CTRL',
      action = wezterm.action.SendKey { key = 'a', mods = 'CTRL' },
    },
    {
      key = 'k',
      mods = 'ALT',
      action = wezterm.action.ScrollToPrompt(-1)
    },
    {
      key = 'j',
      mods = 'ALT',
      action = wezterm.action.ScrollToPrompt(1)
    },
    {
      key = 'c',
      mods = 'LEADER',
      action = wezterm.action.ScrollToPrompt(1)
    },
    {
      key = 'n',
      mods = 'LEADER',
      action = wezterm.action.SwitchWorkspaceRelative(1)
    },
    {
      key = 'p',
      mods = 'LEADER',
      action = wezterm.action.SwitchWorkspaceRelative(-1)
    },
}

config.mouse_bindings = {
    {
      event = { Down = { streak = 2, button='Left' } },
      action = wezterm.action.SelectTextAtMouseCursor 'SemanticZone',
      mods = 'SHIFT'
    }
}

-- stylin'
local color = (function()
  local COLOR = require "colors"

  local coolors = {
    COLOR.VERIDIAN,
    COLOR.PAYNE,
    COLOR.INDIGO,
    COLOR.CAROLINA,
    COLOR.FLAME,
    COLOR.JET,
    COLOR.TAUPE,
    COLOR.ECRU,
    COLOR.VIOLET,
    COLOR.VERDIGRIS
  }

  return coolors[math.random(#coolors)]
end)()

local color_primary = color

local title_color_bg = color_primary.bg
local title_color_fg = color_primary.fg

local color_off = title_color_bg:lighten(0.4)
local color_on = color_off:lighten(0.4)
wezterm.on('update-right-status', function(window)
    local bat = ''
    local b = wezterm.battery_info()[1]
    if b then
      bat = wezterm.format {
        { Foreground = {
          Color =
            b.state_of_charge > 0.2 and color_on or color_off,
        } },
        { Text = '▉' },
        { Foreground = {
          Color =
            b.state_of_charge > 0.4 and color_on or color_off,
        } },
        { Text = '▉' },
        { Foreground = {
          Color =
            b.state_of_charge > 0.6 and color_on or color_off,
        } },
        { Text = '▉' },
        { Foreground = {
          Color =
            b.state_of_charge > 0.8 and color_on or color_off,
        } },
        { Text = '▉' },
        { Background = {
          Color =
            b.state_of_charge > 0.98 and color_on or color_off,
        } },
        { Foreground = {
          Color =
            b.state == "Charging"
              and color_on:lighten(0.3):complement()
              or
                (b.state_of_charge < 0.2 and wezterm.GLOBAL.count % 2 == 0)
                  and color_on:lighten(0.1):complement()
                  or color_off:darken(0.1)
        } },
        { Text = ' ⚡ ' },
      }
    else 
      bat = wezterm.format {
        { Text = '🖥' },
      }
    end

    local time = wezterm.strftime '%k:%M '

    local bg1 = title_color_bg:lighten(0.1)
    local bg2 = title_color_bg:lighten(0.2)

    window:set_right_status(
      wezterm.format {
        { Background = { Color = title_color_bg } },
        { Foreground = { Color = bg1 } },
        { Text = '' },
        { Background = { Color = title_color_bg:lighten(0.1) } },
        { Foreground = { Color = title_color_fg } },
        { Text = ' ' .. window:active_workspace() .. ' ' },
        { Foreground = { Color = bg1 } },
        { Background = { Color = bg2 } },
        { Text = '' },
        { Foreground = { Color = title_color_bg:lighten(0.4) } },
        { Foreground = { Color = title_color_fg } },
        { Text = ' ' .. time .. ' ' .. bat }
      }
    )
end)
wezterm.on('gui-startup', function(cmd)
    local mux = wezterm.mux

    local padSize = 80
    local screenWidth = 1920
    local screenHeight = 1080

    local tab, pane, window = mux.spawn_window(cmd or {
      workspace = 'main',
    })

    if window ~= nil then
      window:gui_window():set_position(padSize, padSize)
      window:gui_window():set_inner_size(screenWidth - (padSize * 2), screenHeight - (padSize * 2) - 48)
    end
end)

local TAB_EDGE_LEFT = wezterm.nerdfonts.ple_left_half_circle_thick
local TAB_EDGE_RIGHT = wezterm.nerdfonts.ple_right_half_circle_thick

local function tab_title(tab_info)
  local title = tab_info.tab_title
  local icon = tab_info.active_pane.user_vars.ICON
  if icon then
    return icon .. ' ' .. tab_info.active_pane.title:gsub("%.exe", ""):gsub("panki: ", "")
  else
    return tab_info.active_pane.title:gsub("%.exe", ""):gsub("panki: ", "")
  end
end

wezterm.on(
  'format-tab-title',
  function(tab, _, _, _, hover, max_width)
    local edge_background = title_color_bg
    local background = title_color_bg:lighten(0.05)
    local foreground = title_color_fg

    if tab.is_active then
      background = background:lighten(0.1)
      foreground = foreground:lighten(0.1)
    elseif hover then
      background = background:lighten(0.2)
      foreground = foreground:lighten(0.2)
    end

    local edge_foreground = background

    local title = tab_title(tab)

    -- ensure that the titles fit in the available space,
    -- and that we have room for the edges.
    title = wezterm.truncate_right(title, max_width - 2)

    return {
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = TAB_EDGE_LEFT },
      { Background = { Color = background } },
      { Foreground = { Color = foreground } },
      { Text = title },
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = TAB_EDGE_RIGHT },
    }
  end
)

config.colors = {
    tab_bar = {
      active_tab = {
        bg_color = title_color_bg:lighten(0.03),
        fg_color = title_color_fg:lighten(0.8),
        intensity = "Bold",
      },
      inactive_tab = {
        bg_color = title_color_bg:lighten(0.01),
        fg_color = title_color_fg,
        intensity = "Half",
      },
      inactive_tab_edge = title_color_bg
    },
    split = title_color_bg:lighten(0.3):desaturate(0.5)
}

config.window_background_opacity = 0.9
config.window_frame = {
    active_titlebar_bg = title_color_bg,
    inactive_titlebar_bg = title_color_bg,
    font_size = 11.0,
}

config.window_decorations = 'RESIZE'
config.win32_system_backdrop = "Disable"
config.show_tab_index_in_tab_bar = false
config.show_new_tab_button_in_tab_bar = false

config.color_scheme = 'Argonaut (Gogh)'

config.font = wezterm.font 'JetBrains Mono'
config.window_padding = {
    left = 0,
    right = 0,
    top = 0,
    bottom = 0,
}
config.use_fancy_tab_bar = true
config.hide_tab_bar_if_only_one_tab = false
config.tab_bar_at_bottom = true
config.tab_max_width=24

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

config.quick_select_alphabet = "asdfqweryxcvjkluiopmghtzbn"
config.scrollback_lines = 9001

config.use_dead_keys = false

dimmer = { brightness = 0.1 }

--#config.background = {
--#  {
--#    source = {
--#      File = 'bg_img\\00009.png'
--#    },
--#    hsb = dimmer,
--#    attachment = { Parallax = 0.1 }
--#  },
--#  {
--#    source = {
--#      File = 'bg_img\\bkgd_2.png'
--#    },
--#    hsb = dimmer,
--#    attachment = { Parallax = 0.2 }
--#  },
--#  {
--#    source = {
--#      File = 'bg_img\\bkgd_6.png'
--#    },
--#    hsb = dimmer,
--#    attachment = { Parallax = 0.3 }
--#  },
--#  {
--#    source = {
--#      File = 'bg_img\\bkgd_7.png'
--#    },
--#    hsb = dimmer,
--#    attachment = { Parallax = 0.6 }
--#  },
--#}

-- WSL
if wezterm.target_triple == 'x86_64-pc-windows-msvc' then
  config.default_domain = 'WSL:Debian'
end

return config
