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

{

  home.packages = with pkgs; [
    swayimg
    wl-clipboard
    waybar
    wofi
  ];
  home.file = {
    ".config/waybar" = {
      source = ./waybar;
    };
    ".config/wofi/config".text = ''
      show=drun
      drun-print_command=true
    '';
  };
  wayland.windowManager.sway.enable = true;
  wayland.windowManager.sway.package = pkgs.swayfx;
  wayland.windowManager.sway.config = {
    bars = [{
      command = "waybar";
      position = "top";
    }];
    colors.focused = {
      border = "#ef5ffc";
      background = "#ef5ffc";
      text = "#333333";
      indicator = "#2e9ef4";
      childBorder = "#ef5ffc";
    };
    defaultWorkspace = "1";
    floating = {
      criteria = [
        {
          app_id = "org.keepassxc.KeePassXC";
        }
      ];
    };
    gaps = {
      inner = 8;
      smartBorders = "on";
      smartGaps = true;
    };
    input = {
      "type:keyboard" = {
        xkb_layout = "us";
        xkb_variant = "altgr-intl";
        xkb_options = "caps:escape,compose:102,shift:both_capslock,eurosign:e";
      };
    };
    keybindings = 
      let
        modifier = config.wayland.windowManager.sway.config.modifier;
        menu = config.wayland.windowManager.sway.config.menu;
      in lib.mkOptionDefault {
        "${modifier}+space" = "exec ${menu}";
        "${modifier}+Control+Space" = "focus mode_toggle";
        "XF86AudioLowerVolume" = "exec pactl set-sink-volume @DEFAULT_SINK@ -5%";
        "XF86AudioRaiseVolume" = "exec pactl set-sink-volume @DEFAULT_SINK@ +5%";
        "XF86AudioMute" = "exec pactl set-sink-mute @DEFAULT_SINK@ toggle";
        "XF86AudioMicMute" = "exec pactl set-source-mute @DEFAULT_SOURCE@ toggle";
        "XF86Launch1" = "exec ~/scripts/sunset.sh";
        "${modifier}+Control+p" = "exec swaylock -f -c ef5ffc";
    };
    menu = "wofi -G | xargs swaymsg exec --";
    modifier = "Mod4";
    output = {
      "*" = {
        bg = "~/.config/sway/xenia.png fill";
      };
    };
    window.titlebar = false;
    terminal = "alacritty";
    window.border = 2;
  };
  wayland.windowManager.sway.extraConfig = ''
    blur enable
    blur_xray disable
    corner_radius 4
    shadows enable
    default_dim_inactive 0.1
    dim_inactive_colors.unfocused #fc28c3

    exec swayidle -w \
      timeout 300 'swaylock -f -c ef5ffc' \
      timeout 600 'swaymsg output * dpms off' \
        resume 'swaymsg putput * dpms on' \
      before-sleep 'swaylock -f -c ef5ffc'
    
    exec keepassxc

    include ~/.config/sway/device-conf
  '';
  # Let Home Manager install and manage itself.
  services.swayidle = {
    enable = false;
    events = [
      {
        event = "before-sleep";
        command = "/run/current-system/sw/bin/swaylock -f -c ef5ffc";
      }
    ];
    timeouts = [
      {
        command = "/run/current-system/sw/bin/swaylock -f -c ef5ffc";
        timeout = 300;
      }
      {
        command = "swaymsg \"output * dpms off\"";
        resumeCommand = "swaymsg \"output * dpms on\"";
        timeout = 600;
      }
    ];
  };
}
