#!/bin/bash

me="colorizer-conky -s"
CONKYDIR="$HOME/.config/conky"
BASECONKY="$CONKYDIR/sysinfo_mbcolor.conkyrc"
if [ ! -f "${BASECONKY}" ]; then
rsync -a /usr/share/mabox-colorizer/conky/* ${CONKYDIR}/
fi

getvalues () {
    read WINTRANS <<< "$(grep own_window_transparent ${BASECONKY} | cut -d'=' -f2 | cut -d"," -f1)"
    read FONTDEF <<< "$(grep "font .*=.*,$" ${BASECONKY} | cut -d'=' -f2,3 |cut -d"'" -f2)"
    FONT=${FONTDEF%:*}
    FONTSIZE=${FONTDEF#*=}
    read WINCLR <<< "$(grep own_window_colour ${BASECONKY} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $WINCLR =~ ^#.* ]] && : || WINCLR="#${WINCLR}"
    
    read CLR <<< "$(grep default_color ${BASECONKY} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR =~ ^#.* ]] && : || CLR="#${CLR}"
    read CLR0 <<< "$(grep color0 ${BASECONKY} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR0 =~ ^#.* ]] && : || CLR0="#${CLR0}"
    read CLR1 <<< "$(grep color1 ${BASECONKY} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR1 =~ ^#.* ]] && : || CLR1="#${CLR1}"
    read CLR2 <<< "$(grep color2 ${BASECONKY} | cut -d'=' -f2 | cut -d"'" -f2)"
    [[ $CLR2 =~ ^#.* ]] && : || CLR2="#${CLR2}"
    
    read BORD <<< "$(grep draw_borders ${BASECONKY} | cut -d'=' -f2 | cut -d"," -f1)"
    read STIP <<< "$(grep stippled_borders ${BASECONKY} | cut -d'=' -f2 | cut -d"," -f1)"
}
    
if pgrep -af "conky.*mbcolor" >/dev/null; then
    
    getvalues
    [[ "$WINTRANS" == false ]] && BG="bgcolor='$WINCLR'" || BG=""

    out+=("^sep(Colorize Conky)")
    out+=("^sep(<small><i>Global settings (apply to all)</i></small>)")
    out+=("<small><span bgcolor='$CLR'>   </span><span ${BG} fgcolor='$CLR'>  AbCd  </span></small>  Default color,^pipe(mbclr '$CLR' conkyctl default_color_all DEFAULT_COLOR '$me')")
    out+=("<small><span bgcolor='$CLR0'>   </span><span ${BG} fgcolor='$CLR0'>  AbCd  </span></small>  Color 0,^pipe(mbclr '$CLR0' conkyctl color0_all COLOR_0 '$me')")
    out+=("<small><span bgcolor='$CLR1'>   </span><span ${BG} fgcolor='$CLR1'>  AbCd  </span></small>  Color 1,^pipe(mbclr '$CLR1' conkyctl color1_all COLOR_1 '$me')")
    out+=("<small><span bgcolor='$CLR2'>   </span><span ${BG} fgcolor='$CLR2'>  AbCd  </span></small>  Color 2,^pipe(mbclr '$CLR2' conkyctl color2_all COLOR_2 '$me')")
    out+=("^sep()")
    if [ "$WINTRANS" == false ];then
        out+=("<small><span bgcolor='$WINCLR'>   </span><span ${BG} fgcolor='$WINCLR'>  AbCd  </span></small>  Background,^checkout(trans)")
    else
        out+=("<small><span>transparent </span></small>  Background,^checkout(trans)")
    fi
    out2+=("^tag(trans)")
    out2+=("^sep(TRANSPARENT?)")
    out2+=("Yes,conkyctl transparent_all true;$me")
    out2+=("No,conkyctl transparent_all false;$me")
    out2+=("^sep()")
    out2+=("<small><span bgcolor='$WINCLR'>        </span></small>  Background color,^pipe(mbclr '$WINCLR' conkyctl win_bgcolor_all WIN_BGCOLOR '$me')")
    out+=("^sep()")
    out+=("FONTS [ $FONT $FONTSIZE ],^checkout(font)")
    out2+=("^tag(font)")
    out2+=("^sep(BASE_FONT)")
    out2+=("<tt>++</tt> Increase,conkyctl basefont_inc_all;$me")
    out2+=("<tt>--</tt> Decrease,conkyctl basefont_dec_all;$me")
    out2+=("^sep(Outline/Shadow)")
    out2+=("No,conkyctl shades_all no;$me")
    out2+=("^sep()")
    out2+=("Draw shades,conkyctl shades_all shadow;$me")
    out2+=("Draw outline,conkyctl shades_all outline;$me")


    out+=("^sep()")
    out+=("BORDERS [ $BORD $STIP ],^checkout(border)")
    out2+=("^tag(border)")
    out2+=("^sep(DRAW BORDERS?)")
    out2+=("Yes,conkyctl draw_borders_all true;$me")
    out2+=("No,conkyctl draw_borders_all false;$me")
    out2+=("^sep(STIPPLED BORDERS?)")
    out2+=("0,conkyctl stippled_borders_all 0;$me")
    out2+=("1,conkyctl stippled_borders_all 1;$me")
    out2+=("2,conkyctl stippled_borders_all 2;$me")
    out2+=("3,conkyctl stippled_borders_all 3;$me")
    out2+=("4,conkyctl stippled_borders_all 4;$me")

    #while read -r pid b c confpath 
    #do
    #    filename=${confpath##*/}
    #    name=${filename%%_mb*}
    #    out+=("${name^},^pipe(conkyctl single ${confpath})")
    #done < <(pgrep -af "conky.*mbcolor")

    # array with running conkies - only filenames with full path
    readarray -t running < <(pgrep -af "conky.*mbcolor" | cut -d' ' -f4)
    
    #array with all supported conkies
    for i in "$CONKYDIR"/*_mbcolor*; do
    supported+=("$i")
    done
        
    #array with not  started conkies
    notstarted=($(echo ${running[@]} ${supported[@]} | tr ' ' '\n' | sort | uniq -u))
    
    if [[ ${#running[@]} > 0 ]];then
        out+=("^sep(<small><i>Individual settings</i></small>)")
        for c in ${running[@]};do
            filename=${c##*/}
            name=${filename%%_mb*}
            title=${name//_/ }
            out+=("${title^},^pipe(conkyctl single ${c})")
        done
        if [[ ${#notstarted[@]} > 0 ]];then
            out+=("^sep(More Conkies Available)")
            out+=("^sep(<small><i>click to start</i></small>)")
            for c in ${notstarted[@]};do
            filename=${c##*/}
            name=${filename%%_mb*}
            title=${name//_/ }
            out+=("${title^},conkyctl startone ${c};$me")
        done
            if [[ ${#notstarted[@]} > 1 ]];then
                out+=("^sep()")
                out+=("Start all (${#notstarted[@]}),conkyctl startall;$me")
                fi
        fi
    fi
    
    #out+=("RESET")

else
    out+=("^sep(No supported Conky running)")
    out+=("^sep(<small><i>click to start some</i></small>)")
    for confpath in "$CONKYDIR"/*_mb*;
    do
    filename=${confpath##*/}
    name=${filename%%_mb*}
    title=${name//_/ }
    out+=("${title^},conkyctl startone ${confpath};$me")
    done
    out+=("^sep()")
    out+=("Start all,conkyctl startall;$me")
fi

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

    out+=("^sep()")
    out+=(" ⮜    Back to Colorizer,colorizer -s")

    if [[ $panels_heightpos == "top" ]]; then
        MENU_VALIGN="top"
        MENU_MARGIN_Y=${panels_topmargin:-0}
    elif [[ $panels_heightpos == "bottom" ]]; then
        MENU_VALIGN="bottom"
        MENU_MARGIN_Y=${panels_topmargin:-0}
    else 
        MENU_VALIGN="center"
    fi
    MENU_HALIGN="left"
    jgmenu_icon_size=0
        menu_margin_x=${submenu_spacing:-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
    
        #TEMP POSITION:
    MENU_VALIGN="top"
    MENU_MARGIN_Y="60"
    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 + 244)),$((jgtools_padding + 6)),29,60,0,left,top,#FFFFFF 30,${WINCLR} 60,
@rect,,$((jgtools_padding + 5)),$((jgtools_padding + 82)),29,40,0,left,top,#FFFFFF 30,${WINCLR} 60,
$(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[@]}"