# vim:fileencoding=utf-8:foldmethod=marker
{
  pkgs,
  config,
  lib,
  osConfig,
  ...
}:
let
  inherit (lib) mapAttrs mkIf;
  inherit (lib.my) getExe getExe';

  desktop = osConfig.modules.system.roles.desktop;
  term = getExe desktop.terminal;
  filemanager = getExe desktop.filemanager;
  cliphistEnabled = osConfig.modules.services.cliphist.enable;

  colors = {
    lavender = "#babbf1";
    black = "#000000";
  };

  inputs = {
    kb = {
      builtin = "1:1:AT_Translated_Set_2_keyboard";
      keychron = "1452:591:Keychron_K4_Keychron_K4";
      keychron_bt = "1452:591:Keychron_K4";
    };
  };

  mkRegexList = list: "^(${(lib.strings.concatStringsSep "|" list)})$";
in
lib.mkIf osConfig.programs.sway.enable {
  services.swayidle.enable = true;
  programs.hyprlock.enable = true;

  wayland.windowManager.sway = {
    enable = true;
    xwayland = true;
    wrapperFeatures.gtk = true;
    extraConfigEarly = ''
      exec systemctl --user import-environment PATH && systemctl --user restart xdg-desktop-portal.service
    '';
    extraConfig = ''
      titlebar_padding 1
      floating_modifier Mod4 normal
      bindgesture {
        swipe:right workspace prev
        swipe:left workspace next
        swipe:up focus up
        swipe:down focus down
      }
      font pango:monospace 0.001
    '';
    config = {
      modifier = "Mod4";
      bars = [ ];
      #: Keybindings {{{
      keybindings =
        let
          mod = config.wayland.windowManager.sway.config.modifier;
        in
        lib.mkOptionDefault {
          "${mod}+p" = "exec ${getExe pkgs.hyprlock}";

          "${mod}+Shift+p" = "exec ${getExe pkgs.scripts.powerMenu}";
          # Reload the config file
          "${mod}+Shift+c" = "reload";
          # Kill the focused window
          "${mod}+Shift+q" = "kill";
          # Make focused window fullscreen
          "${mod}+f" = "fullscreen";
          # Start launcher
          "${mod}+d" = "exec ${getExe desktop.runner.package}";
          "${mod}+s" =
            let
              cliphist = getExe pkgs.cliphist;
              copy = getExe' pkgs.wl-clipboard "wl-copy";
            in
            mkIf cliphistEnabled "exec ${cliphist} list | ${desktop.runner.dmenu}  | ${cliphist} decode |  ${copy}";
          # Toggle the current focus between tiling and floating mode
          "${mod}+Shift+space" = "floating toggle";
          "${mod}+Return" = "exec ${term}";
          "${mod}+e" = "exec ${filemanager}";
          "${mod}+x" = "layout toggle tabbed split";
          "${mod}+Control+Left" = "workspace prev";
          "${mod}+Control+Right" = "workspace next";
          "${mod}+n" = "exec nysh msg dash toggle";
          "--locked XF86MonBrightnessUp" = "exec --no-startup-id ${getExe pkgs.brightnessctl} s +10%";
          "--locked XF86MonBrightnessDown" = "exec --no-startup-id ${getExe pkgs.brightnessctl} s 10%-";
          "--locked XF86AudioRaiseVolume" = "exec --no-startup-id ${getExe pkgs.pamixer} -i 5";
          "--locked XF86AudioLowerVolume" = "exec --no-startup-id ${getExe pkgs.pamixer} -d 5";
          "--locked XF86AudioMicMute" = "exec --no-startup-id ${getExe pkgs.pamixer} --default-source -m";
          "--locked XF86AudioMute" = "exec --no-startup-id ${pkgs.pamixer}/bin/pamixer -t";
          "--locked XF86AudioPlay" = "exec ${pkgs.playerctl}/bin/playerctl play-pause";
          "--locked XF86AudioPause" = "exec ${pkgs.playerctl}/bin/playerctl play-pause";
          "--locked XF86AudioNext" = "exec ${pkgs.playerctl}/bin/playerctl next";
          "--locked XF86AudioPrev" = "exec ${pkgs.playerctl}/bin/playerctl previous";
          "${mod}+u" =
            "exec ${getExe pkgs.hyprshot} -o ${config.xdg.userDirs.pictures}/screenshots -m region";
          "${mod}+Shift+u" =
            "exec ${getExe pkgs.hyprshot} --raw -m region | ${getExe pkgs.satty} -f - --fullscreen";

          #: Workspace movement {{{
          "--input-device=${inputs.kb.builtin} ${mod}+ampersand" = "workspace number 1";
          "--input-device=${inputs.kb.builtin} ${mod}+eacute" = "workspace number 2";
          "--input-device=${inputs.kb.builtin} ${mod}+quotedbl" = "workspace number 3";
          "--input-device=${inputs.kb.builtin} ${mod}+apostrophe" = "workspace number 4";
          "--input-device=${inputs.kb.builtin} ${mod}+parenleft" = "workspace number 5";
          "--input-device=${inputs.kb.builtin} ${mod}+minus" = "workspace number 6";
          "--input-device=${inputs.kb.builtin} ${mod}+egrave" = "workspace number 7";
          "--input-device=${inputs.kb.builtin} ${mod}+underscore" = "workspace number 8";
          "--input-device=${inputs.kb.builtin} ${mod}+ccedilla" = "workspace number 9";
          "--input-device=${inputs.kb.builtin} ${mod}+agrave" = "workspace number 10";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+ampersand" =
            "move container to workspace number 1";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+eacute" = "move container to workspace number 2";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+quotedbl" =
            "move container to workspace number 3";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+apostrophe" =
            "move container to workspace number 4";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+parenleft" =
            "move container to workspace number 5";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+minus" = "move container to workspace number 6";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+egrave" = "move container to workspace number 7";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+underscore" =
            "move container to workspace number 8";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+ccedilla" =
            "move container to workspace number 9";
          "--input-device=${inputs.kb.builtin} ${mod}+Shift+agrave" = "move container to workspace number 10";
          #: }}}
        };
      #: }}}
      assigns = {
        "2" = [ { app_id = "firefox"; } ];
        "3" = [ { app_id = "obsidian"; } ];
        "4" = [
          { class = "discord"; }
          { app_id = "com.discordapp.Discord"; }
        ];
        "5" = [ { app_id = "lollypop"; } ];
      };
      seat = {
        "*" = {
          #xcursor_theme = mkIf (
          #config ? home.pointerCursor.name
          #) "${config.home.pointerCursor.name} ${toString config.home.pointerCursor.size}";
          keyboard_grouping = "none";
        };
      };
      window = {
        border = 3;
        commands = [
          {
            command = "inhibit_idle fullscreen";
            criteria = {
              class = "^.*$";
              app_id = "^.*$";
            };
          }
          {
            command = "floating enable";
            criteria.app_id = mkRegexList [
              "org.keepassxc.KeePassXC"
              "xdg-desktop-portal-gtk"
              "org.kde.polkit-kde-authentication-agent-1"
              "com.nextcloud.desktopclient.nextcloud"
              "org.kde.kdeconnect.daemon"
            ];
          }
          {
            # Make it clear which window runs using xwayland
            command = ''title_format "[XWayland] %title"'';
            criteria.shell = "xwayland";
          }
          {
            # Nextcloud closes when its not focused, thus moving the mouse over any other window will close it
            command = "move position mouse";
            criteria.app_id = "com.nextcloud.desktopclient.nextcloud";
          }
        ];
      };
      terminal = "${term}";
      colors = with colors; {
        focused = {
          border = lavender;
          background = lavender;
          text = black;
          indicator = "#00d4ff";
          childBorder = lavender;
        };
      };
      gaps = {
        inner = 5;
      };
      #: Input {{{
      input = {
        "*" = {
          xkb_layout = "fr,us";
          xkb_options = "grp:alt_shift_toggle,compose:caps";
          xkb_numlock = "enabled";
        };
        "${inputs.kb.builtin}" = {
          xkb_layout = "fr";
        };
        "${inputs.kb.keychron}" = {
          xkb_layout = "us";
        };
        "${inputs.kb.keychron_bt}" = {
          xkb_layout = "us";
        };
        "7504:24926:ZMK_Project_TOTEM_Keyboard" = {
          xkb_layout = "us";
        };
        "type:touchpad" = {
          tap = "enabled";
          natural_scroll = "enabled";
          click_method = "clickfinger";
        };
      };
      #: }}}
      #: Output {{{
      output = mapAttrs (name: value: {
        scale = toString value.scale;
        pos = "${toString value.posX} ${toString value.posY}";
        res = "${toString value.resX}x${toString value.resY}";
        adaptive_sync = "on";
      }) osConfig.modules.system.outputs;
      #: }}}
    };
  };
}
