# Enable mouse mode
set -g mouse on

# Smart pane switching with awareness of Vim splits.
# See: https://github.com/christoomey/vim-tmux-navigator

# decide whether we're in a Vim process
is_vim="ps -o state= -o comm= -t '#{pane_tty}' \
    | grep -iqE '^[^TXZ ]+ +(\\S+\\/)?g?(view|n?vim?x?)(diff)?$'"

bind-key -n 'C-h' if-shell "$is_vim" 'send-keys C-h' 'select-pane -L'
bind-key -n 'C-j' if-shell "$is_vim" 'send-keys C-j' 'select-pane -D'
bind-key -n 'C-k' if-shell "$is_vim" 'send-keys C-k' 'select-pane -U'
bind-key -n 'C-l' if-shell "$is_vim" 'send-keys C-l' 'select-pane -R'

tmux_version='$(tmux -V | sed -En "s/^tmux ([0-9]+(.[0-9]+)?).*/\1/p")'

if-shell -b '[ "$(echo "$tmux_version < 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\'  'select-pane -l'"
if-shell -b '[ "$(echo "$tmux_version >= 3.0" | bc)" = 1 ]' \
    "bind-key -n 'C-\\' if-shell \"$is_vim\" 'send-keys C-\\\\'  'select-pane -l'"

bind-key -n 'C-Space' if-shell "$is_vim" 'send-keys C-Space' 'select-pane -t:.+'

bind-key -T copy-mode-vi 'C-h' select-pane -L
bind-key -T copy-mode-vi 'C-j' select-pane -D
bind-key -T copy-mode-vi 'C-k' select-pane -U
bind-key -T copy-mode-vi 'C-l' select-pane -R
bind-key -T copy-mode-vi 'C-\' select-pane -l
bind-key -T copy-mode-vi 'C-Space' select-pane -t:.+

# List of plugins
set -g @plugin 'tmux-plugins/tpm'
set -g @plugin 'tmux-plugins/tmux-sensible'
set -g @plugin 'tmux-plugins/tmux-cpu'
set -g @plugin 'tmux-plugins/tmux-battery'
set -g @plugin 'tmux-plugins/tmux-resurrect'
set -g @plugin 'xamut/tmux-weather'
set -g @plugin 'jdx/tmux-cpu-info'
set -g @plugin 'catppuccin/tmux'
set -g @plugin 'Alkindi42/tmux-bitwarden'
set -g @catppuccin_flavour 'mocha' # latte,frappe, macchiato or mocha

# Options to make tmux more pleasant
set -g mouse on
set -g default-terminal "xterm-256color"
set -as terminal-features ",wezterm*:RGB"
set -g history-limit 20000

# Bitwarden
#set -g @bw-session 'BW_SESSION'
set -g @bw-copy-to-clipboard 'on'

# Clipboard integration
bind-key -T copy-mode-vi y send-keys -X copy-pipe-and-cancel "wl-copy && wl-paste -n | wl-copy -p"
bind-key p run "wl-paste -n | tmux load-buffer - ; tmux paste-buffer"

# Easy splits
bind-key "|" split-window -h -c "#{pane_current_path}"
bind-key "\\" split-window -fh -c "#{pane_current_path}"

bind-key "-" split-window -v -c "#{pane_current_path}"
bind-key "_" split-window -fv -c "#{pane_current_path}"

bind -r "<" swap-window -d -t -1
bind -r ">" swap-window -d -t +1

# Mouse scroll
set -g mouse on
bind -n WheelUpPane {
    if -F '#{==:#{window_name},nvim}' {
        send-keys -M 
    } {
        copy-mode -e 
    }
}

### tmux-resurrect ###
# Restore vim/nvim sessions
set -g @resurrect-strategy-vim 'session'
set -g @resurrect-strategy-nvim 'session'
# Capture pane contents
set -g @resurrect-capture-pane-contents 'on'

# Apply theme
set -g @catppuccin_window_status_style "rounded"
set -g @catppuccin_window_number_position "right"

set -g @catppuccin_window_default_fill "all"
set -g @catppuccin_window_default_text "#W"

set -g @catppuccin_window_current_fill "all"
set -g @catppuccin_window_current_text "#W"

set -g @catppuccin_status_left_separator  " "
set -g @catppuccin_status_right_separator ""
set -g @catppuccin_status_fill "all"
set -g @catppuccin_status_connect_separator "no"

set -g @catppuccin_directory_text "#{pane_current_path}"

run '~/.config/tmux/plugins/catppuccin/tmux/catppuccin.tmux'

set -g status-left-length 420
set -g status-right-length 420

# Run catppuccin plugin manually or through tpm
# ...

# set -g status-left ""
set -g status-left '#(tmux-cpu-info)'
set -g  status-right "#{E:@catppuccin_status_directory}"
set -ag status-right "#{E:@catppuccin_status_user}"
set -ag status-right "#{E:@catppuccin_status_host}"
set -ag status-right "#{E:@catppuccin_status_session}"

# Allow passthrough for neovim
set-option -g allow-passthrough on

# Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf)
run '~/.config/tmux/plugins/tpm/tpm'

