#user-defined functions

hex_to_string() {
	for hex in $(printf '%s' "$1" | sed 's/../&\n/g'); do
		[ "$hex" = "00" ] && continue
		printf "\x$hex"
	done
	unset hex
}

gtp() {
	[ -z "$*" ] && commit=$(curl -s "https://raw.githubusercontent.com/ngerakines/commitment/master/commit_messages.txt" | shuf -n1) || commit=$*
	git add -p
	git commit -m "$commit"
	git push
	unset commit
}

megamind() {
	len=$(printf '%s' "$*" | wc -c)
	printf "———————————%s———————————
⠀⣞⢽⢪⢣⢣⢣⢫⡺⡵⣝⡮⣗⢷⢽⢽⢽⣮⡷⡽⣜⣜⢮⢺⣜⢷⢽⢝⡽⣝
⠸⡸⠜⠕⠕⠁⢁⢇⢏⢽⢺⣪⡳⡝⣎⣏⢯⢞⡿⣟⣷⣳⢯⡷⣽⢽⢯⣳⣫⠇
⠀⠀⢀⢀⢄⢬⢪⡪⡎⣆⡈⠚⠜⠕⠇⠗⠝⢕⢯⢫⣞⣯⣿⣻⡽⣏⢗⣗⠏⠀
⠀⠪⡪⡪⣪⢪⢺⢸⢢⢓⢆⢤⢀⠀⠀⠀⠀⠈⢊⢞⡾⣿⡯⣏⢮⠷⠁⠀⠀
⠀⠀⠀⠈⠊⠆⡃⠕⢕⢇⢇⢇⢇⢇⢏⢎⢎⢆⢄⠀⢑⣽⣿⢝⠲⠉⠀⠀⠀⠀
⠀⠀⠀⠀⠀⡿⠂⠠⠀⡇⢇⠕⢈⣀⠀⠁⠡⠣⡣⡫⣂⣿⠯⢪⠰⠂⠀⠀⠀⠀
⠀⠀⠀⠀⡦⡙⡂⢀⢤⢣⠣⡈⣾⡃⠠⠄⠀⡄⢱⣌⣶⢏⢊⠂⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⢝⡲⣜⡮⡏⢎⢌⢂⠙⠢⠐⢀⢘⢵⣽⣿⡿⠁⠁⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠨⣺⡺⡕⡕⡱⡑⡆⡕⡅⡕⡜⡼⢽⡻⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⣼⣳⣫⣾⣵⣗⡵⡱⡡⢣⢑⢕⢜⢕⡝⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⣴⣿⣾⣿⣿⣿⡿⡽⡑⢌⠪⡢⡣⣣⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⡟⡾⣿⢿⢿⢵⣽⣾⣼⣘⢸⢸⣞⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠁⠇⠡⠩⡫⢿⣝⡻⡮⣒⢽⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
——————————————————————" "$*"
	for i in $(seq $len); do
		printf '—'
	done
	printf '\n'
	unset len
}
gtb() {
	[ -z "$*" ] && br=$(git branch -a | fzf --border=rounded --layout=reverse --height=10 | tr -d ' ') || br=$*
	[ -z "$br" ] || git checkout $br
	unset br
}

b64() { 
	# base64 wrapper , also converts url base64
	printf "%s" "$1" | tr "._-" '=/+' | base64 -w 0 $2
}

mpv_jellyfin() {
	url=${1:-$(wl-paste)}
	uuid=$(printf '%s' "$url" | sed -nE 's|.*/Items/([^/]*)/Download.*|\1|p')
	key=$(printf '%s' "$url" | sed -nE 's|.*api_key=([^&]*)|\1|p')
	formatted_uuid=$(printf '%s' "$uuid" | sed -E 's/(.{8})(.{4})(.{4})(.{4})(.{12})/\1-\2-\3-\4-\5/')
	subtitle="$(printf '%s' "$url" | sed -nE 's|(.*)/Items/.*|\1|p')/Videos/$formatted_uuid/$uuid/Subtitles/0/0/Stream.ass?api_key=$key"

	if ! curl -s "$subtitle" | grep -q "Error processing request.";then
		sub_arg="--sub-file=$subtitle"
	fi

	setsid -f mpv "$url" $sub_arg
	unset subtitle sub_arg uuid formatted_uuid key
}

mkvcinemas() {
	local base_url="https://mkvcinemas.tokyo"
	local agent="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"

	[ -z "$*" ] && printf "Search> " && read -r query || query="$*"
	
	local movie=$(curl -s "$base_url/wp-admin/admin-ajax.php" -X POST -d "s=$query&action=searchwp_live_search&swpengine=default&swpquery=$query" -e "$base_url" -H "X-Requested-With:XMLHttpRequest" -L -A "$agent" | sed -nE 's|.*url\(([^\)]*).*href=".*/([^"]*)/".*|\1\t\2|p' | fzf -0 --preview="img2sixel {1} -w 280" --with-nth 2.. --layout=reverse --height=20 --border --preview-window right:36%:wrap | cut -f2)
	
	[ -z "$movie" ] && printf "No Movie Found/Selected." && return 1
	
	local url=$(curl -s "$base_url/$movie" -e "$base_url" -LA "$agent" |  sed 's|<span>|\n|g' | sed -nE 's|.*title="([^"]*)" href="([^"]*)" target.*[>|;](.*)</a><.*|\2`[\3] \1|p;' | fzf -0 -d'`' --with-nth 2.. --layout=reverse --height=20 --border | cut -d'`' -f1)
	[ -z "$url" ] && printf "No Url Selected" && return 1
	xdg-open "$url"
}

url_oshi() {
	days=${2:-1}
	out=$(curl -k https://oshi.at -F shorturl=0 -F "f=@$1" -F "expire=$((days * 1440))") #1440 means 1 day duration
	[ -z "$out" ] && return 1
	printf "%s" "$out" | sed -nE 's|DL: (.*)|\1|p' | wl-copy && notify-send "Link copied to clipboard"
	wl-paste
	curr=$(date '+%s')
	#storing only long duration links
	[ -z "$2" ] && printf "%s\n%s" "$out" "$((curr + (86400 * days)))" | tr '\n' '>' | sed 's/>/ | /g' >>$HOME/.cache/oshi-urls

	#deleting file uploaded than nth day ago
	for i in $(cut -d'|' -f3 $HOME/.cache/oshi-urls | tr -d ' '); do
		[ "$curr" -ge "$i" ] && sed -i "/$i/d" $HOME/.cache/oshi-urls &
	done
	echo >>$HOME/.cache/oshi-urls
}

url_nextcloud() {
	[ ! -f "$HOME/.secrets" ] && notify-send "Credentials not found" -u critical && return 1
	source "$HOME"/.secrets && load_nextcloud_creds
	curl "$NC_HOST/remote.php/dav/files/${NC_USER}${NC_FOLDER_PATH}/$(basename "$1")" -b "$NC_COOKIES" -H "Requesttoken: $NC_TOKEN" -T "$1" || return 1

	#To enable sharing, the output will be in json containing share link
	curl -s -b "$NC_COOKIES" -H "Requesttoken: $NC_TOKEN" "$NC_HOST/ocs/v2.php/apps/files_sharing/api/v1/shares" -H 'content-type: application/json' -H 'accept: application/json, text/plain, */*' -H 'OCS-APIRequest: true' --data-raw "{\"path\":\"${NC_FOLDER_PATH}/$(basename "$1")\",\"shareType\":3,\"attributes\":\"[]\"}" | sed -nE 's|.*"url":"([^"]*)".*|\1|p' | sed 's/\\//g' | wl-copy && notify-send "Link copied to clipboard"
	unset NC_HOST NC_USER NC_FOLDER_PATH NC_TOKEN NC_COOKIES NC_FOLDER_ID
}

gtd() {
	preview="git diff $@ --color=always -- {-1}"
	file=$(git diff $@ --name-only --relative | fzf --ansi --preview $preview --preview-window right:65%:wrap -0)
	[ -n "$file" ] && nvim $file
	unset preview file
}

gtc() {
	[ -z "$1" ] && [ -p "/dev/stdin" ] && read -r query </dev/stdin || query=$1
	git clone "$query"
}

gtr() {
	[ -z "$1" ] && [ -p "/dev/stdin" ] && read -r query </dev/stdin || query=$1
	curl -s "https://api.github.com/users/$query/repos" | sed -nE 's|.*ssh_url": "([^"]*)".*|\1|p' | fzf --border --height=10 --layout=reverse -0
}

gtu() { [ -z "$1" ] || curl -s "https://api.github.com/search/users?q=$1" | sed -nE 's_.*login": "([^"]*)".*_\1_p' | fzf --layout=reverse --border --height=10 -0; }

clshist() {
	a=$(cat ~/.histfile | wc -l)
	[ "$a" -gt $HISTSIZE ] && sed -i "1,$((a - HISTSIZE))d" ~/.histfile
}

v() {
	[ -z "$*" ] && nvim -O $(fzf --preview 'bat --color=always --style=numbers --line-range=:500 {}' -m | tr '\n' ' ') || nvim -O $*
}

help() {
	"$@" --help 2>&1 | bat --plain --language=help
}

addpkg() {
	[ -z "$*" ] && printf "\033[1;31mPlease write the name of package (just some words)..\033[0m" && return 1
	paru -Ss "$*" | sed -nE 's|^([a-zA-Z]*)/([^ ]*).*|[\1] \2|p' | fzf --layout=reverse --border --preview 'paru -Si $(echo {} | cut -d" " -f2) | bat --language=yaml --color=always -pp' --preview-window right:65%:wrap -m | cut -d' ' -f2- | paru -S -
}

rmpkg() {
	paru -Qq | fzf --preview 'paru -Si {} | bat --language=yaml --color=always -pp' --preview-window right:65%:wrap -m | paru -Rcns -
}

# Lines configured by zsh-newuser-install
export MANPAGER="sh -c 'col -bx | bat -l man -p'"
export TZ="Asia/Kolkata"
alias cat="bat -pp --color=always"
alias open="xdg-open"
alias cp="cp -v"
alias lsblk="lsblk -o 'NAME,SIZE,FSAVAIL,FSUSED,FSUSE%,MOUNTPOINTS'"
alias art="php artisan"
alias rm="rm -v"
alias mv="mv -v"
alias pgrep="pgrep -af"
alias grep="grep --color=auto"
alias ncdu="ncdu --color dark"
alias ll="lsd --color=auto -alh"
alias ls="lsd --color=auto"
alias nchh="nvim ~/.config/hypr/hyprland.conf"
alias shfmt="shfmt -ci -d -w"
alias reboot_firmware="systemctl reboot --firmware-setup"

HISTFILE=~/.histfile
HISTSIZE=500
SAVEHIST=500
clshist
PROMPT_EOL_MARK=' ⏎ '
setopt autocd
bindkey -e
zstyle :compinstall filename "$0"
fpath=($HOME/.config/zsh/completions $fpath)
autoload -Uz compinit
compinit
zstyle ':completion:*' menu select
eval "$(starship init zsh)"
#plugins
[ -f "/usr/share/nvm/init-nvm.sh" ] && source /usr/share/nvm/init-nvm.sh
[ -f "/usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ] && source /usr/share/zsh/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
[ -f "/usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh" ] && source /usr/share/zsh/plugins/zsh-autosuggestions/zsh-autosuggestions.zsh
[ -f "/usr/share/zsh/plugins/fzf-tab-git/fzf-tab.zsh" ] && source /usr/share/zsh/plugins/fzf-tab-git/fzf-tab.zsh
