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

let cfg = config.sydnix.users.crumb.fcitx5;
in {
  options.sydnix.users.crumb.fcitx5 = {
    enable = lib.mkEnableOption "fcitx5 input method";
  };

  config = lib.mkIf cfg.enable {
    i18n.inputMethod = {
      enable = true;
      type = "fcitx5";
      fcitx5 = {
        waylandFrontend = true;
        addons = [
          pkgs.fcitx5-gtk
          pkgs.fcitx5-table-other
          pkgs.kdePackages.fcitx5-chinese-addons
          pkgs.fcitx5-hangul
          inputs.syd-fcitx5-tables.packages.${system}.default
        ];
        settings = {
          # Bind QuickPhrase to `s-;`.
          addons."quickphrase".sections = {
            TriggerKey."0" = "Super+semicolon";
          };

          globalOptions = {
            # Disable shift to toggle.  I accidentally press it all the time.
            # 날 미치게 만들어요.
            Hotkey.AltTriggerKeys = "";
          };

          # Enable the Engish, math, and X-SAMPA keyboards.  This section is
          # easiest to edit if you first delete the Home-manager symlink at
          # ~/.config/fcitx5, configure Fcitx5 graphically via
          # fcitx5-configtool, then translate the config at
          # ~/.config/fcitx5/profile.
          inputMethod = {
            GroupOrder."0" = "Default";
            "Groups/0" = {
              Name = "Default";
              "Default Layout" = "us";
              DefaultIM = "sydmath";
            };
            "Groups/0/Items/0".Name = "keyboard-us";
            "Groups/0/Items/1".Name = "sydmath";
            "Groups/0/Items/2".Name = "hangul";
            "Groups/0/Items/3".Name = "ipa-x-sampa";
          };
        };
      };
    };
  };
}
