#!/usr/bin/env fish

alias hc=herbstclient

if command -sq "hlctl"; and test "$hlctl_enabled" != "false"
	RUST_LOG=info hlctl init
	exit $status
end
import fish_functions/notify
import fish_functions/restart_svcs
import fish_functions/group
import fish_functions/config
import fish_functions/remove_default

set -g THIS_PATH (pwd)
log "starting in $THIS_PATH"

# default font if not set
set_default hlwm_font "-*-fixed-medium-*-*-*-12-*-*-*-*-*-*-*"
new_config -n Font -v "$hlwm_font"

hc emit_hook reload
if command -sq "xsetroot";
	xsetroot -solid black
end
hc keyunbind --all
group_active_reset

# set -l Mod Mod1 # alt
set -g Mod Mod4

new_config -n ModKey -v $Mod
set -U hlwm_mod $Mod

function key
	log "Registering $Mod-$argv[1] to [$argv[2..]]"
	hc keybind $Mod-$argv[1] $argv[2..]
end

set_default hlwm_terminal_spawn_key "Home"

hc keybind $Mod-Shift-r reload
hc keybind $Mod-Shift-c close
hc keybind $Mod-s spawn "flameshot" gui
hc keybind $Mod-Return spawn "dmenu_run"
hc keybind $Mod-$hlwm_terminal_spawn_key spawn "alacritty"

# basic movement in tiling and floating mode
# focusing clients
hc keybind $Mod-Left  focus left
hc keybind $Mod-Down  focus down
hc keybind $Mod-Up    focus up
hc keybind $Mod-Right focus right

# Move frames
hc keybind $Mod-Shift-Right shift right
hc keybind $Mod-Shift-Left shift left
hc keybind $Mod-Shift-Up shift up
hc keybind $Mod-Shift-Down shift down

# frames
hc keybind $Mod-u       split   bottom  0.5
hc keybind $Mod-o       split   right   0.5
hc keybind $Mod-r	remove
hc keybind $Mod-f	fullscreen toggle
# Cycle layouts
hc keybind $Mod-space or , and . compare tags.focus.curframe_wcount = 2 . cycle_layout +1 vertical horizontal max vertical grid , cycle_layout +1

set -l resize_step
hc keybind $Mod-Control-Left    resize left +$resize_step
hc keybind $Mod-Control-Down    resize down +$resize_step
hc keybind $Mod-Control-Up      resize up +$resize_step
hc keybind $Mod-Control-Right   resize right +$resize_step

set_default hlwm_tags_per_group 5
set_default hlwm_group_count 3
new_config -n GroupCount 		 \
			-v $hlwm_group_count \
			-t uint
new_config -n TagsPerGroup    	  	\
			-v $hlwm_tags_per_group \
			-t uint

log "registering $hlwm_group_count groups, with $hlwm_tags_per_group tags per"
set -l tag_indexes (seq $hlwm_tags_per_group)

for group_num in (seq $hlwm_group_count)
	for tag_index in $tag_indexes
		# set -l tag (math $tag_index + '(('$group_num -1')' x $hlwm_tags_per_group ')')
		log "adding tag [$tag_index] to group [$group_num]"
		hc add "$group_num:$tag_index"
	end
end

# Remove the default tag
remove_default
group_set_keybinds

# focus
hc keybind $Mod-Tab cycle
hc keybind $Mod-i jumpto urgent

# mus
hc mouseunbind --all
hc mousebind $Mod-Button1 move
hc mousebind $Mod-Button2 zoom
hc mousebind $Mod-Button3 resize

#############################
#          Theming          #
#############################
set -l ACTIVE "#800080"
# TODO: Consider 6600ff for active
set -l NORMAL "#330033"
set -l URGENT "#7811A1"
alias hcs="hc set"
function hct
	hc attr theme.$argv[1] $argv[2]
end

function on
	hcs $argv[1] on
end

function off
    	hcs $argv[1] off
end

hct tiling.reset 1
hct floating.reset 1
hcs frame_border_active_color $ACTIVE
hcs frame_border_normal_color black
#hcs frame_bg_normal_color $ACTIVE
hcs frame_bg_active_color black
hcs frame_border_width 2
hcs show_frame_decorations 'focused_if_multiple'
on frame_bg_transparent
off smart_window_surroundings
on smart_frame_surroundings
hcs frame_transparent_width 5
hcs frame_gap 3
hcs window_gap 0
hcs frame_padding 0
hcs mouse_recenter_gap 0

hct title_height 15
hct title_when multiple_tabs
hct title_font 'Dejavu Sans:pixelsize=12'
hct title_depth 3
hct inner_width 0
hct inner_color black
hct border_width 1
hct floating.border_width 4
hct floating.outer_width 1
hct tiling.outer_width 1
hct background_color black

# Active
hct active.color $ACTIVE
hct active.inner_color $ACTIVE

# Normal
hct normal.color $NORMAL
hct normal.inner_color $NORMAL

# Urgent
hct urgent.color $URGENT
hct urgent.inner_color $URGENT

# Text
hct title_color white
hct normal.title_color '#898989'


new_config -n NotifyTextColor \
			-v (hc attr theme.normal.title_color)
new_config -n NotifyBgColor -v $NORMAL

for state in active urgent normal
	hc substitute C theme.$state.inner_color \
		attr theme.$state.outer_color C
end


# Rules
hc unrule -F
function rule
    	hc rule $argv[1]=$argv[2..]
end

function ruletype
    	hc rule windowtype~"\"_NET_WM_WINDOW_TYPE_$argv[1]\"" $argv[2]=$argv[3..]
end

rule focus on
rule floatplacement smart
ruletype '(DIALOG|UTILITY|SPLASH)' floating on
ruletype 'DIALOG' focus on
ruletype '(NOTIFICATION|DOCK|DESKTOP)' manage off
hc rule fixedsize floating=on
hcs tree_style '╾│ ├└╼─┐'

hc unlock

# Start services

log 'Starting services'
restart_svcs "fcitx5"
log 'Services start done'

function keybind_fn
	argparse -i 'direct=' -- $argv
	set -f script_name "fn_caller"
	if set -q _flag_direct
		set -f script_name $_flag_direct
	end
	key $argv[1] try spawn (printf "fish\n%s" "$THIS_PATH/fish_cmds/$script_name.fish") $argv[2]
end

log "Registering custom command binds"
keybind_fn i "toggle_idle_screen"
keybind_fn --direct="switch_group" "grave" "+" # backtick
for group_index in (seq $hlwm_group_count)
	keybind_fn --direct="switch_group" "F$group_index" "$group_index"
end
log "Custom commands bound"

set -l monitors (hc list_monitors | cut -d: -f1)
if not set -q hlwm_skip_panel
	# For now, still use bash panel.sh
	for monitor in $monitors
		bash ./panel.sh "$monitor" &
	end
else
	# undo all the padding
	for monitor in $monitors;
		hc pad $monitor 0
	end
	notify skipping panel
end