local wezterm = require("wezterm")
local act = wezterm.action

local hostname = wezterm.hostname()

--
-- Main configuration.
--

config = {

	-- Set up our font.
	font = wezterm.font_with_fallback({
	 	"MonoLisa",
	 	"Codicons",
	}),
	font_size = 14,

	-- Color scheme stuff.
	color_scheme = "Solarized Dark - Patched",

	-- Generel config.
	term = "wezterm",

	-- Get rid of the annoying update popups we can't do anything about.
	check_for_updates = false,

	-- Automatically reload our config on changes.
	automatically_reload_config = true,

	-- Improve tab viewing by disabling native wonk,
	-- and make it behave more like Tmux.
	use_fancy_tab_bar = false,
	tab_bar_at_bottom = true,
	tab_max_width = 32,

	-- Disable window styling, where possible.
	window_decorations = "RESIZE",

	-- Use WebGpu, when we can.
	front_end = "WebGpu",

	-- Tab bar styling.
	colors = {
		tab_bar = {
			background = "#002b36",
			active_tab = {
				bg_color = "#073642",
				fg_color = "#268bd2",
				intensity = "Bold",
			},
			inactive_tab = {
				bg_color = "#002b36",
				fg_color = "#586e75",
			},
			new_tab = {
				bg_color = "#002b36",
				fg_color = "#586e75",
			},
		},
	},

	-- Set up multiplexing.
	unix_domains = { { name = "unix" } },
	ssh_domains = {
		{
			name = "trailblazer",
			remote_address = "trailblazer",
			remote_wezterm_path = "/run/current-system/sw/bin/wezterm",
		},
		{ name = "valere", remote_address = "valere", remote_wezterm_path = "/run/current-system/sw/bin/wezterm" },
		{ name = "hinata", remote_address = "hinata", remote_wezterm_path = "/run/current-system/sw/bin/wezterm" },
	},

	-- Key bindings.
	leader = { key = "`" },
	keys = {

		-- Tmux keybindings: tabs and splits.
		{
			key = "c",
			mods = "LEADER",
			action = act.SpawnTab("CurrentPaneDomain"),
		},
		{
			key = "RightArrow",
			mods = "LEADER",
			action = act.ActivatePaneDirection("Right"),
		},
		{
			key = "UpArrow",
			mods = "LEADER",
			action = act.ActivatePaneDirection("Up"),
		},
		{
			key = "DownArrow",
			mods = "LEADER",
			action = act.ActivatePaneDirection("Down"),
		},
		{
			key = ";",
			mods = "LEADER",
			action = act.ActivatePaneDirection("Next"),
		},
		{
			key = "v",
			mods = "LEADER",
			action = act.SplitVertical({ domain = "CurrentPaneDomain" }),
		},
		{
			key = "h",
			mods = "LEADER",
			action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }),
		},
		{
			key = "5",
			mods = "LEADER|SHIFT",
			action = act.SplitVertical({ domain = "CurrentPaneDomain" }),
		},
		{
			key = "'",
			mods = "LEADER|SHIFT",
			action = act.SplitHorizontal({ domain = "CurrentPaneDomain" }),
		},
		{
			key = "1",
			mods = "LEADER",
			action = act.ActivateTab(0),
		},
		{
			key = "2",
			mods = "LEADER",
			action = act.ActivateTab(1),
		},
		{
			key = "3",
			mods = "LEADER",
			action = act.ActivateTab(2),
		},
		{
			key = "4",
			mods = "LEADER",
			action = act.ActivateTab(3),
		},
		{
			key = "5",
			mods = "LEADER",
			action = act.ActivateTab(4),
		},
		{
			key = "6",
			mods = "LEADER",
			action = act.ActivateTab(5),
		},
		{
			key = "7",
			mods = "LEADER",
			action = act.ActivateTab(6),
		},
		{
			key = "8",
			mods = "LEADER",
			action = act.ActivateTab(7),
		},
		{
			key = "9",
			mods = "LEADER",
			action = act.ActivateTab(8),
		},
		{
			key = "0",
			mods = "LEADER",
			action = act.ActivateTab(10),
		},
		{
			key = "x",
			mods = "LEADER",
			action = act.CloseCurrentPane({ confirm = true }),
		},

		-- Tmux keybindings: misc.
		{
			key = "`",
			mods = "LEADER",
			action = act.SendKey({ key = "`" }),
		},
		{
			key = "d",
			mods = "LEADER",
			action = act.DetachDomain("CurrentPaneDomain"),
		},
		{
			key = "z",
			mods = "LEADER",
			action = act.TogglePaneZoomState,
		},

		-- Generic customization.
		{ key = "l", mods = "SHIFT|CTRL", action = "ShowDebugOverlay" },
		{ key = "Enter", mods = "ALT", action = "DisableDefaultAssignment" },
		{ key = "Enter", mods = "SUPER", action = "ToggleFullScreen" },
	},
}

--
-- Font Size Tweaks
--
font_overrides = {
	miko = 10,
	hinata = 16,
	valere = 12,
	utol = 13,
	trailblazer = 10,
	kanbaru = 12,
	aigis = 12;
}

if font_overrides[hostname] ~= nil then
	config.font_size = font_overrides[hostname]
end


--
-- Increase tab bar spacing.
--
wezterm.on("format-tab-title", function(tab, tabs, panes, config, hover, max_width)
	-- Truncate our title so we have room for a padding space at the end.
	local title = wezterm.truncate_right(tab.active_pane.title, max_width - 6)
	local number = tostring((tab.tab_index + 1) % 10)

	-- Generate the tab format.
	return " " .. number .. ": " .. title .. " "
end)

-- Windows support.
-- Possibly replace with a per-platform equivalent?'
--if (hostname == "hinata") then
--	config['default_prog'] = {"C:\\Users\\deprekated\\scoop\\apps\\python\\current\\Scripts\\xonsh.exe"}
--end

return config
