-- Pull in the wezterm API
local wezterm = require 'wezterm'

-- This will hold the configuration.
local config = wezterm.config_builder()
local act = wezterm.action

config.scrollback_lines = 1000000
config.enable_scroll_bar = false

config.default_cursor_style = 'BlinkingBar'
-- config.animation_fps = 1
config.front_end = "WebGpu"
config.cursor_blink_ease_in = 'Constant'
config.cursor_blink_ease_out = 'Constant'
config.selection_word_boundary = ' \t\n{}[]()"\'`,;/'

-- This is where you actually apply your config choices
config.ssh_domains = {
  {
    -- This name identifies the domain
    name = 'home',
    -- The hostname or address to connect to. Will be used to match settings
    -- from your ssh config file
    remote_address = '192.168.1.20',
    -- The username to use on the remote host
    username = 'anton',
  },
}
config.keys = {
  {
    key = 'f',
    mods = 'OPT',
    action = wezterm.action.ToggleFullScreen,
  },
	{
    key = ',',
    mods = 'CMD',
    action = act.SpawnCommandInNewTab {
      cwd = os.getenv('WEZTERM_CONFIG_DIR'),
      set_environment_variables = {
        TERM = 'screen-256color',
      },
      args = {
        '/opt/homebrew/bin/nvim',
        os.getenv('WEZTERM_CONFIG_FILE'),
      },
    },
  },
  { key = 'U', mods = 'CTRL|SHIFT', action = act.AttachDomain 'devhost' },
  -- Detaches the domain associated with the current pane
  {
    key = 'D',
    mods = 'CTRL|SHIFT',
    action = act.DetachDomain 'CurrentPaneDomain',
  },
  -- Detaches the "devhost" domain
  {
    key = 'E',
    mods = 'CTRL|SHIFT',
    action = act.DetachDomain { DomainName = 'devhost' },
  },
  {
    key = '|',
    mods = 'CMD',
    action = wezterm.action.SplitHorizontal { domain = 'CurrentPaneDomain' },
  },
  {
    key = 'Enter',
    mods = 'CMD',
    action = wezterm.action.SplitVertical { domain = 'CurrentPaneDomain' },
  },
    {
    key = 'p',
    mods = 'CMD',
    action = wezterm.action.ActivateCommandPalette,
  },
}

config.window_decorations = "RESIZE|INTEGRATED_BUTTONS"
config.initial_cols = 127
config.initial_rows = 67
config.window_background_opacity = 1
config.text_background_opacity = 1
config.macos_window_background_blur = 10

config.quit_when_all_windows_are_closed = false


-- For example, changing the color scheme: ==>  A!=B
config.color_scheme = 'Galaxy'
config.font = wezterm.font('Jetbrains Mono', { weight = 600 })
config.freetype_load_target = 'Light'
config.font_size = 14
config.line_height = 1.05
config.harfbuzz_features = { 'calt=1', 'clig=0', 'liga=0' }



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


config.window_frame = {
  font = wezterm.font { family = 'Jetbrains Mono', weight = 700 },

  -- The size of the font in the tab bar.
  font_size = 15.0,

  -- The overall background color of the tab bar when
  -- the window is focused
  -- active_titlebar_bg = '#222222',
  -- inactive_titlebar_bg = '#FF0000',
  -- active_titlebar_fg = '#ffffff',

 border_left_width = '1cell',
 border_right_width = '0.5cell',
 border_bottom_height = '0.25cell',
 border_top_height = '2cell',
 border_left_color = '#1c2836',
 border_right_color = '#1c2836',
 border_bottom_color = '#1c2836',
 border_top_color = '#1c2836',
}

config.use_fancy_tab_bar = false
config.tab_bar_at_bottom = true

config.colors = {
  tab_bar = {
    -- The color of the strip that goes along the top of the window
    -- (does not apply when fancy tab bar is in use)
    background = '#1c2836',

    -- The active tab is the one that has focus in the window
    active_tab = {
      bg_color = '#5588DD',
      fg_color = '#000000',

      -- Specify whether you want "Half", "Normal" or "Bold" intensity for the
      -- label shown for this tab.
      -- The default is "Normal"
      intensity = 'Normal',
    },

    inactive_tab = {
      bg_color = '#222222',
      fg_color = '#808080',
    },

    inactive_tab_hover = {
      bg_color = '#3b3052',
      fg_color = '#909090',
      italic = true,
    },

    new_tab = {
      bg_color = '#1c2836',
      fg_color = '#AAAACC',
    },

    new_tab_hover = {
      bg_color = '#3b3052',
      fg_color = '#909090',
      italic = true,
    },
  },
}
config.tab_max_width = 32
-- The filled in variant of the < symbol
local SOLID_LEFT_ARROW = wezterm.nerdfonts.ple_left_half_circle_thick

-- The filled in variant of the > symbol
local SOLID_RIGHT_ARROW = wezterm.nerdfonts.ple_right_half_circle_thick

-- This function returns the suggested title for a tab.
-- It prefers the title that was set via `tab:set_title()`
-- or `wezterm cli set-tab-title`, but falls back to the
-- title of the active pane in that tab.
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 edge_background = '#1c2836'
    local background = '#141c26'
    local foreground = '#4d82bf'

    if tab.is_active then
      background = '#2e84e6'
      foreground = '#000000'
    elseif hover then
      background = '#155082'
      foreground = '#000000'
    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 - 5)

    return {
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = " "..SOLID_LEFT_ARROW },
      { Background = { Color = background } },
      { Foreground = { Color = foreground } },
      { Text = " "..title.." " },
      { Background = { Color = edge_background } },
      { Foreground = { Color = edge_foreground } },
      { Text = SOLID_RIGHT_ARROW },
    }
  end
)

wezterm.on('update-right-status', function(window, pane)
  local domain = pane:get_domain_name()
  if (domain == "local") then
	  domain = ""
  end
  -- Make it italic and underlined
  window:set_right_status(wezterm.format {
    { Attribute = { Underline = 'None' } },
    { Attribute = { Italic = false } },
    { Text =  domain },
  })
end)


-- and finally, return the configuration to wezterm
return config
