#! /usr/bin/env nix-shell
#! nix-shell -i bash -p bash gum trashy fd ripgrep mediainfo

replace_extension() {
    local file_basename
    file_basename=$(basename "$1")
    echo "${file_basename%.*}.$2"
}

convert_gif() {
    file_newname=$(replace_extension "$1" gif)
    ffpb -i "$(realpath "$1")" -vf fps=12,scale=480:-1,smartblur=ls=-0.5 "$file_newname"
}

convert_av1() {
    local file_newname
    file_newname=$(replace_extension "$1" mp4)
    local file_tempdest=/dev/shm/$file_newname
    local file_destination
    file_destination=$(dirname "$(realpath "$1")")/$file_newname
    ffpb -i "$1" \
        -c:v libaom-av1 \
        -threads 12 -cpu-used 7 \
        "$file_tempdest"
    trash "$1"
    mv -i "$file_tempdest" "$file_destination"
}

convert_mp4() {
    local file_newname
    file_newname=$(replace_extension "$1" mp4)
    local file_tempdest=/dev/shm/$file_newname
    local file_destination
    file_destination=$(dirname "$(realpath "$1")")/$file_newname
    ffpb -i "$1" \
        -c:v libx265 \
        -preset veryslow \
        "$file_tempdest"
    trash "$1"
    mv -i "$file_tempdest" "$file_destination"
}

convert_e265_nvenc() {
    local file_newname
    file_newname=$(replace_extension "$1" mp4)
    local file_tempdest=/dev/shm/$file_newname
    local file_destination
    file_destination=$(dirname "$(realpath "$1")")/$file_newname
    ffpb -i "$1" \
        -c:v hevc_nvenc \
        -preset p7 -rc vbr_hq -cq 19 -b:v 0 \
        -spatial_aq 1 -aq-strength 8 \
        "$file_tempdest"
    trash "$1"
    mv -i "$file_tempdest" "$file_destination"
}

convert_discord() {
    local file_newname
    file_newname=$2_$(replace_extension "$1" mp4)
    local dir_ram=/dev/shm/ffmpeg
    mkdir -p $dir_ram/{in,out}
    ffpb -hwaccel cuda -i "$(realpath "$1")" \
        -c:v h264_nvenc \
        "$dir_ram"/in/discord.mp4
    cd "$dir_ram" || exit
    codec=x264 audio_br=$3 fs=$4 reso=$5 ffmpeg4discord
    mv "$dir_ram"/out/small_discord.mp4 ~/"$file_newname"
    command rm -rf "$dir_ram"
}

operation=$(gum choose mp4 av1 discord nitro gif enc265 e265_nvidia)

case $operation in
1 | mp4)
    to_convert=()
    while IFS= read -r file; do
        to_convert+=("$file")
    done < <(fd . "$(pwd)" -tf -aL | fzf --multi -i)
    for file in "${to_convert[@]}"; do
        convert_mp4 "$file"
    done
    ;;
2 | av1)
    to_convert=()
    while IFS= read -r file; do
        to_convert+=("$file")
    done < <(fd . "$(pwd)" -tf -aL | fzf --multi -i)
    for file in "${to_convert[@]}"; do
        convert_av1 "$file"
    done
    ;;
3 | discord)
    to_convert=()
    while IFS= read -r file; do
        to_convert+=("$file")
    done < <(fd . "$(pwd)" -tf -aL | fzf --multi -i)
    for file in "${to_convert[@]}"; do
        convert_discord "$file" discord 96 8.0 "1280x720"
    done
    ;;
4 | nitro)
    to_convert=()
    while IFS= read -r file; do
        to_convert+=("$file")
    done < <(fd . "$(pwd)" -tf -aL | fzf --multi -i)
    for file in "${to_convert[@]}"; do
        convert_discord "$file" nitro 128 500.0 "1920x1080"
    done
    ;;
5 | gif)
    to_convert=()
    while IFS= read -r file; do
        to_convert+=("$file")
    done < <(fd . "$(pwd)" -tf -aL | fzf --multi -i)
    for file in "${to_convert[@]}"; do
        convert_gif "$file"
    done
    ;;
6 | enc265)
    to_convert=()
    extensions=(flv m4v mpg avi mov ts mkv mp4 webm)
    for ext in "${extensions[@]}"; do
        while IFS= read -r file; do
            if ! (mediainfo "$file" | grep Writing\ library | grep -q x265); then
                to_convert+=("$file")
            fi
        done < <(fd . -e "$ext" -tf -aL)
    done
    for file in "${to_convert[@]}"; do
        convert_mp4 "$file"
    done
    ;;
7 | e265_nvidia)
    to_convert=()
    extensions=(flv m4v mpg avi mov ts mkv mp4 webm)
    for ext in "${extensions[@]}"; do
        while IFS= read -r file; do
            if ! (mediainfo "$file" | grep Writing\ library | grep -q x265); then
                to_convert+=("$file")
            fi
        done < <(fd . -e "$ext" -tf -aL)
    done
    for file in "${to_convert[@]}"; do
        convert_e265_nvenc "$file"
    done
    ;;
*)
    echo -n "Please select a valid input"
    ;;
esac
