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

with lib;

let
  cfg = config.home.roles.plasma-manager;
  wallpaperConfig = import ../../wallpapers;
  currentWallpaper = builtins.elemAt wallpaperConfig.wallpapers wallpaperConfig.currentIndex;
in
{
  options.home.roles.plasma-manager = {
    enable = mkEnableOption "KDE Plasma desktop environment configuration";
  };

  config = mkIf cfg.enable {
    # The current KDE config can be output with the command:
    # nix run github:nix-community/plasma-manager
    #
    # Plasma-manager options documentation
    # https://nix-community.github.io/plasma-manager/options.xhtml
    #
    # TODO: (ambitious) Add Kmail support to plasma-manager
    programs.plasma = {
      enable = true;
      overrideConfig = true;

      hotkeys.commands."launch-ghostty" = {
        name = "Launch Ghostty";
        key = "Meta+Return";
        command = "ghostty";
      };

      shortcuts = {
        kmix = {
          "decrease_microphone_volume" = "Microphone Volume Down";
          "decrease_volume" = "Volume Down";
          "decrease_volume_small" = "Shift+Volume Down";
          "increase_microphone_volume" = "Microphone Volume Up";
          "increase_volume" = "Volume Up";
          "increase_volume_small" = "Shift+Volume Up";
          "mic_mute" = ["Microphone Mute" "Meta+Volume Mute,Microphone Mute" "Meta+Volume Mute,Mute Microphone"];
          "mute" = "Volume Mute";
        };

        mediacontrol = {
          "mediavolumedown" = "none,,Media volume down";
          "mediavolumeup" = "none,,Media volume up";
          "nextmedia" = "Media Next";
          "pausemedia" = "Media Pause";
          "playmedia" = "none,,Play media playback";
          "playpausemedia" = "Media Play";
          "previousmedia" = "Media Previous";
          "stopmedia" = "Media Stop";
        };

        ksmserver = {
          "Lock Session" = ["Meta+Ctrl+Q" "Screensaver" "Screensaver,Lock Session"];
        };

        kwin = {
          "Window Close" = "Meta+Shift+Q";
          "Kill Window" = "Meta+Ctrl+Esc";
          "Window Operations Menu" = "Alt+F3";
          "Window Resize" = "Meta+R,,Resize Window";

          "Overview" = "Meta+Ctrl+W";
          "Grid View" = "Meta+G";
          "Edit Tiles" = "Meta+T";

          "Activate Window Demanding Attention" = "Meta+Ctrl+A";

          "Show Desktop" = "Meta+Ctrl+D";

          "Walk Through Windows" = "Alt+Tab";
          "Walk Through Windows (Reverse)" = "Alt+Shift+Tab";
          "Walk Through Windows of Current Application" = "Alt+`";
          "Walk Through Windows of Current Application (Reverse)" = "Alt+~";

          "Window Quick Tile Bottom" = "Meta+Down";
          "Window Quick Tile Left" = "Meta+Left";
          "Window Quick Tile Right" = "Meta+Right";
          "Window Quick Tile Top" = "Meta+Up";

          "Switch to Desktop 1" = "Meta+1";
          "Switch to Desktop 2" = "Meta+2";
          "Switch to Desktop 3" = "Meta+3";
          "Switch to Desktop 4" = "Meta+4";
          "Switch to Desktop 5" = "Meta+5";
          "Switch to Desktop 6" = "Meta+6";
          "Switch to Desktop 7" = "Meta+7";
          "Switch to Desktop 8" = "Meta+8";
          "Switch to Desktop 9" = "Meta+9";
          "Switch to Desktop 10" = "Meta+0";

          "Window to Desktop 1" = "Meta+!";    # Meta+Shift+1
          "Window to Desktop 2" = "Meta+@";    # Meta+Shift+2
          "Window to Desktop 3" = "Meta+#";    # Meta+Shift+3
          "Window to Desktop 4" = "Meta+$";    # Meta+Shift+4
          "Window to Desktop 5" = "Meta+%";    # Meta+Shift+5
          "Window to Desktop 6" = "Meta+^";    # Meta+Shift+6
          "Window to Desktop 7" = "Meta+&";    # Meta+Shift+7
          "Window to Desktop 8" = "Meta+*";    # Meta+Shift+8
          "Window to Desktop 9" = "Meta+(";    # Meta+Shift+9
          "Window to Desktop 10" = "Meta+)";   # Meta+Shift+0

          "view_actual_size" = "Meta+Ctrl+=";
          "view_zoom_in" = ["Meta++" "Meta+=,Meta++" "Meta+=,Zoom In"];
          "view_zoom_out" = "Meta+-";
        };
        "org_kde_powerdevil"."Decrease Keyboard Brightness" = "Keyboard Brightness Down";
        "org_kde_powerdevil"."Decrease Screen Brightness" = "Monitor Brightness Down";
        "org_kde_powerdevil"."Decrease Screen Brightness Small" = "Shift+Monitor Brightness Down";
        "org_kde_powerdevil"."Hibernate" = "Hibernate";
        "org_kde_powerdevil"."Increase Keyboard Brightness" = "Keyboard Brightness Up";
        "org_kde_powerdevil"."Increase Screen Brightness" = "Monitor Brightness Up";
        "org_kde_powerdevil"."Increase Screen Brightness Small" = "Shift+Monitor Brightness Up";
        "org_kde_powerdevil"."PowerDown" = "Power Down";
        "org_kde_powerdevil"."PowerOff" = "Power Off";
        "org_kde_powerdevil"."Sleep" = "Sleep";
        "org_kde_powerdevil"."Toggle Keyboard Backlight" = "Keyboard Light On/Off";
        "org_kde_powerdevil"."Turn Off Screen" = [ ];
        "org_kde_powerdevil"."powerProfile" = ["Battery" "Meta+B,Battery" "Meta+B,Switch Power Profile"];

        plasmashell = {
          "activate application launcher" = ["Meta" "Alt+F1,Meta" "Alt+F1,Activate Application Launcher"];
          "activate task manager entry 1" = "none,,";
          "activate task manager entry 2" = "none,,";
          "activate task manager entry 3" = "none,,";
          "activate task manager entry 4" = "none,,";
          "activate task manager entry 5" = "none,,";
          "activate task manager entry 6" = "none,,";
          "activate task manager entry 7" = "none,,";
          "activate task manager entry 8" = "none,,";
          "activate task manager entry 9" = "none,,";
          "activate task manager entry 10" = "none,,";
          "show activity switcher" = "none,,";
        };
      };

      configFile = {
        kwinrc.Desktops.Number = {
          value = 10;
          immutable = true;
        };

        # Enable KWin tiling features
        kwinrc.Tiling = {
          # Enable tiling functionality
          "padding" = 4;
        };

        # Enable krohnkite plugin automatically
        kwinrc.Plugins = {
          krohnkiteEnabled = true;
        };

        kwinrc.Effect-overview = {
          # Configure overview effect for better tiling workflow
          BorderActivate = 9;  # Top-left corner activation
        };

        kcminputrc.Libinput = {
          AccelerationProfile = "adaptive";
          PointerAcceleration = 0.5;
        };

        kcminputrc.Mouse = {
          X11LibInputXAccelProfileFlat = false;
          XLbInptAccelProfileFlat = false;
        };

        kdeglobals.KDE.LookAndFeelPackage = "org.kde.breezedark.desktop";

        # Focus follows mouse configuration
        kwinrc.Windows = {
          FocusPolicy = "FocusFollowsMouse";
          AutoRaise = true;  # Set to true if you want windows to auto-raise on focus
          AutoRaiseInterval = 750;  # Delay in ms before auto-raise (if enabled)
          DelayFocusInterval = 0;  # Delay in ms before focus follows mouse
        };

        # Desktop wallpaper configuration
        plasma-localerc.Formats.LANG = "en_US.UTF-8";

        # Set wallpaper for all desktops
        plasmarc.Wallpapers.usersWallpapers = "${currentWallpaper.file}";
      };
    };
  };
}
