# Kill process by port
killport () { lsof -ti ":$1" | xargs kill -9; }

# Rename tmux window after ssh session
ssh() {
    tmux rename-window "$*"
    command ssh "$@"
    tmux rename-window -t${TMUX_PANE} "`hostname -s`:$0"
    # exit
}

# Use fzf to find all your configured ssh connection
ssh-host() {
    if [ "$(which fzf 2>/dev/null)" ]; then
        ssh $(cat "$HOME/.ssh/config" "$HOME/.ssh/conf.d/individual_user" \
                | awk '$1 == "Host" && $2 != "*" {print $2}' |  fzf --reverse)
    else
        echo "fzf not installed!"
    fi
}

# Creates a new directory and immediately changes into it
mkcd() {
    mkdir -p "$1" && cd "$1"
}

# Extract compressed files easily
extract() {
    if [ -f "$1" ] ; then
        case "$1" in
            *.tar.bz2) tar xvjf "$1" ;;
            *.tar.gz) tar xvzf "$1" ;;
            *.tar.xz) tar Jxvf "$1" ;;
            *.bz2) bunzip2 "$1" ;;
            *.rar) unrar x "$1" ;;
            *.gz) gunzip "$1" ;;
            *.tar) tar xvf "$1" ;;
            *.tbz2) tar xvjf "$1" ;;
            *.tgz) tar xvzf "$1" ;;
            *.zip) unzip "$1" ;;
            *.Z) uncompress "$1" ;;
            *.7z) 7z x "$1" ;;
            *) echo "'$1' cannot be extracted via extract()" ;;
        esac
    else
        echo "'$1' is not a valid file"
    fi
}

# Show your current public IP address to the clipboard
showip() {
    curl -s http://ifconfig.me ; echo "" #| tr -d '\n' | pbcopy
    # echo "Public IP Address copied to clipboard!"
}

# Show the process behind a port
showport() {
    fuser -v -n tcp "$1"
}

# Get the weather forecast for a given location.
weather() {
    if [ -n "$1" ]; then
        curl v2.wttr.in/"$1"
    else
        curl v2.wttr.in
    fi
}

# Fetch common .gitignore templates and append them to the current .gitignore file
gitignore() {
    curl -sL "https://www.gitignore.io/api/$1" >> .gitignore
}

# A quick way to search for a file in the current directory.
qfind() {
    find . -name "$1"
}

# Go up a specified number of directories (or one level if no argument provided)
up() {
    local p=""
    for ((i=1; i<=$1; i++)); do
        p="../$p"
    done
    cd "$p" || return
}

# Get the current timestamp (useful for creating timestamped files)
timestamp() {
    date +%Y-%m-%d_%H-%M-%S
}

# Create a backup of a file or directory with a timestamp appended to its name
backup() {
    cp -r "$1" "$1_$(timestamp)"
}

# URL-encode a given string (useful for creating encoded URLs in scripts)
urlencode() {
    echo -n "$1" | xxd -plain | tr -d '\n' | sed 's/\(..\)/%\1/g'
}

# ---- Config ----
# Preferred networks (higher = more preferred)
declare -A WIFI_PRIORITY=(
    ["HomeWiFi"]=100
    ["WorkWiFi"]=90
    ["PhoneHotspot"]=80
)

# ---- Helpers ----
_wifi_scan() {
    nmcli -t -f IN-USE,SSID,SIGNAL,SECURITY dev wifi list \
    | sed '/^::/d' | sort -t: -k3 -nr
}

# ---- List ----
wifi_list() {
    nmcli -f IN-USE,SSID,SIGNAL,SECURITY dev wifi list
}

# ---- FZF Connect ----
wifi_show() {
    command -v fzf >/dev/null || { echo "fzf not installed"; return 1; }

    local choice ssid
    choice=$(_wifi_scan | fzf --delimiter=: --with-nth=2,3,4 --prompt="Select Wi-Fi: ")

    [ -z "$choice" ] && return 1
    ssid=$(echo "$choice" | cut -d: -f2)

    read -s -p "Password (leave empty if open): " password
    echo

    if [ -z "$password" ]; then
        nmcli dev wifi connect "$ssid"
    else
        nmcli dev wifi connect "$ssid" password "$password"
    fi
}
