local wezterm = require("wezterm")
local os = require("os")
local platform = require("platform")

local M = {}

-- Determine the appearance (light or dark)
-- @return: The appearance
local function get_appearance()
	if wezterm.gui then
		return wezterm.gui.get_appearance()
	end
	return "Dark"
end

-- Set the color scheme and tab bar colors based on the appearance
-- @param appearance: The appearance (light or dark)
-- @param schemes: The color schemes for light and dark appearances
-- @return: The color scheme for the appearance
local function scheme_for_appearance(appearance, schemes)
	if appearance:find("Dark") then
		return schemes.dark
	else
		return schemes.light
	end
end

-- Color schemes for light and dark appearances
local color_schemes = {
	dark = "Monokai (dark) (terminal.sexy)",
	light = "Solarized (light) (terminal.sexy)",
}

-- Define custom tab bar colors for light and dark themes
local tab_bar_colors = {
	dark = {
		active_tab = {
			bg_color = "#1c1f24",
			fg_color = "#f8f8f2",
		},
		inactive_tab = {
			bg_color = "#3e4451",
			fg_color = "#a0a0a0",
		},
		inactive_tab_hover = {
			bg_color = "#4e545e",
			fg_color = "#c0c0c0",
		},
		new_tab = {
			bg_color = "#1c1f24",
			fg_color = "#f8f8f2",
		},
		new_tab_hover = {
			bg_color = "#4e545e",
			fg_color = "#c0c0c0",
		},
	},
	light = {
		active_tab = {
			bg_color = "#eee8d5",
			fg_color = "#073642",
		},
		inactive_tab = {
			bg_color = "#93a1a1",
			fg_color = "#586e75",
		},
		inactive_tab_hover = {
			bg_color = "#839496",
			fg_color = "#586e75",
		},
		new_tab = {
			bg_color = "#eee8d5",
			fg_color = "#073642",
		},
		new_tab_hover = {
			bg_color = "#839496",
			fg_color = "#586e75",
		},
	},
}

-- Check if a command is available
-- @param cmd: The command to check for
-- @return: Whether the command is available
local function is_command_available(cmd)
	-- Use os.execute with a command that checks for command existence
	local result = os.execute('command -v ' .. cmd)
	return result
end

-- Function to update the Starship palette in the starship.toml file using sed
-- @param palette_name: The name of the palette to switch to
local function update_starship_palette(palette_name)
	if not is_command_available("starship") then
		wezterm.log_info("Starship is not installed")
		return
	end

	local starship_config_path = os.getenv("HOME") .. "/.dotfiles/starship/.config/starship.toml"

	-- Get platform information
	local plat = platform.platform()

	local sed_command
	if plat.is_mac then
		-- macOS sed command
		sed_command = string.format(
			'sed -i \'\' \'s/palette = .*/palette = "%s"/\' %s',
			palette_name,
			starship_config_path
		)
		wezterm.log_info("Switching Starship palette to " .. palette_name)
	elseif plat.is_linux then
		-- Linux sed command
		sed_command = string.format(
			'sed -i \'s/palette = .*/palette = "%s"/\' %s',
			palette_name,
			starship_config_path
		)
		wezterm.log_info("Switching Starship palette to " .. palette_name)
	else
		wezterm.log_info("Unsupported operating system: " .. plat.os)
		return
	end

	os.execute(sed_command)
end

-- Apply the configuration and update the Starship palette
-- @param config: The configuration object
-- @return: The updated configuration object
function M.apply_to_config(config)
	local appearance = get_appearance()
	config.color_scheme = scheme_for_appearance(appearance, color_schemes)
	config.colors = {
		tab_bar = scheme_for_appearance(appearance, tab_bar_colors),
	}

	-- Update the Starship palette based on the appearance
	if appearance:find("Dark") then
		update_starship_palette("monokai_pro")
	else
		update_starship_palette("solarized_light")
	end
end

return M

