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

with lib;

let

  cfg = config.programs.cava;

  iniFmt = pkgs.formats.ini { };

in {
  meta.maintainers = [ maintainers.bddvlpr ];

  options.programs.cava = {
    enable = mkEnableOption "Cava audio visualizer";

    package = mkPackageOption pkgs "cava" { };

    settings = mkOption {
      type = iniFmt.type;
      default = { };
      example = literalExpression ''
        {
          general.framerate = 60;
          input.method = "alsa";
          smoothing.noise_reduction = 88;
          color = {
            background = "'#000000'";
            foreground = "'#FFFFFF'";
          };
        }
      '';
      description = ''
        Settings to be written to the Cava configuration file. See
        <https://github.com/karlstav/cava/blob/master/example_files/config> for
        all available options.
      '';
    };
  };

  config = mkIf cfg.enable {
    home.packages = [ cfg.package ];

    xdg.configFile."cava/config" = mkIf (cfg.settings != { }) {
      text = ''
        ; Generated by Home Manager

        ${generators.toINI { } cfg.settings}
      '';
    };
  };
}