{pkgs, ... }:

{
  services.yabai = {
    enable = true;
    config = {
      mouse_follows_focus = "off";
      focus_follows_mouse = "off";
      window_placement = "second_child";
      window_topmost = "off";
      window_opacity = "off";
      window_opacity_duration = 0.0;
      window_shadow = "on";
      window_border = "off";
      window_border_placement = "inset";
      window_border_width = 4;
      window_border_radius = -1.0;
      active_window_border_topmost = "off";
      active_window_border_color = "0xff775759";
      normal_window_border_color = "0xff505050";
      insert_window_border_color = "0xffd75f5f";
      active_window_opacity = 1.0;
      normal_window_opacity = 0.9;
      split_ratio = 0.73;
      auto_balance = "on";
      mouse_modifier = "fn";
      mouse_action1 = "move";
      mouse_action2 = "resize";
      layout = "bsp";
      top_padding = 5;
      bottom_padding = 5;
      left_padding = 5;
      right_padding = 5;
      window_gap = 5;
    };
    extraConfig = ''
      # Do not manage windows with certain titles eg. Copying files or moving to bin
      yabai -m rule --add title="(Copy|Bin|About This Mac|Info)" manage=off
      # Do not manage some apps which are not resizable
      yabai -m rule --add app="^(Calculator|System Preferences|[sS]tats|[Jj]et[Bb]rains [Tt]ool[Bb]ox|kftray)$" manage=off
    '';
  };

  system.activationScripts.yabai = {
    enable = true;
    text = ''
      yabai --install-service && yabai --start-service
    '';
  };

  services.skhd = {
    enable = true;
    skhdConfig = ''
# Open iTerm2
cmd - enter : kitty --single-instance -d ~

##################
# Window Motions #
##################
# Rotate
lalt - r : yabai -m space --rotate 90
# Mirror verticaly
lalt - x : yabai -m space --mirror y-axis
# Mirror horizontaly
lalt - y : yabai -m space --mirror x-axis
# yes, i know i swapped x and y, but I mainly use y-axis and y is further...
# Fullscreen
cmd - f : yabai -m window --toggle zoom-fullscreen
# Swap
lalt - q : yabai -m window --swap west
lalt - s : yabai -m window --swap south
lalt - z : yabai -m window --swap north
lalt - d : yabai -m window --swap east
# Warp
shift + lalt - q : yabai -m window --warp west
shift + lalt - s : yabai -m window --warp south
shift + lalt - z : yabai -m window --warp north
shift + lalt - d : yabai -m window --warp east

#########
# Focus #
#########
# Clockwise
# alt - tab : yabai -m window --focus "$(yabai -m query --windows --space | jq -re "[sort_by(.id, .frame) | .[] | select(.role == \"AXWindow\" and .subrole == \"AXStandardWindow\") | .id] | nth(index($(yabai -m query --windows --window | jq -re ".id")) - 1)")"
# Counter-clockwise
# shift - tab : yabai -m window --focus "$(yabai -m query --windows --space | jq -re "[sort_by(.id, .frame) | reverse | .[] | select(.role == \"AXWindow\" and .subrole == \"AXStandardWindow\") | .id] | nth(index($(yabai -m query --windows --window | jq -re ".id")) - 1)")"

##########
# Spaces #
##########
ctrl - left : yabai -m space --focus prev
ctrl - right : yabai -m space --focus next
ctrl + shift - right : yabai -m window --space next; yabai -m space --focus next
ctrl + shift - left : yabai -m window --space prev; yabai -m space --focus prev
cmd - 1 : yabai -m window --space  1; yabai -m space --focus 1
cmd - 2 : yabai -m window --space  2; yabai -m space --focus 2
cmd - 3 : yabai -m window --space  3; yabai -m space --focus 3
cmd - 4 : yabai -m window --space  4; yabai -m space --focus 4
cmd - 5 : yabai -m window --space  5; yabai -m space --focus 5
  '';
  };
}
