{ config, pkgs, lib, ... }:
let
  modKey = "Mod4";
  UP = "k";
  DOWN = "j";
  LEFT = "h";
  RIGHT = "l";
  MENU = "rofi -show drun";
  TERM = "alacritty";
  EXIT = "i3-nagbar -t warning -m 'Exit i3?' -B 'Yes' 'i3-msg exit'";
in
{
  programs.i3status = {
    enable = true;
    enableDefault = false;
    general = {
      output_format = "i3bar";
      interval = 1;
      colors = true;
      color_separator = "#FFFFFF";
      color_good = "#00FF00";
      color_degraded = "#00FFFF";
      color_bad = "#FF0000";
      separator = "][";
    };
    modules = {
      "time" = {
        position = 1;
        settings = {
          format = "%Y %m/%d %a | %H:%M:%S";
        };
      };
      "load" = {
        position = 2;
        settings = {
          format = "LD %1min:%5min:%15min";
          format_above_threshold = "LD H (%1min:%5min)";
          max_threshold = "4.9";
        };
      };
      "memory" = {
        position = 3;
        settings = {
          memory_used_method = "classical";
          decimals = "1";
          unit = "Mi";
          format = "%available (%used) / %total";
          format_degraded = "Mem LOW (%available)";
          threshold_degraded = "10%";
          threshold_critical = "6.25%";
        };
      };
      "disk /" = {
        position = 4;
        settings = {
          prefix_type = "decimal";
          format = "/ %percentage_used";
        };
      };
      "volume master" = {
        position = 5;
        settings = {
          format = "Vol: %volume";
          format_muted = "Vol: Muted (%volume)";
          device = "pulse:1";
        };
      };
      "wireless _first_" = {
        position = 6;
        settings = {
          format_up = "W: (%quality @ %essid w/ %bitrate) %ip";
          format_down = "W: down";
          format_bitrate = "%g %cb/s";
          format_quality = "%03d%s";
        };
      };
    };
  };

  xsession.windowManager.i3 = {
    enable = true;
    config = {
      defaultWorkspace = ''workspace number "1"'';
      modifier = modKey;
      menu = MENU;
      terminal = TERM;
      workspaceLayout = "tabbed";
      gaps = {
        smartBorders = "on";
        smartGaps = true;
        inner = 10;
      };
      startup = [
        { command = "fcitx5 -r"; always = true; notification = false; }
      ];
      modes = {
        resize = {
          "${UP}" = "resize shrink height 10 px or 10 ppt";
          "${DOWN}" = "resize grow height 10 px or 10 ppt";
          "${LEFT}" = "resize shrink width 10 px or 10 ppt";
          "${RIGHT}" = "resize grow width 10 px or 10 ppt";
          Escape = "mode default";
        };
      };
      keybindings = {
        "${modKey}+Return" = "exec ${TERM}";
        "${modKey}+d" = "exec ${MENU}";
        "${modKey}+Shift+q" = "kill";
        "${modKey}+${LEFT}" = "focus left";
        "${modKey}+${RIGHT}" = "focus right";
        "${modKey}+${UP}" = "focus up";
        "${modKey}+${DOWN}" = "focus down";
        "${modKey}+Shift+${LEFT}" = "move left";
        "${modKey}+Shift+${RIGHT}" = "move right";
        "${modKey}+Shift+${UP}" = "move up";
        "${modKey}+Shift+${DOWN}" = "move down";
        "${modKey}+y" = "split h";
        "${modKey}+t" = "split v";
        "${modKey}+f" = "fullscreen toggle";
        "${modKey}+s" = "layout stacking";
        "${modKey}+w" = "layout tabbed";
        "${modKey}+e" = "layout toggle split";
        "${modKey}+Shift+space" = "floating toggle";
        "${modKey}+space" = "focus mode_toggle";
        "${modKey}+minus" = "scratchpad show";
        "${modKey}+Shift+minus" = "move scratchpad";
        "${modKey}+n" = "workspace next";
        "${modKey}+p" = "workspace prev";
        "${modKey}+Shift+c" = "reload";
        "${modKey}+Shift+r" = "restart";
        "${modKey}+Shift+e" = "exec ${EXIT}";
        "${modKey}+r" = "mode resize";
        "${modKey}+1" = ''workspace number "1"'';
        "${modKey}+2" = ''workspace number "2"'';
        "${modKey}+3" = ''workspace number "3"'';
        "${modKey}+4" = ''workspace number "4"'';
        "${modKey}+5" = ''workspace number "5"'';
        "${modKey}+6" = ''workspace number "6"'';
        "${modKey}+7" = ''workspace number "7"'';
        "${modKey}+8" = ''workspace number "8"'';
        "${modKey}+9" = ''workspace number "9"'';
        "${modKey}+0" = ''workspace number "10"'';
        "${modKey}+Shift+1" = ''move container to workspace number "1"'';
        "${modKey}+Shift+2" = ''move container to workspace number "2"'';
        "${modKey}+Shift+3" = ''move container to workspace number "3"'';
        "${modKey}+Shift+4" = ''move container to workspace number "4"'';
        "${modKey}+Shift+5" = ''move container to workspace number "5"'';
        "${modKey}+Shift+6" = ''move container to workspace number "6"'';
        "${modKey}+Shift+7" = ''move container to workspace number "7"'';
        "${modKey}+Shift+8" = ''move container to workspace number "8"'';
        "${modKey}+Shift+9" = ''move container to workspace number "9"'';
        "${modKey}+Shift+0" = ''move container to workspace number "10"'';
        "XF86AudioRaiseVolume" = ''exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +5%'';
        "XF86AudioLowerVolume" = ''exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -5%'';
        "XF86AudioMute" = ''exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle'';
      };
    };
  };

  home.packages = with pkgs; [
    rofi
  ];
}

