#!/bin/bash

# a menu of customized quick shortcuts

if ! [ -e ~/.config/instantos/quickmenu ]; then
    mkdir -p ~/.config/instantos/quickmenu
fi

cd ~/.config/instantos/quickmenu || exit 1

TERMINAL_CMD="$(command -v kitty 2>/dev/null || true)"

launch_terminal() {
    if [ -n "$TERMINAL_CMD" ]; then
        "$TERMINAL_CMD" "$@"
    else
        ins menu message "kitty terminal not found"
        return 1
    fi
}

pickscript() {
    unset CHOICE
    while [ -z "$CHOICE" ]; do
        if [ -z "$(ls)" ]; then
            CHOICE="$(echo 'edit menu
close' | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 15 \
                --width auto \
                --insensitive)"

        else
            CHOICE="$(ls -p | sed -e '$aedit menu\nclose' | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 15 \
                --width auto \
                --insensitive)"
        fi
        if [ -e "$CHOICE" ]; then
            if [ -d "$CHOICE" ]; then
                cd "$CHOICE" || exit 1
                unset CHOICE
            else
                realpath "$CHOICE"
            fi
        else
            if grep -Eiq '^(close|edit)' <<<"$CHOICE"; then
                echo "$CHOICE"
                pwd >/tmp/quickpath
            else
                return 1
            fi
        fi
    done
}

# create a new script with the given arguments
chooseaddmode() {
    ADDCHOICE="$(echo 'enter command
edit script
options
cancel' | ins menu choice)"

    if [ -z "$1" ]; then
        echo "#!/bin/bash
" | tee "$SCRIPTNAME"
    fi

    case "$ADDCHOICE" in
        enter*)
            COMMAND="$(ins menu input 'enter command')"
            echo "$COMMAND" >>"$SCRIPTNAME"
            ;;
        edit*)
            echo "editing script"
            echo "# instantOS quickmenu script" >>"$SCRIPTNAME"
            launch_terminal -e nvim -c ":e $(realpath "$SCRIPTNAME")"
            ;;
        options)
            echo "#!/bin/bash" >"$SCRIPTNAME"
            ADDTEMPLATE="$(echo 'run in terminal
enable argument dialog' | ins menu checklist)"
            if grep -q 'terminal' <<<"$ADDTEMPLATE"; then
                echo "enabling terminal"
                echo '#instantosterm' >>"$SCRIPTNAME"
            fi

            if grep -q 'argument' <<<"$ADDTEMPLATE"; then
                echo "enabling argument"
                echo '#instantosarg' >>"$SCRIPTNAME"
            fi
            chooseaddmode no
            ;;
        cancel)
            [ -e "$SCRIPTNAME" ] && rm "$SCRIPTNAME"
            exit
            ;;
    esac
}

addmenu() {
    cd "$(cat /tmp/quickpath)" || exit 1
    ADDCHOICE="$(echo 'add entry
edit entry
remove entry
rename entry
new category' | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 7 \
                --width auto \
                --insensitive)"
    if [ -z "$ADDCHOICE" ]; then
        echo "no addchoice"
        exit
    fi
    case "$ADDCHOICE" in
        remove*)
            echo "removing entry"
            REMOVECHOICE="$(ls -p | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 7 \
                --width auto \
                --insensitive)"
            if [ -d "$REMOVECHOICE" ] && [ -n "$(ls "$REMOVECHOICE/")" ]; then
                ins menu message "category is not empty"
                exit
            elif ! [ -e "$REMOVECHOICE" ]; then
                ins menu message "selection $REMOVECHOICE invalid"
            fi

            if ins menu confirm "remove $REMOVECHOICE"; then
                if [ -d "$REMOVECHOICE" ]; then
                    rm -rf "$REMOVECHOICE"
                else
                    rm "$REMOVECHOICE"
                fi
            fi

            ;;
        new*)
            CATEGORYNAME="$(ins menu input 'enter category name')"
            [ -n "$CATEGORYNAME" ] && mkdir "$CATEGORYNAME"
            ;;
        add*)
            echo "adding new entry"
            SCRIPTNAME="$(ins menu input 'enter name')"

            if [ -e "$SCRIPTNAME" ]; then
                ins menu message "script $SCRIPTNAME already existing"
                exit
            fi

            if [ -z "$SCRIPTNAME" ]; then
                exit
            fi

            touch "$SCRIPTNAME" || {
                ins menu message "invalid script name"
                exit
            }
            chooseaddmode
            ;;
        edit*)
            echo "editing existing entry"
            EDITCHOICE="$(ls -p | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 7 \
                --width auto \
                --insensitive)"
            [ -z "$EDITCHOICE" ] && exit
            if [ -d "$EDITCHOICE" ]; then
                ins menu message "selection is not a script"
                exit
            elif ! [ -e "$EDITCHOICE" ]; then
                ins menu message "selection $EDITCHOICE invalid"
                exit
            fi
            launch_terminal -e nvim -c ":e $(realpath "$EDITCHOICE")"
            ;;
        rename*)
            echo "renaming entry"
            EDITCHOICE="$(ls -p | instantmenu \
                --border-width 4 \
                --follow-cursor \
                --lines 7 \
                --width auto \
                --insensitive)"
            [ -z "$EDITCHOICE" ] && exit

            if ! [ -e "$EDITCHOICE" ]; then
                ins menu message "$EDITCHOICE does not exist"
            fi

            NEWNAME="$(ins menu input 'enter new name')"
            [ -z "$NEWNAME" ] && exit
            mv "$EDITCHOICE" "$NEWNAME"

            ;;
    esac
}

QUICKCHOICE="$(pickscript)"
echo "$QUICKCHOICE"

if [ -z "$QUICKCHOICE" ]; then
    exit
fi

case "$QUICKCHOICE" in
    close)
        exit
        ;;
    "edit menu")
        echo "editing menu"
        addmenu
        ;;
    *)
        if ! [ -e "$QUICKCHOICE" ]; then
            echo ""
            echo "choice $QUICKCHOICE not found"
            exit
        fi

        if grep -q "^#instantosarg" "$QUICKCHOICE"; then
            QUICKARGS="$(ins menu input options)"
            [ -z "$QUICKARGS" ] && exit
        fi

        if grep -q "^#instantosterm" "$QUICKCHOICE"; then
            chmod +x "$QUICKCHOICE"
            launch_terminal -e bash -c "$QUICKCHOICE $QUICKARGS"
        else
            bash "$QUICKCHOICE" "$QUICKARGS"
        fi

        ;;
esac
