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

let
  cfg = config.sydnix.users.crumb.mpd;
in {
  options.sydnix.users.crumb.mpd.enable =
    lib.mkEnableOption "MPD, à la crumb";

  config = lib.mkIf cfg.enable {
    sydnix.sops.secrets = {
      lastfm-password = {};
      librefm-password = {};
    };

    home.packages = [
      # An MPD client with a nice GUI.
      pkgs.cantata
    ];

    sydnix.impermanence.directories =
      let xdg-data-home =
        lib.removePrefix "${config.home.homeDirectory}"
          (lib.removePrefix "~"
            (config.xdg.dataHome));
      in [ "${xdg-data-home}/cantata" ];


    xdg.configFile."cantata/cantata.conf".text = ''
      [General]
      fetchCovers=true
      storeCoversInMpdDir=true
      showPopups=true
      storeCoversInMpdDir=true
      storeLyricsInMpdDir=true
      useOriginalYear=true

      [Connection]
      allowLocalStreaming=true
      applyReplayGain=true
      autoUpdate=false
      dir=~/Music/
      host=localhost
      partition=default
      passwd=
      port=6600
      streamUrl=
      
      [LibraryPage]
      artist\gridZoom=100
      artist\searchActive=false
      artist\viewMode=detailedtree
    '';
    
    sydnix.mpd = {
      enable = true;
      discord.enable = true;
      scrobbling = {
        enable = true;
        endpoints = {
          "last.fm" = {
            passwordFile =
              "/home/crumb/.config/sops-nix/secrets/lastfm-password";
            username = "crumb1";
          };
          "libre.fm" = {
            passwordFile =
              "/home/crumb/.config/sops-nix/secrets/librefm-password";
            username = "crumbtoo";
          };
        };
      };
    };
  };
}
