{ config, lib, pkgs, ... }: 
let 
  searchwolf = import ../../../other/scripts/desktop/searchwolf.nix { inherit pkgs; };
  desktop-ctl = import ../../../other/scripts/desktop/desktopctl.nix { inherit pkgs; };
  pc-get = pkgs.writeShellScriptBin "pc-get.sh" (builtins.readFile ../../../other/scripts/desktop/permaclip/pc-get.sh);
  pc-set = pkgs.writeShellScriptBin "pc-set.sh" (builtins.readFile ../../../other/scripts/desktop/permaclip/pc-set.sh);
  em-record = pkgs.writeShellScriptBin "em-record.sh" (builtins.readFile ../../../other/scripts/desktop/macros/record.sh);
  em-play = pkgs.writeShellScriptBin "em-play.sh" (builtins.readFile ../../../other/scripts/desktop/macros/play.sh);
  em-play-loop = pkgs.writeShellScriptBin "em-play-loop.sh" (builtins.readFile ../../../other/scripts/desktop/macros/play-loop.sh);
  ocr-screenshot = pkgs.writeShellScriptBin "ocr-screenshot.sh" (builtins.readFile ../../../other/scripts/desktop/ocr-screenshot.sh);
  # rofi calculator copied from https://github.com/barbuk/menu-qalc
  menu-qalc = pkgs.writeShellScriptBin "menu-qalc" (builtins.readFile ../../../other/scripts/desktop/menu-qalc.sh);
in with lib; {
  options.jade.desktop.i3wm = {
    enable = mkEnableOption "Enable i3wm";
  };
  config = mkIf config.jade.desktop.i3wm.enable {
    services.xserver.windowManager.i3.enable = true;
    home-manager.users.jade = { pkgs, ... }: {
        xsession.windowManager.i3 = {
          enable = true;
          config = {
            bars = [];
            workspaceAutoBackAndForth = true;
            focus = {
              followMouse = false;
            };
            # Startup {{{
              startup = [
                # wallpaper
                { 
                  command = "feh --bg-fill ${../../../other/wallpaper.jpg}"; 
                  always = true; 
                  notification = false; 
                }

                # meowmeowmeow
                { 
                  command = "pkill oneko; oneko -tofocus  -tora -position +-1+5 -fg palevioletred3 -bg pink -name 'Rose the desktop kitty'"; 
                  always = true; 
                }
              ];
            # }}}
            # Assigns {{{
            assigns = {
              "1" = [ 
                { class = "discord"; } 
                { class = "nheko"; } 
              ];
              "2" = [ 
                { class = "librewolf"; } 
              ];
            };
            # }}}
            # Keyboard shortcuts {{{
            modifier = "Mod4";
            modes = {
              resize = with {
                # mod = builtins.break config.xsession.windowManager.i3.config.modifier;
                mod = builtins.break "Mod4";
              }; {
                aaa = builtins.break "a";
                "${mod}+h" = "resize shrink width 10 px or 10 ppt";
                "${mod}+j" = "resize grow height 10 px or 10 ppt";
                "${mod}+k" = "resize shrink height 10 px or 10 ppt";
                "${mod}+l" = "resize grow width 10 px or 10 ppt";

                "Return" = "mode default";
                "Escape" = "mode default";
                "${mod}+r" = "mode default";
              };
            };
            keybindings = with {
                 #mod = config.xsession.windowManager.i3.config.modifier;
                 mod = "Mod4";
            }; lib.mkOptionDefault {
              # switch window focus
              "${mod}+h" = "focus left";
              "${mod}+j" = "focus down";
              "${mod}+k" = "focus up";
              "${mod}+l" = "focus right";
              # move windows
              "${mod}+Shift+h" = "move left";
              "${mod}+Shift+j" = "move down";
              "${mod}+Shift+k" = "move up";
              "${mod}+Shift+l" = "move right";
              # layout shit 
              "${mod}+shift+semicolon" = "split h";
              "${mod}+semicolon" =  "split v";
              "${mod}+f" = "fullscreen toggle";
              "${mod}+Shift+w" = "layout tabbed";
              "${mod}+e" = "layout toggle split";
              "${mod}+Shift+space" = "floating toggle";
              # focus parents/children
              "${mod}+Shift+a" = "focus parent";
              "${mod}+Shift+c" = "focus child";
              # screenshot
              "${mod}+w" = "exec window-screenshot.sh";
              "${mod}+s" = "exec flameshot gui -c -p $HOME/Pictures/screenshots";
              "${mod}+a" = "exec flameshot screen -c -p $HOME/Pictures/screenshots";
              "${mod}+t" = "exec ocr-screenshot.sh";

              # rofi fuckery
              "${mod}+d" = "exec --no-startup-id rofi -show drun -theme ${../../../other/rofi-themes/applauncher.rasi}";
              "${mod}+space" = "exec --no-startup-id rofi -show combi -combi-show window#run -modes combi -theme ${../../../other/rofi-themes/applauncher.rasi}";
              "${mod}+i" = "exec --no-startup-id rofimoji -f alchemical_symbols anatolian_hieroglyphs emojis braille_patterns box_drawing chess_symbols emoticons geometric_shapes gothic greek_extended math mathematical_alphanumeric_symbols mathematical_operators miscellaneous_symbols miscellaneous_mathematical_symbols-a miscellaneous_mathematical_symbols-b miscellaneous_symbols_and_arrows miscellaneous_symbols_and_pictographs miscellaneous_technical modi modifier_tone_letters musical_symbols nerd_font number_forms shorthand_format_controls specials variation_selectors vertical_forms -a copy";
              "${mod}+Shift+e" = "exec --no-startup-id rofi -show \"desktopctl\" -modes \"desktopctl:${desktop-ctl.outPath}/bin/desktopctl\" -theme ${../../../other/rofi-themes/applauncher.rasi}";
              "${mod}+m" = "exec --no-startup-id menu-qalc";

              "${mod}+o" = "exec --no-startup-id rofi -show searchwolf -modes \"searchwolf:${searchwolf.outPath}/bin/searchwolf\"";

              # audio
              "XF86AudioRaiseVolume" = "exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ +10% && $refresh_i3status";
              "XF86AudioLowerVolume" = "exec --no-startup-id pactl set-sink-volume @DEFAULT_SINK@ -10% && $refresh_i3status";
              "XF86AudioMute" = "exec --no-startup-id pactl set-sink-mute @DEFAULT_SINK@ toggle && $refresh_i3status";
              "XF86AudioMicMute" = "exec --no-startup-id pactl set-source-mute @DEFAULT_SOURCE@ toggle && $refresh_i3status";

              "XF86AudioNext" = "exec playerctl next";
              "XF86AudioPrev" = "exec playerctl previous";
              "XF86AudioPause" = "exec playerctl pause";
              "XF86AudioPlay" = "exec playerctl play";
              "XF86AudioStop" = "exec playerctl stop";

              "XF86MonBrightnessUp" = "exec brightnessctl set 5%+";
              "XF86MonBrightnessDown" = "exec brightnessctl set 5%-";

              "XF86KbdBrightnessUp" = "exec brillo -kA 10.0";
              "XF86KbdBrightnessDown" = "exec brillo -kU 10.0";

              # macros
              "${mod}+q" = "exec em-record.sh";
              "${mod}+p" = "exec em-play.sh";
              "${mod}+Shift+p" = "exec em-play-loop.sh";

              # permaclip
              "${mod}+c" = "exec rofi -show register -modes \"register:${pc-set.outPath}/bin/pc-set.sh\" -theme gruvbox-dark";
              "${mod}+v" = "exec rofi -show register -modes \"register:${pc-get.outPath}/bin/pc-get.sh\" -theme gruvbox-dark";
            };
            # }}}
            # Visuals {{{
            fonts = {
              names = [ "Atkinson Hyperlegible" ];
              style = "Regular";
              size = 9.0;
            };
            gaps = {
              #top = 24;
              inner = 15;
              outer = 0;
              smartGaps = true;
              smartBorders = "on";
            };
            colors = {
              background = "#1d2021";
              focused = { background = "#282828"; border = "#504945"; childBorder = "#7c6f64"; indicator = "#504945"; text = "#ebdbb2"; };
              focusedInactive = { background = "#1d2021"; border = "#504945"; childBorder = "#665c54"; indicator = "#664c54"; text = "#d5c4a1"; };
              placeholder = { background = "#1d2021"; border = "#00ff00"; childBorder = "#504945"; indicator = "#504945"; text = "#928374"; };
              unfocused = { background = "#1d2021"; border = "#3c3836"; childBorder = "#504945"; indicator = "#504945"; text = "#bdae93"; };
              urgent = { background = "#9d0006"; border = "#cc241d"; childBorder = "#3c3836"; indicator = "#fb4943"; text = "#ebdbb2"; };
            };
            window = {
              border = 2;
              titlebar = false;
            };
            # }}}
          };
        };
    };
  };
}
