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

with lib;
let 
  cfg = config.modules.software.distractions.discord;
  flags = [
    "--flag-switches-begin"
    "--disable-gpu-memory-buffer-video-frames"
    "--enable-accelerated-mjpeg-decode"
    "--enable-accelerated-video"
    "--enable-gpu-rasterization"
    "--enable-native-gpu-memory-buffers"
    "--enable-zero-copy"
    "--ignore-gpu-blocklist"
    "--enable-features=UseOzonePlatform"
    "--enable-features=VaapiVideoDecoder"
    "--enable-features=VaapiVideoEncoder"
    "--enable-features=WebRTCPipeWireCapturer"
    "--flag-switches-end"
  ];
  vanillaDiscordPackage = pkgs.discord-canary.override {
    withOpenASAR = true;
    withVencord = true;
  };
  package = if cfg.armcord then pkgs.armcord else (if cfg.vesktop then pkgs.vesktop else vanillaDiscordPackage);
in {
  options.modules.software.distractions.discord = {
    enable = mkEnableOption "Enable discord, a social messaging app";
    armcord = mkEnableOption "Use Armcord, an alternative Electron client";
    vesktop = mkEnableOption "Use Vesktop, an alternative Electron client with vencord preinstalled";
  };

  config = mkIf cfg.enable {
    assertions = [
      {
        assertion = !(cfg.armcord && cfg.vesktop);
        message = "You must either enable Armcord or Vesktop, not both";
      }
    ];

    user.packages = [
      (package.overrideAttrs (old: {
        preInstall = ''
          gappsWrapperArgs+=("--add-flags" "${concatStringsSep " " flags}")
        '';
      }))
    ];
  };
}