inputs@{ machine, pkgs, ... }:
let
  kanata-config = ''
    (defcfg
      process-unmapped-keys yes
      danger-enable-cmd yes
    )

    (defsrc
      esc
      grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
      tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
      caps a    s    d    f    g    h    j    k    l    ;    '    ret
      lsft z    x    c    v    b    n    m    ,    .    /    rsft
      lctl lmet lalt           spc            ralt rctl
    )

    (deflayer qwerty
      @esc
      grv  1    2    3    4    5    6    7    8    9    0    -    =    bspc
      tab  q    w    e    r    t    y    u    i    o    p    [    ]    \
      @cap a    s    d    f    g    h    j    k    l    ;    '    ret
      lsft z    x    c    v    b    n    m    ,    .    /    rsft
      lctl @lmet lalt           spc            ralt @rctl
    )

    (deflayer control
      @esc
      grv  1       2      3        4     5    6    7    8    9    0    -    =    bspc
      tab  @mcleft @mup @mcright @replay t    y    u    i    o    p  @wup @wdown  \
      @cap @mleft @mdown @mright f       g  left  down  up  right ;    '    bspc
      lsft z    x   C-c   v   bspc  n  @macro ,    .   C-f    rsft
      lctl lmet lalt           spc            ralt @rctl
    )

    (deflayermap (other)
      w @mup
      a @mleft
      s @mdown
      d @mright

      q @mcleft
      e @mcright

      [ @wup
      ] @wdown
    )

    (defalias
      ;; hold esc
      esc (tap-hold 200 200 C-k (macro C-f esc))

      ;; control
      cap (tap-hold-release 200 200
        (multi dynamic-macro-record-stop esc)
        (layer-while-held control)
      )

      lmet (
        tap-hold-press 200 200
        (cmd ${pkgs.lib.getExe' pkgs.custom.niri "niri"} msg action toggle-overview)
        lmet
      )

      mup (movemouse-up 1 1)
      mleft (movemouse-left 1 1)
      mdown (movemouse-down 1 1)
      mright (movemouse-right 1 1)

      mcleft mlft
      mcright mrgt

      macro (dynamic-macro-record 0)
      replay (dynamic-macro-play 0)

      wup (mwheel-up 50 120)
      wdown (mwheel-down 50 120)

      ;; programming
      rctl (
        tap-hold-release
        200 200
        C-k (layer-while-held other)
      )
    )
  '';
in
{
  imports = machine.program {
    name = "kanata";
    inherit inputs;
    requirements = [ "graphical" ];
    home-config =
      { pkgs, ... }:
      {
        systemd.user.services.kanata = {
          Unit = {
            Description = "kanata";
          };

          Service = {
            Restart = "always";
            RestartSec = "3";
            ExecStart = "${pkgs.kanata-with-cmd}/bin/kanata --cfg ${pkgs.writeText "kanata.kbd" kanata-config}";
            Nice = "-20";
          };

          Install = {
            WantedBy = [ "default.target" ];
          };
        };

        home.file.kanata = {
          target = ".config/kanata/kanata.kbd";
          text = kanata-config;
        };
      };
  };

  # custom.program.kanata.system-config =
  #   { pkgs, ... }:
  #   {
  # sudo groupadd uinput
  # sudo usermod -aG input $USER
  # sudo usermod -aG uinput $USER
  # echo "KERNEL=="uinput", MODE="0660", GROUP="uinput", OPTIONS+="static_node=uinput"" >> /etc/udev/rules.d/99-input.rules
  # reboot or sudo udevadm control --reload-rules && sudo udevadm trigger
  # sudo modprobe uinput

  # };

}
