#
#  Sway NixOS & Home manager configuration
#
#  flake.nix
#   ├─ ./hosts
#   │   └─ ./laptop
#   │       └─ home.nix
#   └─ ./modules
#       └─ ./desktop
#           └─ ./sway
#               └─ home.nix *
#

{ config, lib, pkgs, ... }:

{
  home.file.".config/sway/scripts/2in1screen".source = ../scripts/2in1screen;

  wayland.windowManager.sway = {
     enable = true;
     config = rec {
        menu = "${pkgs.bemenu}/bin/bemenu-run -m -1 --hf '#ffff00' --tf '#888888' --nf '#00ff40' --hb '#424242' | xargs ${pkgs.sway}/bin/swaymsg exec --";
        left = "m";
        down = "n";
        up = "e";
        right = "i";
        modifier = "Mod4";
        floating.modifier = "Mod4";
        floating.titlebar = true;
        
        input = {
            "type:keyboard" = { 
                xkb_layout = "us";
                xkb_variant = "altgr-intl";
            };
            "type:touchpad" = {
                tap = "enabled";
                natural_scroll = "disabled";
                drag = "enabled";
                dwt = "enabled";
                middle_emulation = "enabled";
                scroll_method = "two_finger";
                tap_button_map = "lmr";
            };
        };

        output = {
            "*" = { 
                bg = "$HOME/.config/wall fill";
            };
            "DP-1" = {
                mode = "1920x1080";
                pos = "0,0";
            };
            "HDMI-A-1" = {
                mode = "2560x1080";
                pos = "1920,0";
            };
            #"eDP-1" = { 
            #    mode = "1920x1080";
            #    #pos = "4480,0";
            #    pos = "2560,0";
            #    subpixel = "none";
            #    scale = "1.3";
            #};
        };

        terminal = "${pkgs.alacritty}/bin/alacritty";
    
        fonts = {
            names = [ "Source Code Pro" ];
            size = 10.0;
        };
        
        colors.focused = {
            background = "#212121";
            border = "#999999";
            childBorder = "#999999";
            indicator = "#999999";
            text = "#FFFFFF";
        };

        startup = [
            #{ command = "$HOME/.config/sway/scripts/2in1screen"; }
            { command = "xrdb -load ~/.Xresources"; }
            { command = "gsettings set org.gnome.desktop.interface gtk-theme Arc"; }
            { command = "gsettings set org.gnome.desktop.interface icon-theme ePapirus"; }
            { command = "gsettings set org.gnome.desktop.interface cursor-theme Adwaita"; }
            #{ command = "exec ${pkgs.networkmanagerapplet}/bin/nm-applet --indicator"; }
            { command = "${pkgs.thunderbird}/bin/thunderbird"; }
            { command = "${pkgs.firefox}/bin/firefox"; }
            { command = "${pkgs.element-desktop}/bin/element-desktop"; }
        ];
    
        workspaceAutoBackAndForth = true;
    
        gaps.inner = 8;
        gaps.outer = 4;
        gaps.smartGaps = true;
        gaps.smartBorders = "on";

        assigns = {
            "workspace number 1" = [{ app_id = "thunderbird"; }];
            "workspace number 2" = [{ app_id = "firefox"; }];
            "workspace number 3" = [{ class = "Element"; }];
        };

        window.commands = [
            { command = "floating enable"; criteria = { app_id = ".yubioath-flutter-wrapped"; }; }
            { command = "floating enable"; criteria = { app_id = "pavucontrol"; }; }
            { command = "floating enable"; criteria = { app_id = "galculator"; }; }
            { command = "floating enable"; criteria = { app_id = "com.nitrokey."; }; }
            { command = "floating enable"; criteria = { app_id = "org.keepassxc.KeePassXC."; }; }
            { command = "floating enable"; criteria = { app_id = "virt-manager"; }; }
            { command = "floating enable"; criteria = { class = "lxqt-openssh-askpass"; }; }
            { command = "floating enable"; criteria = { class = "pop-up"; }; }
        ];
        
        bars = [
            { command = "${pkgs.waybar}/bin/waybar"; }
        ];

        defaultWorkspace = "workspace number 2";

        keybindings = 
          let
            mod = config.wayland.windowManager.sway.config.modifier;
            alt = "Mod1";
            left = config.wayland.windowManager.sway.config.left;
            down = config.wayland.windowManager.sway.config.down;
            up = config.wayland.windowManager.sway.config.up;
            right = config.wayland.windowManager.sway.config.right;
          in
          {
            "${mod}+Escape" = "exec swaymsg exit";     # Exit Sway
            "${mod}+Return" = "exec ${terminal}";      # Open terminal
            "${mod}+d" = "exec ${menu}";           # Open menu
            "${mod}+l" = "exec ${pkgs.swaylock}/bin/swaylock"; # Lock Screen

            "${mod}+r" = "reload";                     # Reload environment
            "${mod}+q" = "kill";                       # Kill container

            "${mod}+Shift+f" = "exec ${pkgs.pcmanfm}/bin/pcmanfm"; # File Manager


            "${alt}+${left}" = "workspace prev_on_output";         # Navigate to previous or next workspace on output if it exists
            "${alt}+${right}" = "workspace next_on_output";
            "${alt}+Shift+${left}" = "move container to workspace prev, workspace prev";    # Move container to next available workspace and focus
            "${alt}+Shift+${right}" = "move container to workspace next, workspace next";

            "XF86TouchpadToggle" = "input type:touchpad events toggle enabled disabled";
            "XF86AudioRaiseVolume" = "exec pulsemixer --change-volume +5 && ${config.cmds.notifications.volume}";
            "XF86AudioLowerVolume" = "exec pulsemixer --change-volume -5 && ${config.cmds.notifications.volume}";
            "XF86AudioMute" = "exec pulsemixer --toggle-mute && ${config.cmds.notifications.volume}";
            #"XF86AudioRaiseVolume" = "exec pulsemixer --change-volume +5";
            #"XF86AudioLowerVolume" = "exec pulsemixer --change-volume -5";
            #"XF86AudioMute" = "exec pulsemixer --toggle-mute";
            "XF86AudioMicMute" = "exec pactl set-source-mute @DEFAULT_SOURCE@ toggle";
            "XF86MonBrightnessDown" = "exec light -s sysfs/backlight/intel_backlight -U 5% && ${config.cmds.notifications.brightness}";
            "XF86MonBrightnessUp" = "exec light -s sysfs/backlight/intel_backlight -A 5% && ${config.cmds.notifications.brightness}";
            "XF86AudioPlay" = "exec playerctl play-pause";
            "XF86AudioNext" = "exec playerctl next";
            "XF86AudioPrev" = "exec playerctl previous";
            "XF86AudioStop" = "exec playerctl stop";
  
              #XF86AudioMute = "exec pactl set-sink-mute @DEFAULT_SINK@ toggle";
              #XF86AudioRaiseVolume = "exec pactl set-sink-volume @DEFAULT_SINK@ +5%";
              #XF86AudioLowerVolume = "exec pactl set-sink-volume @DEFAULT_SINK@ -5%";
              #XF86AudioPlay = "exec ~/.config/waybar/scripts/toggle-play";
              #XF86AudioNext = "exec playerctl --player=spotify next";
              #XF86AudioPrev = "exec playerctl --player=spotify previous";
  
            "${mod}+${left}" = "focus left";
            "${mod}+${down}" = "focus down";
            "${mod}+${up}" = "focus up";
            "${mod}+${right}" = "focus right";
  
            "${mod}+Shift+${left}" = "move left";
            "${mod}+Shift+${down}" = "move down";
            "${mod}+Shift+${up}" = "move up";
            "${mod}+Shift+${right}" = "move right";
  
            "${mod}+${alt}+${left}" = "resize grow width 10px";
            "${mod}+${alt}+${down}" = "resize shrink height 10px";
            "${mod}+${alt}+${up}" = "resize grow height 10px";
            "${mod}+${alt}+${right}" = "resize shrink width 10px";
  
            "${mod}+1" = "workspace number 1";
            "${mod}+2" = "workspace number 2";
            "${mod}+3" = "workspace number 3";
            "${mod}+4" = "workspace number 4";
            "${mod}+5" = "workspace number 5";
            "${mod}+6" = "workspace number 6";
            "${mod}+7" = "workspace number 7";
            "${mod}+8" = "workspace number 8";
            "${mod}+9" = "workspace number 9";
            "${mod}+0" = "workspace number 10";
  
            "${mod}+Shift+1" = "move container to workspace number 1";
            "${mod}+Shift+2" = "move container to workspace number 2";
            "${mod}+Shift+3" = "move container to workspace number 3";
            "${mod}+Shift+4" = "move container to workspace number 4";
            "${mod}+Shift+5" = "move container to workspace number 5";
            "${mod}+Shift+6" = "move container to workspace number 6";
            "${mod}+Shift+7" = "move container to workspace number 7";
            "${mod}+Shift+8" = "move container to workspace number 8";
            "${mod}+Shift+9" = "move container to workspace number 9";
            "${mod}+Shift+0" = "move container to workspace number 10";
  
            "${mod}+k" = "splith";
            "${mod}+v" = "splitv";
  
            "${mod}+b" = "layout stacking";
            "${mod}+w" = "layout tabbed";
            "${mod}+p" = "layout toggle split";
  
            "${mod}+f" = "fullscreen";
            "${mod}+Shift+space" = "floating toggle";
            "${mod}+h" = "focus mode_toggle";
            "${mod}+a" = "focus parent";
  
            "${mod}+Shift+minus" = "move scratchpad";
            "${mod}+minus" = "scratchpad show";
        };
     };
    systemdIntegration = true;
    wrapperFeatures.gtk = true;
    extraSessionCommands = ''
      export MOZ_ENABLE_WAYLAND="1";
      export MOZ_WEBRENDER="1";
      export MOZ_USE_XINPUT2="2";
      export MOZ_DBUS_REMOTE="1";
      #export GDK_BACKEND="wayland";
      export LIBVA_DRIVER_NAME="iHD";
      export VDPAU_DRIVER="iHD";
      export XDG_SESSION_TYPE="wayland";
      export XDG_CURRENT_DESKTOP="sway";
      export QT_QPA_PLATFORMTHEME="wayland-egl";
      export GST_VAAPI_ALL_DRIVERS="1";
      export GTK_THEME="Arc";
      export _JAVA_AWT_WM_NONREPARENTING="1";
      #export LIBCL_ALWAYS_SOFTWARE="1";       # For applications in VM like alacritty to work
      #export WLR_NO_HARDWARE_CURSORS="1";     # For cursor in VM
    '';

    extraConfig = ''
        set $output-primary HDMI-A-1
        set $output-secondary DP-1

        workspace 1 output $output-secondary
        workspace 2 output $output-primary
        workspace 3 output $output-secondary
    '';
  };

  programs.swaylock.settings = {
    color = "000000";
    image = "$HOME/.config/lockwall"; 
    indicator-caps-lock = true;
    show-keyboard-layout = true;
  };

  services.swayidle = {
    enable = true;
    events = [
      { event = "before-sleep"; command = "${pkgs.swaylock}/bin/swaylock"; }
      { event = "lock"; command = "${pkgs.swaylock}/bin/swaylock -fF"; }
      { event = "after-resume"; command = "${pkgs.sway}/bin/swaymsg 'output * dpms on'"; }
      { event = "unlock"; command = "${pkgs.sway}/bin/swaymsg 'output * dpms on'"; }
    ];
    timeouts = [
      { timeout = 300; command = "${pkgs.swaylock}/bin/swaylock -fF"; }
      { timeout = 600; command = "${pkgs.sway}/bin/swaymsg 'output * dpms off'"; resumeCommand = "${pkgs.sway}/bin/swaymsg 'output * dpms on'"; }
    ];
  };
}