# =============================================
# Tmux Configuration
# =============================================

# -----------------------------------------
# Core Settings (Fixing Pi's Warning)
# -----------------------------------------

# Enable extended keys for modified Enter keys to work properly
set -g extended-keys on

# Format for extended keys (csi-u required for Pi)
set -g extended-keys-format csi-u

# Enable true color support
set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"

# =============================================
# Theme Colors (generated by matugen)
# =============================================
source-file ~/.config/tmux/colors.conf

# Faster escape time (for vim/neovim)
set -sg escape-time 10

# Increase scrollback buffer size
set -g history-limit 50000

# Enable mouse support (scroll, select, resize)
set -g mouse on

# Start window and pane numbering at 1 (easier to reach)
set -g base-index 1
setw -g pane-base-index 1

# Renumber windows when one is closed
set -g renumber-windows on

# Allow focus events (for vim/neovim)
set -g focus-events on

# -----------------------------------------
# Prefix Key (Change from Ctrl+b to Ctrl+s)
# -----------------------------------------

unbind C-b
set -g prefix C-s
bind C-s send-prefix

# -----------------------------------------
# Window Management
# -----------------------------------------

# Create new window
bind c new-window -c "#{pane_current_path}"

# Split panes with | and - (more intuitive)
bind | split-window -h -c "#{pane_current_path}"
bind - split-window -v -c "#{pane_current_path}"
unbind '"'
unbind %

# Quick window switching with prefix + number
bind -r 1 select-window -t 1
bind -r 2 select-window -t 2
bind -r 3 select-window -t 3
bind -r 4 select-window -t 4
bind -r 5 select-window -t 5
bind -r 6 select-window -t 6
bind -r 7 select-window -t 7
bind -r 8 select-window -t 8
bind -r 9 select-window -t 9

# Swap windows left/right
bind -r < swap-window -t -1
bind -r > swap-window -t +1

# -----------------------------------------
# Pane Navigation (Vim-style)
# -----------------------------------------

# Move between panes with vim keys
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R

# Zellij-style: Alt+arrows to navigate panes (no prefix!)
bind -n M-Left select-pane -L
bind -n M-Down select-pane -D
bind -n M-Up select-pane -U
bind -n M-Right select-pane -R

# Resize panes - Ctrl+S then r, then arrows
bind r switch-client -T resize-pane
bind -T resize-pane Left resize-pane -L 5 \; switch-client -T resize-pane
bind -T resize-pane Right resize-pane -R 5 \; switch-client -T resize-pane
bind -T resize-pane Up resize-pane -U 5 \; switch-client -T resize-pane
bind -T resize-pane Down resize-pane -D 5 \; switch-client -T resize-pane
bind -T resize-pane Escape switch-client -T root
bind -T resize-pane Enter switch-client -T root

# -----------------------------------------
# Moving Panes (Zellij-like)
# -----------------------------------------

# Break pane into new window (pop out)
bind b break-pane

# Join pane from another window (bring in)
bind j command-prompt -p "join pane from window #:" "join-pane -s '%%'"

# Swap panes with directional keys
bind > swap-pane -D
bind < swap-pane -U

# Move panes around - Ctrl+s m then arrows
bind m switch-client -T move-pane
bind -T move-pane Left swap-pane -s '{left-of}' \; switch-client -T move-pane
bind -T move-pane Right swap-pane -s '{right-of}' \; switch-client -T move-pane
bind -T move-pane Up swap-pane -s '{up-of}' \; switch-client -T move-pane
bind -T move-pane Down swap-pane -s '{down-of}' \; switch-client -T move-pane
bind -T move-pane Escape switch-client -T root
bind -T move-pane Enter switch-client -T root

# Mark pane + swap with marked pane (advanced)
bind M select-pane -m  # Mark current pane (capital M)
bind * swap-pane       # Swap with marked pane

# Move pane to another window by number
bind @ command-prompt -p "send pane to window #:" "join-pane -t ':%%'"

# Alt+Shift+Left/Right for previous/next window (Zellij-style tabs)
bind -n M-S-Left previous-window
bind -n M-S-Right next-window

# -----------------------------------------
# Layouts (Stacking-like)
# -----------------------------------------

# Cycle through layouts: even-horizontal, even-vertical, main-horizontal, main-vertical, tiled
bind Space next-layout
bind BSpace previous-layout

# Quick layout shortcuts
bind Enter select-layout tiled

# Easy horizontal/vertical layouts
bind = select-layout even-horizontal
bind + select-layout even-vertical

# Toggle between horizontal and vertical
bind t run-shell "tmux list-windows -F '#{window_layout}' | head -1 | grep -q 'even-horizontal' && tmux select-layout even-vertical || tmux select-layout even-horizontal"

# Other layouts - Ctrl+L then h/v (no prefix)
bind -n C-l switch-client -T layout
bind -T layout h select-layout main-horizontal \; switch-client -T root
bind -T layout v select-layout main-vertical \; switch-client -T root

# Note: Pane resize with H/J/K/L is defined in the resize section above

# -----------------------------------------
# Copy Mode (Vim-style)
# -----------------------------------------

setw -g mode-keys vi

# Enter copy mode with prefix + [
bind [ copy-mode

# v to start selection (like vim)
bind -T copy-mode-vi v send -X begin-selection

# y to yank (copy)
bind -T copy-mode-vi y send -X copy-selection-and-cancel

# Escape to exit copy mode
bind -T copy-mode-vi Escape send -X cancel

# -----------------------------------------
# Session Management
# -----------------------------------------

# Prefix + d to detach from session (keeps it running)
# Reattach with: tmux attach

# Prefix + s to list and switch sessions
bind s choose-session

# Prefix + $ to rename current session
bind $ command-prompt -I "#S" "rename-session '%%'"

# Prefix + , to rename current window
bind , command-prompt -I "#W" "rename-window '%%'"

# -----------------------------------------
# Status Bar
# -----------------------------------------

# Update status bar every second
set -g status-interval 1

# Position at top (for dual status bar with tabs on top, cheatsheet on bottom)
# Note: status-position bottom puts tabs at bottom-1, cheatsheet at very bottom

# Status bar sizing
set -g status-left-length 40
set -g status-right-length 100

# Note: Colors are defined in ~/.config/tmux/colors.conf (generated by matugen)
# The theme file sets: status-style, status-left, status-right, 
# window-status-format, window-status-current-format, pane borders, etc.

# -----------------------------------------
# Single Status Bar at Bottom
# -----------------------------------------
set -g status on
set -g status-position bottom
set -g status-interval 1
set -g status-left-length 40
set -g status-right-length 150



# -----------------------------------------
# Other Useful Bindings
# -----------------------------------------

# Prefix + . to reload config
bind . source-file ~/.config/tmux/tmux.conf \; display "Config reloaded!"

# Prefix + x to kill current pane (with confirmation)
bind x confirm-before -p "kill-pane #P? (y/n)" kill-pane

# Prefix + & to kill current window (with confirmation)
bind & confirm-before -p "kill-window #W? (y/n)" kill-window

# Synchronize all panes in current window (toggle)
bind S set synchronize-panes

# Zoom current pane (toggle full-screen for pane)
bind z resize-pane -Z

# -----------------------------------------
# Plugins (local dotfiles)
# -----------------------------------------

# Set plugin path to dotfiles location
set-environment -g TMUX_PLUGIN_MANAGER_PATH '~/.config/tmux/plugins/'

# Resurrect settings
set -g @resurrect-save 'S'
set -g @resurrect-restore 'L'
set -g @resurrect-capture-pane-contents 'on'
set -g @resurrect-strategy-nvim 'session'

# Continuum settings - auto-save every 15 minutes
set -g @continuum-save-interval '15'
set -g @continuum-restore 'on'

# Load plugins directly (bypass TPM)
run '~/.config/tmux/plugins/tmux-resurrect/resurrect.tmux'
run '~/.config/tmux/plugins/tmux-continuum/continuum.tmux'

# -----------------------------------------
# Notification Settings
# -----------------------------------------

# Enable activity monitoring
setw -g monitor-activity on
set -g visual-activity off

# Bell settings
set -g bell-action any
set -g visual-bell off
