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

with lib;

let
  cfg = config.home.roles.tmux;

  tokyo-night = pkgs.tmuxPlugins.mkTmuxPlugin {
    pluginName = "tokyo-night";
    rtpFilePath = "tokyo-night.tmux";
    version = "1.6.1";
    src = pkgs.fetchFromGitHub {
      owner = "janoamaral";
      repo = "tokyo-night-tmux";
      rev = "d610ced20d5f602a7995854931440e4a1e0ab780";
      sha256 = "sha256-17vEgkL7C51p/l5gpT9dkOy0bY9n8l0/LV51mR1k+V8=";
    };
  };
in
{
  options.home.roles.tmux = {
    enable = mkEnableOption "tmux terminal multiplexer with Tokyo Night theme";
  };

  config = mkIf cfg.enable {
    programs.tmux.enable = true;
    programs.tmux.terminal = "tmux-direct";
    programs.tmux.keyMode = "vi";
    programs.tmux.escapeTime = 0;
    programs.tmux.mouse = true;
    programs.tmux.newSession = true;
    programs.tmux.historyLimit = 50000;
    programs.tmux.clock24 = true;
    programs.tmux.baseIndex = 1;
    programs.tmux.prefix = "M-\\\\";

    programs.tmux.plugins = with pkgs; [
      tmuxPlugins.cpu
      tmuxPlugins.battery
      tmuxPlugins.better-mouse-mode
      tmuxPlugins.net-speed
      tmuxPlugins.online-status
      tmuxPlugins.pain-control
      tmuxPlugins.tilish
      tmuxPlugins.yank

      {
        plugin = tmuxPlugins.resurrect;
        extraConfig = "set -g @resurrect-strategy-nvim 'session'";
      }
      {
        plugin = tmuxPlugins.continuum;
        extraConfig = ''
          set -g @continuum-restore 'on'
          set -g @continuum-save-interval '15' # minutes
        '';
      }

      tokyo-night
    ];
  };
}
