{ lib, pkgs, ... }:

with import ../theme; let
  themeFolder = ".local/share/fcitx5/themes/${theme}/";
in
{
  i18n.inputMethod = {
    enabled = "fcitx5";
    fcitx5.addons = with pkgs; [
      (fcitx5-anthy.overrideAttrs (oldAttrs: {
        # bugged custom Nicola tables fixed in master
        # https://github.com/fcitx/fcitx5-anthy/issues/8
        src = fetchFromGitHub {
          owner = "fcitx";
          repo = "fcitx5-anthy";
          rev = "9031b56c334dd0a2da6a5b5d3ab3570e06395ca0";
          hash = "sha256-a0z3OoSiiVslrTZuI5PDws+ykWFFKdev7fSpGgjNAA8=";
        };
      }))
    ];
  };

  home.file = {
    "${themeFolder}theme.conf".source = with colors; pkgs.substituteAll ({
      src = ./theme.conf;
      background = "${bg1}80";
      highlight = bg0;
    } // colors);
    "${themeFolder}arrow.png".source = ./arrow.png;
    "${themeFolder}radio.png".source = ./radio.png;
    ".config/fcitx5/conf/classicui.conf".text = "Theme=${theme}";
  };
  # Generating patch:
  # $ source="$(find /nix/store -maxdepth 1 -type d -name '*fcitx5-anthy*' | head -n 1)/share/fcitx5/anthy/nicola-j.sty"
  # $ cp $source nicola.sty
  # $ chmod 644 nicola.sty
  # $ vim nicola.sty
  # $ diff $source nicola.sty > nicola.patch
  home.activation.nicola = let
    targetDir = ".local/share/fcitx5/anthy/";
    target = "nicola.sty";
  in lib.hm.dag.entryAfter["writeBoundary"] ''
    mkdir -p ${targetDir}
    rm -f -- ${target}
    patch -ruN ${pkgs.fcitx5-anthy}/share/fcitx5/anthy/nicola-j.sty ${./nicola.patch} -o ${targetDir}${target}
  '';
}
