#!/bin/bash

# desc: Permite configurar el modo de bloqueo mediante un yad gráfico
# lastact: arturo@2022-5-12

# --column=STRING[:TYPE]
# Set the column header. Types are TEXT, NUM, SZ, FLT, CHK, RD, BAR, IMG, HD or TIP.  TEXT type is default. Use NUM for integers and FLT for double  values.  TIP  is
# used  for  define  tooltip  column.  SZ size column type. Works exactly like NUM column but shows human readable sizes instead of numbers.  CHK (checkboxes) and RD
# (radio toggle) are a boolean columns.  BAR is a progress bar column. Value must be between 0 and 100. If value is outside is range it  will  be  croped  to  neares
# legal  value.   HD type means a hidden column. Such columns are not displayes in the list, only in output.  IMG may be path to image or icon name from currnet GTK+
# icon theme.  Size of icons may be set in gtk config file at GTK_ICON_SIZE_MENU position of gtk-icon-sizes. Image field prints as empty value.

# Averiguamos el tipo de filtrado que tiene actualmente:
OPCION_CONFIGURADA="$(vx-blockhosts-checkoption)"

TITULO="Gestión de Bloqueo de URLs"
ICONO="vx-blockhosts-nube"
TEXTO="\nIndica que tipo de URLs o sitios Webs quieres bloquear por defecto:"
TEXTO+="\n[Actualmente tienes configurada la <b>Opción ${OPCION_CONFIGURADA//:*}</b>]\n\n"
OPCIONES=(
    "0" "Sin filtrado, permitir acceso a cualquier tipo de Web"
    "1" "Feaknews"
    "2" "Porno"
    "3" "Social (redes sociales)"
    "4" "Gambling (juegos online)"
    "5" "Fakenews, Gambling, Porno"
    "6" "Fakenews, Porno, Social"
    "7" "Gambling, Porno, Social"
    "8" "Todo => Feaknews, Gambling, Porno y Social (opción por defecto)"
)
VENTANA="yad --title \"${TITULO}\" \
--center \
--window-icon \"${ICONO}\" \
--image \"${ICONO}\" \
--height \"370\" --width \"720\" \
--justify=\"center\" --text-align=\"center\" \
--text \"${TEXTO}\" \
--list --radiolist \
--column \"Elección\":CHK \
--column \"Opción\" \
--column \"Descripción\""
CONT=0
NUM="${#OPCIONES[@]}"
while (( CONT < NUM )) ; do
    VENTANA+=" FALSE \"${OPCIONES[${CONT}]}\" \"${OPCIONES[$((CONT + 1))]}\""
    CONT=$(( CONT + 2 ))
done
VENTANA+=" --buttons-layout center \
--button \"Aplicar\":0 \
--button \"Cancelar\":1"

if RESULTADO="$(eval "${VENTANA}")" ; then
    # El yad anterior devuelve tantas líneas como CHK han sido marcados (caso del checklist)
    # El formato de cada linea devuelta es el siguiente:
    # TRUE|campo2|campo3
    OPCION="$(echo "${RESULTADO}" | cut -d"|" -f2)"
    echo "=> Opción escogida: ${OPCION}"
    vx-blockhosts "${OPCION}"
fi

exit 0