-- hyprland.lua — migrated from hyprlang for Hyprland 0.55+
-- https://wiki.hypr.land/Configuring/Start/

-- ── Variables ────────────────────────────────────────────────────────────────

local super = "SUPER"
local terminal = "kitty"
local fileManager = "yazi"
local theme = "-theme $HOME/.config/rofi/custom.rasi"
local menu = "rofi -show drun " .. theme
local filebrowser = "rofi -show filebrowser " .. theme
local power = "rofi -show p -modi p:rofi-power-menu -theme $HOME/.config/rofi/power.rasi"
local apps = "rofi -show window " .. theme
local screenshot = "$HOME/Pictures/Screenshots/$(date +'%s_grim.png')"

-- ── Helpers ───────────────────────────────────────────────────────────────────

-- Bind a list of { key, action [, opts] } entries
local function bind_all(list)
	for _, b in ipairs(list) do
		hl.bind(b[1], b[2], b[3])
	end
end

-- Bind a list of { key, cmd [, opts] } entries as exec_cmd actions
local function bind_exec(list)
	for _, b in ipairs(list) do
		hl.bind(b[1], hl.dsp.exec_cmd(b[2]), b[3])
	end
end

-- Bind a list of { key, layout_cmd } entries as layout actions
local function bind_layout(list)
	for _, b in ipairs(list) do
		hl.bind(b[1], hl.dsp.layout(b[2]))
	end
end

-- ── Monitors ─────────────────────────────────────────────────────────────────

local monitors = {
	{ output = "DP-1", mode = "1920x1080@60", position = "1920x0", scale = 1 },
	{ output = "HDMI-A-2", mode = "1920x1080@60", position = "0x0", scale = 1 },
}

for _, m in ipairs(monitors) do
	hl.monitor(m)
end

-- ── Environment ──────────────────────────────────────────────────────────────

local env_vars = {
	NIXOS_OZONE_WL = "1",
	MOZ_ENABLE_WAYLAND = "1",
	MOZ_WEBRENDER = "1",
	_JAVA_AWT_WM_NONREPARENTING = "1",
	QT_WAYLAND_DISABLE_WINDOWDECORATION = "1",
	QT_QPA_PLATFORM = "wayland",
	SDL_VIDEODRIVER = "wayland",
	GDK_BACKEND = "wayland,x11",
	XCURSOR_SIZE = "24",
	XCURSOR_THEME = "catppuccin-mocha-dark",
	EDITOR = "nvim",
	GSK_RENDERER = "gl",
	HYPRCURSOR_THEME = "catppuccin-mocha-dark",
	HYPRCURSOR_SIZE = "24",
}

for k, v in pairs(env_vars) do
	hl.env(k, v)
end

-- ── Autostart ────────────────────────────────────────────────────────────────

local autostart = {
	"awww-daemon --no-cache & disown",
	"awww img ~/Pictures/Wallpapers/Ghost_in_the_Shell.png",
	"waybar &",
}

hl.on("hyprland.start", function()
	for _, cmd in ipairs(autostart) do
		hl.exec_cmd(cmd)
	end
end)

-- ── Config ───────────────────────────────────────────────────────────────────

hl.config({
	debug = {
		disable_logs = false,
		enable_stdout_logs = false,
	},

	input = {
		kb_layout = "de",
		kb_variant = "mac",
		kb_options = "apple:fn_lock",
		repeat_rate = 50,
		repeat_delay = 300,
		accel_profile = "flat",
		follow_mouse = 1,
		mouse_refocus = false,
		sensitivity = 0,
		numlock_by_default = true,
		touchpad = {
			natural_scroll = true,
			tap_to_click = true,
		},
	},

	general = {
		gaps_in = 2,
		gaps_out = 0,
		border_size = 4,
		col = {
			active_border = "rgba(a6e3a1ff)",
			inactive_border = "rgba(f38ba8ff)",
		},
		layout = "master",
		allow_tearing = false,
	},

	decoration = {
		rounding = 1,
		shadow = {
			enabled = false,
			range = 16,
			render_power = 4,
			color = "rgba(a6e3a1ff)",
			color_inactive = "rgba(f38ba8ff)",
		},
		blur = {
			enabled = false,
			size = 1,
			passes = 3,
			new_optimizations = true,
			noise = 0.04,
		},
	},

	animations = {
		enabled = true,
	},

	master = {
		new_status = "slave",
		new_on_top = false,
		new_on_active = "after",
		orientation = "left",
		mfact = 0.55,
		allow_small_split = false,
		smart_resizing = true,
		drop_at_cursor = true,
		focus_master_on_close = false,
	},

	misc = {
		force_default_wallpaper = 0,
		disable_hyprland_logo = true,
		disable_splash_rendering = true,
		mouse_move_enables_dpms = true,
		key_press_enables_dpms = true,
		vrr = 0,
	},
})

-- ── Animations ───────────────────────────────────────────────────────────────

hl.curve("myBezier", { type = "bezier", points = { { 0.05, 0.9 }, { 0.1, 1.05 } } })

local animations = {
	{ leaf = "windows", enabled = true, speed = 7, bezier = "myBezier" },
	{ leaf = "windowsOut", enabled = true, speed = 7, bezier = "default", style = "popin 80%" },
	{ leaf = "border", enabled = true, speed = 10, bezier = "default" },
	{ leaf = "borderangle", enabled = true, speed = 8, bezier = "default" },
	{ leaf = "fade", enabled = true, speed = 7, bezier = "default" },
	{ leaf = "workspaces", enabled = true, speed = 6, bezier = "default" },
}

for _, anim in ipairs(animations) do
	hl.animation(anim)
end

-- ── Devices ──────────────────────────────────────────────────────────────────

local devices = {
	{ name = "usb-optical-mouse-", sensitivity = 0 },
}

for _, d in ipairs(devices) do
	hl.device(d)
end

-- ── Binds ────────────────────────────────────────────────────────────────────

-- Applications & launchers
bind_exec({
	{ super .. " + Q", terminal },
	{ super .. " + E", fileManager },
	{ super .. " + O", "obsidian" },
	{ super .. " + I", "floorp" },
	{ super .. " + G", "thunderbird" },
	{ "XF86Mail", "thunderbird" },
	{ super .. " + N", "nautilus" },
	{ "XF86Search", "nautilus" },
	-- Rofi
	{ super .. " + F", filebrowser },
	{ super .. " + A", apps },
	{ "Menu", apps },
	{ super .. " + R", menu },
	{ "XF86LaunchA", menu },
	{ super .. " + S", power },
	{ "XF86LaunchB", power },
	-- Screenshots
	{ super .. " + Z", 'grim -g "$(slurp)" ' .. screenshot },
	{ super .. " + U", "grim " .. screenshot },
	-- Media & brightness (locked = works on lockscreen, repeating = held key)
	{ "XF86AudioMute", "pamixer -t", { locked = true } },
	{ "XF86AudioPlay", "playerctl play-pause", { locked = true } },
	{ "XF86AudioNext", "playerctl next", { locked = true } },
	{ "XF86AudioPrev", "playerctl previous", { locked = true } },
	{ "XF86AudioRaiseVolume", "pamixer -i 5", { locked = true, repeating = true } },
	{ "XF86AudioLowerVolume", "pamixer -d 5", { locked = true, repeating = true } },
	{ "XF86MonBrightnessUp", "brightnessctl set +5%", { locked = true, repeating = true } },
	{ "XF86MonBrightnessDown", "brightnessctl set 5%-", { locked = true, repeating = true } },
})

-- Window actions
bind_all({
	{ super .. " + M", hl.dsp.exit() },
	{ super .. " + P", hl.dsp.window.pseudo() },
	{ super .. " + V", hl.dsp.window.float({ action = "toggle" }) },
	{ super .. " + C", hl.dsp.window.close() },
	-- Mouse move/resize
	{ super .. " + mouse:272", hl.dsp.window.drag() },
	{ super .. " + mouse:273", hl.dsp.window.drag({ resize = true }) },
	-- Scroll through workspaces
	{ super .. " + mouse_down", hl.dsp.focus({ workspace = "e+1" }) },
	{ super .. " + mouse_up", hl.dsp.focus({ workspace = "e-1" }) },
})

-- Directional focus
for _, dir in ipairs({ "left", "right", "up", "down" }) do
	hl.bind(super .. " + " .. dir, hl.dsp.focus({ direction = dir }))
end

-- Master layout commands
bind_layout({
	{ super .. " + RETURN", "swapwithmaster auto" },
	{ super .. " + SHIFT + RETURN", "focusmaster auto" },
	{ super .. " + TAB", "cyclenext" },
	{ super .. " + SHIFT + TAB", "cycleprev" },
	{ super .. " + bracketright", "swapnext" },
	{ super .. " + bracketleft", "swapprev" },
	{ super .. " + period", "addmaster" },
	{ super .. " + comma", "removemaster" },
	{ super .. " + SHIFT + O", "orientationcycle left top right bottom" },
	{ super .. " + equal", "mfact +0.05" },
	{ super .. " + minus", "mfact -0.05" },
})

-- Workspaces 1–10 (key 0 maps to workspace 10)
for i = 1, 10 do
	local key = tostring(i % 10)
	hl.bind(super .. " + " .. key, hl.dsp.focus({ workspace = i }))
	hl.bind(super .. " + SHIFT + " .. key, hl.dsp.window.move({ workspace = i }))
end

-- Special workspace (scratchpad)
hl.bind(super .. " + SHIFT + S", hl.dsp.window.move({ workspace = "special:magic" }))

-- ── Workspace Rules ───────────────────────────────────────────────────────────

local workspace_rules = {
	{ workspace = "1", layout_opts = { orientation = "left" } },
	{ workspace = "2", layout_opts = { orientation = "top" } },
}

for _, rule in ipairs(workspace_rules) do
	hl.workspace_rule(rule)
end
