#!/usr/bin/env sh

#// set variables
scrDir=$(dirname "$(realpath "$0")")
# source $scrDir/globalcontrol.sh
conf_path="$HOME/.config/rofi/cliphist.rasi"

#// set rofi scaling
[[ "${rofiScale}" =~ ^[0-9]+$ ]] || rofiScale=10
# r_scale="configuration {font: \"JetBrainsMono Nerd Font ${rofiScale}\";}"
wind_border=$((hypr_border * 3 / 2))
elem_border=$([ $hypr_border -eq 0 ] && echo "5" || echo $hypr_border)

#// evaluate spawn position
readarray -t curPos < <(hyprctl cursorpos -j | jq -r '.x,.y')
readarray -t monRes < <(hyprctl -j monitors | jq '.[] | select(.focused==true) | .width,.height,.scale,.x,.y')
readarray -t offRes < <(hyprctl -j monitors | jq -r '.[] | select(.focused==true).reserved | map(tostring) | join("\n")')
monRes[2]="$(echo "${monRes[2]}" | sed "s/\.//")"
# monRes[0]="$(( ${monRes[0]} * 100 / ${monRes[2]} ))" monRes[1]="$(( ${monRes[1]} * 100 / ${monRes[2]} ))"
# curPos[0]="$(( ${curPos[0]} - ${monRes[3]} ))"
# curPos[1]="$(( ${curPos[1]} - ${monRes[4]} ))"

# if [ "${curPos[0]}" -ge "$((${monRes[0]} / 2))" ] ; then
#     x_pos="east"
#     x_off="-$(( ${monRes[0]} - ${curPos[0]} - ${offRes[2]} ))"
# else
#     x_pos="west"
#     x_off="$(( ${curPos[0]} - ${offRes[0]} ))"
# fi
#
# if [ "${curPos[1]}" -ge "$((${monRes[1]} / 2))" ] ; then
#     y_pos="south"
#     y_off="-$(( ${monRes[1]} - ${curPos[1]} - ${offRes[3]} ))"
# else
#     y_pos="north"
#     y_off="$(( ${curPos[1]} - ${offRes[1]} ))"
# fi

# r_override="window{location:${x_pos} ${y_pos};anchor:${x_pos} ${y_pos};x-offset:${x_off}px;y-offset:${y_off}px;border:${hypr_width}px;border-radius:${wind_border}px;} wallbox{border-radius:${elem_border}px;} element{border-radius:${elem_border}px;}"

#// clipboard action
case "${1}" in
c|-c|--copy)
    cliphist list | rofi -dmenu -theme-str "entry { placeholder: \"Copy...\";}"  -theme-str "${r_override}" -config "${conf_path}" | cliphist decode | wl-copy
    sleep 0.1
    xdotool search --sync --onlyvisible --classname "rofi" windowactivate --sync %@
    ;;
d|-d|--delete)
    cliphist list | rofi -dmenu -theme-str "entry { placeholder: \"Delete...\";}" -theme-str "${r_scale}" -theme-str "${r_override}" -config "${roconf}" | cliphist delete
    sleep 0.1
    xdotool search --sync --onlyvisible --classname "rofi" windowactivate --sync %@
    ;;
w|-w|--wipe)
    if [ $(echo -e "Yes\nNo" | rofi -dmenu -theme-str "entry { placeholder: \"Clear Clipboard History?\";}" -theme-str "${r_scale}" -theme-str "${r_override}" -config "${roconf}") == "Yes" ] ; then
        cliphist wipe
    fi
    sleep 0.1
    xdotool search --sync --onlyvisible --classname "rofi" windowactivate --sync %@
    ;;
*)
    echo -e "cliphist.sh [action]"
    echo "c -c --copy    :  cliphist list and copy selected"
    echo "d -d --delete  :  cliphist list and delete selected"
    echo "w -w --wipe    :  cliphist wipe database"
    exit 1
    ;;
esac

