# Defualt Shell
set-option -g default-shell /bin/zsh

# Enable 256-color and true-color (24-bit) support in tmux
set -g default-terminal "screen-256color" # Set terminal type for 256-color support
set -ga terminal-overrides ",*256col*:Tc" # Override to enable true-color for compatible terminals

bind -n WheelUpPane if-shell -F -t = "#{mouse_any_flag}" "send-keys -M" "if -Ft= '#{pane_in_mode}' 'send-keys -M' 'copy-mode -e'"
setw -g mode-keys vi
bind -T copy-mode-vi WheelUpPane send -X scroll-up
bind -T copy-mode-vi WheelDownPane send -X scroll-down
#set-option -g aggressive-resize off

set -g mouse on
set -g set-clipboard on
set -g @yank_selection_mouse 'clipboard'
set -g @yank_selection 'clipboard'
set -g status-justify centre
set -g detach-on-destroy off
set -g status-interval 3
set -g allow-passthrough on

#Change Tmux keybindings
unbind C-b
set -g prefix C-Space
bind C-Space send-prefix

# Vim style pane selection
bind h select-pane -L
bind j select-pane -D 
bind k select-pane -U
bind l select-pane -R

# Start windows and panes at 1, not 0
set -g base-index 1
set -g pane-base-index 1
set-window-option -g pane-base-index 1
set-option -g renumber-windows on
set-option -g @ssh-split-h-key "v"
set-option -g @ssh-split-v-key 'h'
set-option -g @ssh-split-keep-cwd "true"
set-option -g @ssh-split-keep-remote-cwd "true"

# Resource Config
bind r source-file ~/.config/tmux/tmux.conf

# resize windows
bind -n C-M-h resize-pane -L 5
bind -n C-M-l resize-pane -R 5
bind -n C-M-k resize-pane -U 5
bind -n C-M-j resize-pane -D 5

# Shift arrow to switch windows
bind -n S-Left  previous-window
bind -n S-Right next-window

# Shift Alt vim keys to switch windows
bind -n M-H previous-window
bind -n M-L next-window

# Plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'christoomey/vim-tmux-navigator'
set -g @plugin 'catppuccin/tmux'
set -g @plugin 'tmux-plugins/tmux-yank'
set -g @plugin 'pschmitt/tmux-ssh-split'
set -g @plugin 'nhdaly/tmux-better-mouse-mode'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'alexwforsythe/tmux-which-key'
set -g @catppuccin_flavor 'mocha'
set -g @catppuccin_window_status_style "rounded"

set -g @catppuccin_window_text ' #W'
set -g @catppuccin_window_current_text ' #W'
set -g status-left "#[fg=#{@thm_crust},bg=#{@thm_mauve}] #S "
set -g status-right "#[fg=#{@thm_crust},bg=#{@thm_mauve}] #(git rev-parse --abbrev-ref HEAD) "

set -g window-status-format "##I"
set -g window-status-current-format "##I"

# Resurrect
set -g @resurrect-capture-pane-contents 'on'

# set vi-mode
set-window-option -g mode-keys vi
# keybindings
bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi C-v send-keys -X rectangle-toggle
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel "xsel -i --clipboard"

unbind '"'
unbind %
#bind '"' split-window -v -c "#{pane_current_path}"
#bind % split-window -h -c "#{pane_current_path}"
bind c new-window -c "#{pane_current_path}"

bind n run 'zsh -c ". $HOME/.config/scripts/sessions.sh new"'
bind s display-popup -E -w 50% -h 50% "zsh -c '. $HOME/.config/scripts/sessions.sh switch'"
bind d display-popup -E -w 50% -h 50% "zsh -c '. $HOME/.config/scripts/sessions.sh delete'"
bind w display-popup -E -w 50% -h 50% "zsh -c '. $HOME/.config/scripts/sessions.sh window'"

bind g display-popup -E -w 80% -h 75% "lazygit"
bind k display-popup -E -w 80% -h 75% "k9s"
bind f display-popup -E -w 80% -h 75% "yazi"

bind T if-shell -F '#{==:#{session_name},popup}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E ". $HOME/.config/scripts/sessions.sh popup"
}

bind S if-shell -F '#{==:#{session_name},scratch}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E ". $HOME/.config/scripts/sessions.sh scratch"
}

bind N if-shell -F '#{==:#{session_name},notes}' {
    detach-client
} {
    display-popup -d "#{pane_current_path}" -xC -yC -w 80% -h 75% -E ". $HOME/.config/scripts/sessions.sh notes"
}

bind m display-menu -x 20 -y 8 \
  "Even Vertical" 1 "select-layout even-vertical" \
  "Even Horizontal" 2 "select-layout even-horizontal" \
  "Tiled" 3 "select-layout tiled"

run '~/.tmux/plugins/tpm/tpm'
