#!/bin/bash

# TODO Start Pyradio if not started
# TODO check if theme is writeable (users themes)
# TODO Save as new theme


me="colorizer-pyradio -s"

THEMERC=~/.config/pyradio/themes/mbcolors.pyradio-theme

case "$LANG" in
    pl*)
    COLORIZE_PYRADIO="Pokoloruj PyRadio"
    START_PYRADIO="Uruchom PyRadio"
    INFO="Info"
    INFO_HEAD="Jak działa automatyczna zmiana kolorów?"
    INFO_TXT="Automatyczna zmiana kolorów działa z motywem <b>mbcolors</b> (User theme).\nAby go ustawić:\n- użyj klawisza <b>t</b> - wybór motywów\n- zaznacz motyw <i>mbcolors</i> i naciśnij <b>spację</b> - (ustawia jako domyślny)\n- wciśnij <b>c</b> - aby włączyć śledzenie zmian w pliku motywu.\n\n"
    GENERATEFROMWP="Generuj z kolorów tapety..."
    LIGHTBG="Jasne tło"
    DARKBG="Ciemne tło"
    SETCOLORS="Ustaw kolory..."
    RESETTHEME="Resetuj motyw"
    EXPORTTHEME="Eksportuj obecne kolory jako nowy motyw"
    CONFIG_AND_DOCS="Konfiguracja i Dokumentacja"
    README="PyRadio CZYTAJTO"
    MANUAL="Manual PyRadio"
    EDITCONF="Edytuj plik konfiguracyjny"
    EDITSTATIONS="Edytuj listę stacji"
    CONFIGDIR="Katalog z konfiguracją"
    ;;
    *)
    COLORIZE_PYRADIO="Colorize PyRadio"
    START_PYRADIO="Start PyRadio"
    INFO="Info"
    INFO_HEAD="How it works?"
    INFO_TXT="Automatic theme generation works with <b>mbcolors</b> user theme.\n Configuration:/n- use <b>t</b> key to show theme selection dialog\n- navigate to <i>mbcolors</i> theme and press <b>c</b>\n\n"
    GENERATEFROMWP="Generate from wallpaper colors..."
    LIGHTBG="Light Background"
    DARKBG="Dark Background"
    SETCOLORS="Set colors individually..."
    RESETTHEME="Reset theme"
    EXPORTTHEME="Export current colors as new theme"
    CONFIG_AND_DOCS="Config and Docs"
    README="PyRadi README"
    MANUAL="PyRadio Manual"
    EDITCONF="Edit config file"
    EDITSTATIONS="Edit stations list"
    CONFIGDIR="Config Dir"
    ;;
esac

getvalues () {
read S SFG SBG<<< "$(grep '^Stations ' ${THEMERC})"
read AS1 AS2 ASFG<<< "$(grep '^Active Station ' ${THEMERC})"
read SB1 SB2 SBFG SBBG<<< "$(grep '^Status Bar ' ${THEMERC})"
read NC1 NC2 NCFG NCBG<<< "$(grep '^Normal Cursor ' ${THEMERC})"
read AC1 AC2 ACFG ACBG<<< "$(grep '^Active Cursor ' ${THEMERC})"
read EC1 EC2 ECFG ECBG<<< "$(grep '^Edit Cursor ' ${THEMERC})"
read EF1 EF2 EFFG<<< "$(grep '^Extra Func ' ${THEMERC})"
read PURL1 PURL2 PURLFG<<< "$(grep '^PyRadio URL ' ${THEMERC})"
read MB1 MB2 MBFG<<< "$(grep '^Messages Border ' ${THEMERC})"

}
    getvalues

    out+=("^sep($COLORIZE_PYRADIO)")
    out+=("$START_PYRADIO,terminator --geometry=420x440-20-20 -e pyradio")
    out+=("$INFO,notify-send.sh -i music '$INFO_HEAD' '$INFO_TXT' -t 20000")
    out+=("^sep($GENERATEFROMWP)")
    out+=("$LIGHTBG,w2theme pyradio light;$me")
    out+=("$DARKBG,w2theme pyradio dark;$me")
    out+=("^sep($SETCOLORS)")
    out+=("<tt><span weight='bold' bgcolor='$SBG' fgcolor='$SFG'>  AbCd   </span></tt>  Stations,^checkout(stations)")
    out2+=("^tag(stations)")
    out2+=("^sep(Stations)")
    out2+=("Stations Text,^pipe(mbclr '$SFG' prtctl stations_txt 'Stations Text' '$me')")
    out2+=("Stations Background,^pipe(mbclr '$SBG' prtctl stations_bg 'Stations Background' '$me')")
    
    out+=("<tt><span weight='bold' bgcolor='$SBG' fgcolor='$ASFG'>  AbCd   </span></tt>  Active Station (text),^pipe(mbclr '$ASFG' prtctl active_station ACTIVE_STATION '$me')")
    
    out+=("<tt><span weight='bold' bgcolor='$NCBG' fgcolor='$NCFG'>  AbCd   </span></tt>  Normal Cursor,^checkout(normalcursor)")
    out2+=("^tag(normalcursor)")
    out2+=("^sep(Normal Cursor)")
    out2+=("Normal Cursor Text,^pipe(mbclr '$NCFG' prtctl normal_cursor_txt 'Normal Cursor Text' '$me')")
    out2+=("Normal Cursor Background,^pipe(mbclr '$NCBG' prtctl normal_cursor_bg 'Normal Cursor Background' '$me')")
    
    out+=("<tt><span weight='bold' bgcolor='$ACBG' fgcolor='$ACFG'>  AbCd   </span></tt>  Active Cursor,^checkout(activecursor)")
    out2+=("^tag(activecursor)")
    out2+=("^sep(Active Cursor)")
    out2+=("Active Cursor Text,^pipe(mbclr '$ACFG' prtctl active_cursor_txt 'Active Cursor Text' '$me')")
    out2+=("Active Cursor Background,^pipe(mbclr '$ACBG' prtctl active_cursor_bg 'Active Cursor Background' '$me')")
    
    
    out+=("<tt><span weight='bold' bgcolor='$ECBG' fgcolor='$ECFG'>  AbCd   </span></tt>  Edit Cursor,^checkout(editcursor)")
    out2+=("^tag(editcursor)")
    out2+=("^sep(Edit Cursor)")
    out2+=("Edit Cursor Text,^pipe(mbclr '$ECFG' prtctl edit_cursor_txt 'Edit Cursor Text' '$me')")
    out2+=("Edit Cursor Background,^pipe(mbclr '$ECBG' prtctl edit_cursor_bg 'Edit Cursor Background' '$me')")
    
    out+=("<tt><span weight='bold' bgcolor='$SBBG' fgcolor='$SBFG'>  AbCd   </span></tt>  Status Bar,^checkout(statusbar)")
    out2+=("^tag(statusbar)")
    out2+=("^sep(Status Bar)")
    out2+=("Status Bar Text,^pipe(mbclr '$SBFG' prtctl status_bar_txt 'Status Bar Text' '$me')")
    out2+=("Status Bar Background,^pipe(mbclr '$SBBG' prtctl status_bar_bg 'Status Bar Background' '$me')")
    out+=("<tt><span weight='bold' bgcolor='$SBG' fgcolor='$EFFG'>  AbCd   </span></tt>  Extra Func,^pipe(mbclr '$EFFG' prtctl extra_func EXTRA_FUNC '$me')")
    out+=("<tt><span weight='bold' bgcolor='$SBG' fgcolor='$PURLFG'>  AbCd   </span></tt>  PyRadio URL,^pipe(mbclr '$PURLFG' prtctl pyradio_url 'PyRadio URL' '$me')")
    out+=("<tt><span weight='bold' bgcolor='$SBG' fgcolor='$MBFG'>  AbCd   </span></tt>  Messages Border,^pipe(mbclr '$MBFG' prtctl messages_border 'Messages Border' '$me')")
    out+=("^sep()")
    out+=("   $RESETTHEME,prtctl resettheme;$me")
    out+=("^sep()")
    out+=("$EXPORTTHEME,prtctl exp;$me")
    
    out+=("^sep($CONFIG_AND_DOCS)")
    #out+=("PyRadio README,cat /usr/share/doc/pyradio/README.html| yad --html --no-buttons --no-escape --height=800 --width=600")
    out+=("   $README,xdg-open /usr/share/doc/pyradio/README.html")
    out+=("   $MANUAL,^term(man pyradio)")
    out+=("^sep()")
    out+=("   $EDITCONF,xdg-open ~/.config/pyradio/config")
    out+=("   $EDITSTATIONS,xdg-open ~/.config/pyradio/stations.csv")
    out+=("^sep()")
    out+=("   $CONFIGDIR,^pipe(jgbrowser ~/.config/pyradio)")


### RUN
if [[ "$1" == "-s" ]]; then
    . /usr/share/mb-jgtools/pipemenu-standalone.cfg
    . $HOME/.config/mabox/mabox.conf
    
    CNF_DIR="$HOME/.config/colorizer"
    CNF_FILE="$CNF_DIR/colorizer.conf"
    source "$CNF_FILE"

    out+=("^sep()")
    out+=(" ⮜⮜⮜    Colorizer,colorizer -s")
    
    if [[ "$colorizer_size" == "full" ]];then
    eval $(xdotool getdisplaygeometry --shell)
    MENU_HEIGHT_MIN="$HEIGHT"
    MENU_HEIGHT_MAX=$MENU_HEIGHT_MIN
    MENU_RADIUS="0"
    menu_margin_x="0"
    else
    MENU_VALIGN="center"
    menu_margin_x=${submenu_spacing:-0}
    fi
    

    MENU_HALIGN="left"
    jgmenu_icon_size=0
    
    [ $(pidof picom) ] && MENU_RADIUS=$jgtools_radius
    [ -z $jgmenu_use_borders ] && menu_border=0
    JGWIDTH=$((jgtools_padding + 278 + jgtools_padding))
    MENU_PADDING_TOP=$((jgtools_padding + 158))
    
    #WALLPAPER
    read WALLPATH<<< $(grep file "$HOME"/.config/nitrogen/bg-saved.cfg | cut -d'=' -f2)
    WALLPAPER=${WALLPATH##*/}
    THUMBDIR="$HOME/.config/mabox/wallthumbs"
    mkdir -p ${THUMBDIR}
    THUMB="${THUMBDIR}/${WALLPAPER}.png"
    if [[ ! -f "$THUMB" ]]
        then
    convert ${WALLPATH} -resize 270x150^ -gravity center -extent 270x150 ${THUMB}
    fi
    
    mkconfigfile

cat <<EOF > ${MENU_ITEMS}
@icon,,$((jgtools_padding + 4)),$((jgtools_padding + 4)),270,150,4,left,top,,,${THUMB}
#@icon,,$((jgtools_padding + 4)),$((jgtools_padding + 4)),270,150,4,left,top,,,/usr/share/mabox-colorizer/img/colorizer-conky.png
@rect,,$((jgtools_padding + 144)),$((jgtools_padding + 122)),120,30,4,left,top,#FFFFFF 60,#222222 80,
@text,,$((jgtools_padding + 150)),$((jgtools_padding + 122)),120,30,0,left,top,#FFFFFF,${WINCLR}, <b>PyRadio</b>

$(printf '%s\n' "${out[@]}")
$(printf '%s\n' "${out2[@]}")
EOF

    jgmenu --config-file=${CONFIG_FILE} --csv-file=${MENU_ITEMS} 2>/dev/null
    exit 0
fi

printf '%s\n' "${out[@]}"
printf '%s\n' "${out2[@]}"