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

let cfg = config.sydnix.users.crumb.rnote;
in {
  options.sydnix.users.crumb.rnote = {
    enable = lib.mkEnableOption "Rnote";
  };

  config = lib.mkIf cfg.enable {
    home.packages = [
      pkgs.rnote
    ];

    # C.f. https://github.com/gepbird/dotfiles/blob/main/modules/rnote.nix.
    # Browse options with
    #   dconf read /com/github/flxzt/rnote/engine-config \
    #     | sed -e #  "s/'\(.*\)'/\1/" | jq
    dconf.settings."com/github/flxzt/rnote" =
      let
        colour = r: g: b: a: { inherit r g b a; };
        stylix = name: {
          r = builtins.fromJSON config.lib.stylix.colors."${name}-dec-r";
          g = builtins.fromJSON config.lib.stylix.colors."${name}-dec-g";
          b = builtins.fromJSON config.lib.stylix.colors."${name}-dec-b";
          a = 1.0;
        };
        weird-stylix = name: let c = stylix name;
                             in lib.gvariant.mkTuple [c.r c.g c.b c.a];
      in {
        active-stroke-color = weird-stylix "base06"; # Old White
        active-fill-color = weird-stylix "base06"; # Old White
        engine-config = builtins.toJSON {
          penholder.shortcuts = {
            stylus_secondary_button.change_pen_style = {
              style = "selector";
              mode = "toggle";
            };
            stylus_primary_button.change_pen_style = {
              style = "tools";
              mode = "toggle";
            };
          };
          document.background = {
            color = stylix "base00"; # Sumi Ink 1
            pattern_color =
              # Sumi Ink 2
              colour 0.1647058824 0.1647058824 0.2156862745 1.0;
            format = {
              show_borders = false;
              border_color = colour 0.0 0.0 0.0 0.0;
            };
          };
          pens_config = {
            brush_config = {
              stroke_color = stylix "base06"; # Old White
              fill_color = stylix "base06"; # Old White
            };
          };
        };
      };
  };
}
