
{ lib, config, pkgs, ... }:
with lib;
let
  cfg = config.modules.cli.tmux;
in
{
  options.modules.cli.tmux = {
    enable = mkEnableOption "enable Tmux";
  };
  config = mkIf cfg.enable {
    programs.tmux = {
      enable = true;
      aggressiveResize = true;
      baseIndex = 1;
      customPaneNavigationAndResize = false;
      escapeTime = 0;
      focusEvents = true;
      keyMode = "vi";
      mouse = true;
      prefix = "C-a";
      terminal = "tmux-256color";
      extraConfig = ''

        bind -n -N "Select pane to the left of the active pane" M-h select-pane -L
        bind -n -N "Select pane below the active pane" M-j select-pane -D
        bind -n -N "Select pane above the active pane" M-k select-pane -U
        bind -n -N "Select pane to the right of the active pane" M-l select-pane -R

        bind -n -r -N "Resize the pane left by 5" M-H resize-pane -L 5
        bind -n -r -N "Resize the pane down by 5" M-J resize-pane -D 5
        bind -n -r -N "Resize the pane up by 5" M-K resize-pane -U 5
        bind -n -r -N "Resize the pane right by 5" M-L resize-pane -R 5
        bind -n -n M-n split-window -h -c "#{pane_current_path}"
        bind -n -n M-N split-window -v -c "#{pane_current_path}"

        # Theme
        set -g status-interval 2
        setw -g automatic-rename on   # rename window to reflect current program
        set -g renumber-windows on    # renumber windows when a window is closed
        set -g set-titles on
        set -g mode-style bg=colour18,fg=colour7
        set -g set-titles-string "#T"
        set -g status-bg colour0
        set -g status-fg colour7
        set -g message-style bg=colour19,fg=colour7

        setw -g window-status-current-format '#[fg=colour18,bg=colour11] #I\
         #[bg=colour19,fg=colour7,bold] #W\
         #{?window_active,󰎂 ,}#{?window_marked_flag,󰃃 ,}#{?window_activity_flag, ,}#{?window_silence_flag,󰝟 ,}#{?window_zoomed_flag,󱀅 ,}#{?window_bell_flag,#[fg=colour1]󱈸 ,}'

        set -g pane-border-style fg=colour19
        set -g pane-active-border-style fg=colour4

        setw -g window-status-format '#[bg=color12,fg=colour19] #I\
         #[bg=colour18,fg=colour7,dim] #W\
         #{?window_last_flag, ,}#{?window_marked_flag,󰃃 ,}#{?window_activity_flag, ,}#{?window_silence_flag,󰝟 ,}#{?window_zoomed_flag,󱀅 ,}#{?window_bell_flag,#[fg=colour1]󱈸 ,}'
        set -g status-left-length 100
        set -g status-left '#[bg=colour0, fg=colour6]  #S '
        set -g status-right ' '
      '';
      plugins = with pkgs; [
        tmuxPlugins.tmux-fzf
      ];
    };
  };
}
