#!/bin/bash

VERSION="1.1.0"

type=$1
title=$2
text=$3
label=${4//_/__} # "Escape" underscores}
timeout=$5
init=$6
ADDITIONAL_ARGS=7

function help {
  (
    echo "Usage: oidc-prompt TITLE TEXT LABEL [INIT] [LIST_ELEMENTS ...]"
    echo "oidc-prompt -- An interface for prompting the user."
    echo
    echo "This tool is intended as a internal tool of oidc-agent. Different
    oidc-agent components use it to prompt the user for information. The user
    should not call oidc-prompt."
  )>&2
}

case "$1" in
    "-?"|-h|--help)
      usage
      help
      exit
      ;;
    --usage)
      help
      exit
      ;;
    -V|--version)
      echo "oidc-prompt $VERSION"
      exit
      ;;
esac

if [ $# -le 2 ]; then
  help
  exit
fi

# To use another dialog creation tool, create a shell script that provides the
# needed functions and include it here.


function password {
    yad --no-markup --title "$title" --center --skip-taskbar --splash --text "$text" --button gtk-cancel:1 --button gtk-ok:0 --image dialog-password --entry --entry-label "$label" --entry-text "$init" --hide-text --completion --timeout $timeout --timeout-indicator bottom
    exit $?
}

function input {
    yad --no-markup --title "$title" --center --skip-taskbar --splash --text "$text" --button gtk-cancel:1 --button gtk-ok:0 --image dialog-question --entry --entry-label "$label" --entry-text "$init" --completion --timeout $timeout --timeout-indicator bottom
    exit $?
}

function confirm_default_no {
    yad --no-markup --title "$title" --center --skip-taskbar --splash --text "$text" --button gtk-no:1 --button gtk-yes:0 --image dialog-question --timeout $timeout --timeout-indicator bottom
    ret=$?
    if [ $ret == 0 ]; then
      echo "yes"
    fi
    exit $ret
}

function confirm_default_yes {
    yad --no-markup --title "$title" --center --skip-taskbar --splash --text "$text" --button gtk-yes:0 --button gtk-no:1 --image dialog-question --timeout $timeout --timeout-indicator bottom
    ret=$?
    if [ $ret == 0 ]; then
      echo "yes"
    fi
    exit $ret
}

function _select {
    h=$((125 + 22 * $#))
    yad --no-markup --title "$title" --width 400 --height $h --center --skip-taskbar --splash --text "$text" --button gtk-cancel:1 --button gtk-ok:0 --timeout $timeout --timeout-indicator bottom --image dialog-question --list --separator "" --search-column 1 --regex-search --column "$label" "$init" "${@}"
}

function select2 {
    if [ "$type" == "select-other" ]; then
      out=$(_select ${@} "Other")
    else
      out=$(_select ${@})
    fi
    ret=$?
    out=${out//\\n/}
    if [ $ret != 0 ]; then
      exit $ret
    fi
    if [ "$out" != "Other" ]; then
      echo $out
      exit $ret
    else
      text=${text//Select/Enter}
      text=${text//select/enter}
      input
    fi
}

function multiple {
   printf '%s\n' "${@}" | head -c -1 | yad --no-markup --title "$title" --width 400 --height 222 --center --skip-taskbar --splash --text "$text" --button gtk-cancel:1 --button gtk-ok:0 --timeout $timeout --timeout-indicator bottom --image dialog-question --label "$label" --text-info --editable | grep -v '^$'
}

function simple_link_QR {
  echo "$text" | yad --no-markup --title "$title" --width 600 --height 350 --center --skip-taskbar --splash --button gtk-ok:0 --image "$init" --wrap --show-uri --text-info --timeout $timeout --timeout-indicator bottom
}

function complex_link {
  # The option --wrap would be nice, but it wraps the long auth url really ugly
  # The option --show-uri would be nice (it makes uris clickable), but it doesn't work with query parameters.
  echo "$text" | yad --no-markup --title "$title" --width 800 --height 222 --center --skip-taskbar --splash --button gtk-ok:0 --text-info --timeout $timeout --timeout-indicator bottom
}

case $type in
  "password")
    password
    ;;

  "input")
    input
    ;;

  "confirm-default-no")
    confirm_default_no
   ;;
  "confirm-default-yes")
    confirm_default_yes
    ;;
  "confirm")
    confirm_default_yes
    ;;

  select*)
    select2 "${@:$ADDITIONAL_ARGS}"
    ;;

  "multiple")
    multiple "$init" "${@:$ADDITIONAL_ARGS}"
    ;;
  "complex-link")
    complex_link
    ;;
  "simple-link")
    simple_link_QR
    ;;

  *)
    >&2 echo "unknown type"
    exit 1
    ;;
esac
