{ config, pkgs, lib, ... }:
with lib;
let
  cfg = config.modules.desktop.sway;
in
{
  config = mkIf config.modules.desktop.sway.enable {
    wayland.windowManager.sway = {
	    enable = true;
      wrapperFeatures.gtk = true;
      systemd.enable = true;
      config = {
        modifier = "Mod4";
        terminal = "${pkgs.foot}/bin/foot";
        left = "h";
        down = "j";
        up = "k";
        right = "l";
        bars= [];
        input = {
          "4617:27904:MNT_Research_MNT_Reform_Keyboard_2.0_US/ST" = {
            xkb_layout = "us";
            xkb_variant = "altgr-intl";
          };
        };
        output = {
          "*" = {
            bg = "${cfg.wallpapers.desktop}";
          };
        };
        seat = {
          "*".hide_cursor = "when-typing enable";
        };
        keybindings = let
          mod = config.wayland.windowManager.sway.config.modifier;
          inherit (config.wayland.windowManager.sway.config)
            left down up right terminal;
        in
        {
        # navigate only with direction or workspace
          "${mod}+${left}" = "focus left";
          "${mod}+${down}" = "focus down";
          "${mod}+${up}" = "focus up";
          "${mod}+${right}" = " focus right";
          "${mod}+1" = "workspace $ws1";
          "${mod}+2" = "workspace $ws2";
          "${mod}+3" = "workspace $ws3";
          "${mod}+4" = "workspace $ws4";
          "${mod}+5" = "workspace $ws5";
          "${mod}+6" = "workspace $ws6";
          "${mod}+7" = "workspace $ws7";
          "${mod}+8" = "workspace $ws8";
          "${mod}+9" = "workspace $ws9";
          "${mod}+0" = "workspace $ws0";
        # move with Shift and direction
          "${mod}+Shift+${left}" = "move left";
          "${mod}+Shift+${down}" = "move down";
          "${mod}+Shift+${up}" = "move up";
          "${mod}+Shift+${right}" = "move right";
          "${mod}+Shift+1" = "move container to workspace $ws1";
          "${mod}+Shift+2" = "move container to workspace $ws2";
          "${mod}+Shift+3" = "move container to workspace $ws3";
          "${mod}+Shift+4" = "move container to workspace $ws4";
          "${mod}+Shift+5" = "move container to workspace $ws5";
          "${mod}+Shift+6" = "move container to workspace $ws6";
          "${mod}+Shift+7" = "move container to workspace $ws7";
          "${mod}+Shift+8" = "move container to workspace $ws8";
          "${mod}+Shift+9" = "move container to workspace $ws9";
          "${mod}+Shift+0" = "move container to workspace $ws0";
          "${mod}+Ctrl+${right}" = "move workspace to output right";
          "${mod}+Ctrl+${left}" = "move workspace to output left";
          "${mod}+Return" = "exec ${terminal}";
          "${mod}+d" = "exec ${pkgs.fuzzel}/bin/fuzzel";
        # manage windows manager actions with Alt
          "${mod}+Alt+k" = "kill";
          "${mod}+Alt+c" = "reload";
          "${mod}+Alt+Backspace" = "exec swaynag -t warning -m 'Exit sway?' -B 'Yes' 'swaymsg exit'";
          "${mod}+Alt+l" = "exec ${pkgs.swaylock}/bin/swaylock";
        # Manage windows placement
          "${mod}+b" = "splith";
          "${mod}+v" = "splitv";
          "${mod}+s" = "layout stacking";
          "${mod}+w" = "layout tabbed";
          "${mod}+e" = "layout toggle split";
          "${mod}+f" = "fullscreen";
          "${mod}+Shift+space" = "floating toggle";
          "${mod}+space" = "focus mode_toggle";
          "${mod}+a" = "focus parent";
          "${mod}+Shift+p" = "move scratchpad";
          "${mod}+p" = "scratchpad show";
        # Media stuff
          "${mod}+F1" = "exec ${pkgs.brightnessctl}/bin/brightnessctl s 1%-";
          "${mod}+F2" = "exec ${pkgs.brightnessctl}/bin/brightnessctl s +1%";
          "${mod}+F3" = "exec ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_SINK@ 5%-";
          "${mod}+F4" = "exec ${pkgs.wireplumber}/bin/wpctl set-volume @DEFAULT_SINK@ 5%+";
          "${mod}+F5" = "exec ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_SINK@ toggle";
          "${mod}+m" = "exec ${pkgs.wireplumber}/bin/wpctl set-mute @DEFAULT_SOURCE@ toggle";
          "${mod}+r" = "mode resize";
          "${mod}+n" = "exec makoctl dismiss";
          "${mod}+shift+n" = "exec makoctl dismiss -a";
          "${mod}+alt+n" = "mode notification";
        };
        modes =
        let
          inherit (config.wayland.windowManager.sway.config)
            left down up right;
        in {
          "resize" = {
              "${left}" = "resize shrink width 10 px or 10 ppt";
              "${down}" = "resize grow height 10 px or 10 ppt";
              "${up}" = "resize shrink height 10 px or 10 ppt";
              "${right}" = "resize grow width 10 px or 10 ppt";
              "Return" = "mode default";
              "Escape" = "mode default";
          };
          "notification" = {
            "r" = "exec ${pkgs.mako}/bin/makoctl restore";
            "Escape" = "mode default";
          };
        };
        colors = {
          focused = {
            border = "#f268b3";
            background = "#f268b3";
            text = "#000000";
            indicator = "#1cd180";
            childBorder = "#f268b3";
          };
          focusedInactive = {
            border = "#323232";
            background = "#323232";
            text = "#f268b3";
            indicator = "#1cd180";
            childBorder = "#323232";
          };
          unfocused = {
            border = "#323232";
            background = "#323232";
            text = "#cccccc";
            indicator = "#1cd180";
            childBorder = "#323232";
          };
          urgent = {
            border = "#2f343a";
            background = "#900000";
            text = "#cccccc";
            indicator = "#900000";
            childBorder = "#900000" ;
          };
          #focused_tab_title = {
          #  boder = "#2f343a";
          #  background = "#2f343a";
          #  text = "#ffffff";
          #};
        };
        window = {
          border = 1;
          titlebar = false;
          commands = [
          {
            command = "inhibit_idle fullscreen";
            criteria = {
              title = "^.*";
            };
          }
          {
            command = "inhibit_idle fullscreen";
            criteria = {
              app_id = "^.*";
            };
          }
          ];
        };
        gaps = {
          smartGaps = true;
          smartBorders = "on";
          inner = 2;
        };
        assigns = {
          "$ws3" = [{app_id = "firefox";} {app_id = "org.qutebrowser.qutebrowser";}];
          "$ws4" = [{app_id = "WebCord";}];
          "$ws7" = [{app_id = "org.inkscape.Inkscape";} {app_id = "org.kde.kdenlive";}];
        };
      };
      extraConfigEarly = ''
        set $ws1 1: 
        set $ws2 2: 󰇮
        set $ws3 3: 󰈹
        set $ws4 4: 󰍦
        set $ws5 5: 
        set $ws6 6: 󰈙
        set $ws7 7: 󰤚
        set $ws8 8: 
        set $ws9 9: 
        set $ws0 10: 
        default_orientation auto
        workspace_layout default
        font pango:Fira Code Nerd Font Mono 10
        default_border pixel 1
        title_align right
      '';
    };
  };
}
