#!/bin/bash
#
#    colorizer - set of tools for Mabox (auto)theming
#    Copyright (C) 2022-2023 Daniel Napora    <napcok@gmail.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#  w2theme - generate themes from wallpaper colors

WALLPALDIR="$HOME/.cache/colorizer/palettes"
mkdir -p "$WALLPALDIR"
read WALLPATH<<< $(grep file "$HOME"/.config/nitrogen/bg-saved.cfg | cut -d'=' -f2)

NAME=${WALLPATH////_}
if [[ "${NAME}" =~ ^_home_.* ]]; then
n=${#HOME}
((n++))
NAME=${NAME:${n}}
fi


if [ ! -f "$WALLPALDIR/$NAME.clr" ]; then
convert ${WALLPATH} -resize 25% -colors 16 -unique-colors txt:- |grep -v '^#'| awk '{print substr($3,1,7)}' |pastel sort-by brightness |pastel format hex > "$WALLPALDIR/$NAME.clr"
fi
mapfile -t w < "$WALLPALDIR/$NAME.clr"

if [[ "${#w[@]}" -lt 8 ]]; then
w+=("#2e3440" "#4c566a" "#5e81ac" "#bf616b" "#b48ead" "#a3be8c" "#ebcb8b" "#d8dee9" "#eceff4")
fi

menu_theme () {
    jgctl theme MBcolors
    case "$1" in
        dark) #dark bg
        jgctl color_menu_bg "${w[2]}"
        jgctl color_menu_bg_to "${w[1]}"
        jgctl color_menu_border "${w[3]}"
        jgctl color_norm_fg "${w[-2]}"
        jgctl color_title_bg "${w[1]}"
        jgctl color_title_fg "${w[-3]}"
        jgctl color_title_border "${w[0]}"
        jgctl color_sel_bg "${w[4]}"
        jgctl color_sel_fg "${w[-1]}"
        jgctl color_sel_border "${w[3]}"
        jgctl color_sep_fg "${w[3]}"
        #sleep 1
        ;;
        *) #light bg
        jgctl color_menu_bg "${w[-1]}"
        jgctl color_menu_bg_to "${w[-2]}"
        jgctl color_menu_border "${w[-3]}"
        jgctl color_norm_fg "${w[1]}"
        jgctl color_title_bg "${w[-3]}"
        jgctl color_title_fg "${w[1]}"
        jgctl color_title_border "${w[-4]}"
        jgctl color_sel_bg "${w[-5]}"
        jgctl color_sel_fg "${w[0]}"
        jgctl color_sel_border "${w[-4]}"
        jgctl color_sep_fg "${w[-5]}"
        #sleep 1
        ;;
    esac
       case "$2" in
        show) 
        colorizer-menus -s
        ;;
        *):;;
    esac
}
conky_colors() {
    source ~/.config/colorizer/colorizer.conf
    case "$1" in
        dark) #dark bg
        conkyctl win_bgcolor_all "${w[0]}"
        conkyctl default_color_all "${w[-1]}"
        conkyctl color0_all "${w[-3]}"
        conkyctl color1_all "${w[-4]}"
        conkyctl color2_all "${w[-5]}"
        if [[ $conky_diff_colors == "yes" ]];then
            conkyctl win_bgcolor ${w[2]} "$HOME/.config/conky/mabox_tools_mbcolor.conkyrc"
            conkyctl win_bgcolor ${w[1]} "$HOME/.config/conky/shortcuts_mbcolor.conkyrc"
            conkyctl win_bgcolor ${w[3]} "$HOME/.config/conky/tiling_terminal_mbcolor.conkyrc"
        fi
        
        ;;
        *) #light bg
        conkyctl win_bgcolor_all "${w[-1]}"
        conkyctl default_color_all "${w[0]}"
        conkyctl color0_all "${w[2]}"
        conkyctl color1_all "${w[4]}"
        conkyctl color2_all "${w[6]}"
        if [[ $conky_diff_colors == "yes" ]];then
            conkyctl win_bgcolor ${w[-3]} "$HOME/.config/conky/mabox_tools_mbcolor.conkyrc"
            conkyctl win_bgcolor ${w[-5]} "$HOME/.config/conky/shortcuts_mbcolor.conkyrc"
            conkyctl win_bgcolor ${w[-4]} "$HOME/.config/conky/tiling_terminal_mbcolor.conkyrc"
        fi
        ;;
    esac
}
conky_colorsone () {
    source ~/.config/colorizer/colorizer.conf
    case "$1" in
        dark) #dark bg
        conkyctl win_bgcolor "${w[0]}" "${2}"
        conkyctl default_color "${w[-1]}" "${2}"
        conkyctl color0 "${w[-3]}" "${2}"
        conkyctl color1 "${w[-4]}" "${2}"
        conkyctl color2 "${w[-5]}" "${2}"
        ;;
        *) #light bg
        conkyctl win_bgcolor "${w[-1]}" "${2}"
        conkyctl default_color "${w[0]}" "${2}"
        conkyctl color0 "${w[2]}" "${2}"
        conkyctl color1 "${w[4]}" "${2}"
        conkyctl color2 "${w[6]}" "${2}"
        ;;
    esac
}
pyradio_theme() {
    case "$1" in
        dark) #dark bg
        prtctl stations "${w[-3]}" "${w[0]}"
        prtctl active_station "${w[-1]}"
        prtctl status_bar "${w[0]}" "${w[-3]}"
        prtctl normal_cursor "${w[1]}" "${w[-4]}"
        prtctl active_cursor "${w[2]}" "${w[-3]}"
        prtctl edit_cursor "${w[2]}" "${w[-3]}"
        prtctl extra_func "${w[6]}" 
        prtctl pyradio_url "${w[2]}" 
        prtctl messages_border "${w[2]}" 
        ;;
        *) # light bg
        prtctl stations "${w[4]}" "${w[-1]}"
        prtctl active_station "${w[0]}"
        prtctl status_bar "${w[0]}" "${w[-3]}"
        prtctl normal_cursor "${w[4]}" "${w[-3]}"
        prtctl active_cursor "${w[2]}" "${w[-3]}"
        prtctl edit_cursor "${w[2]}" "${w[-3]}"
        prtctl extra_func "${w[6]}" 
        prtctl pyradio_url "${w[2]}" 
        prtctl messages_border "${w[4]}" 
        ;;
        esac
}
accent () {
    #obtctl clractivebg ${1}
    #colorTo=$(pastel darken 0.2 "${1}"|pastel format hex)
    #obtctl clractivebgto ${colorTo}
    # JGMENU based menus, sidepanels and tools
    #jgctl color_title_bg ${1}
    #TITLE_FG=$(pastel textcolor ${colorTo}|pastel format hex)
    #TITLE_FG=$(pastel textcolor ${1}|pastel format hex)
    #notify-send.sh "title_fg $TITLE_FG" "${w[1]} ${w[-1]}"
    #if [[ "${TITLE_FG}" == *"ffffff"* ]];then
    #FG=${w[-2]}
    #BBG=${w[1]}
    #else
    #FG=${w[2]}
    #BBG=${w[1]}
    #fi
    SEL_FG=$(pastel textcolor ${1}|pastel format hex)
    #jgctl color_title_fg ${FG}
    #jgctl color_title_border ${BBG}
    jgctl color_sel_bg ${1}
    jgctl color_sel_bg 100
    jgctl color_sel_fg ${SEL_FG}
}

gradient () {
    source ~/.config/colorizer/colorizer.conf
    #notify-send.sh "zmiennne" "${1} ${2}"
    if [ "$gradient_obtheme" = "yes" ]; then
        obtctl activegradient "${1}" "${2}"
    fi
    if [ "$gradient_menu" = "yes" ]; then
        jgctl gradient "${1}" "${2}"
    fi
    if [ "$gradient_conkies" = "yes" ]; then
        conkygradient "${1}" "${2}"
    fi
}
conkygradient () {
    CONKYDIR="$HOME"/.config/conky
    for i in "$CONKYDIR"/*_mbcolor*; do
    if [[ "$i" = *"sysinfo_graph"* ]];then
        conkyctl win_bgcolor ${1} ${i}
        FG_COLOR=$(pastel textcolor ${1}|pastel format hex)
        if [[ "${FG_COLOR}" == *"ffffff"* ]]; then
            conkyctl default_color ${w[-1]} ${i}
            conkyctl color0 ${w[-2]} ${i}
            conkyctl color1 ${w[-3]} ${i}
        else
            conkyctl default_color ${w[0]} ${i}
            conkyctl color0 ${w[1]} ${i}
            conkyctl color1 ${w[2]} ${i}
        fi
    conkyctl color2 ${2} ${i}
    elif [[ "$i" = *"sysinfo_mb"* ]];then
    conkyctl win_bgcolor ${2} ${i}
        FG_COLOR=$(pastel textcolor ${2}|pastel format hex)
        if [[ "${FG_COLOR}" == *"ffffff"* ]]; then
            conkyctl default_color ${w[-1]} ${i}
            conkyctl color0 ${w[-2]} ${i}
            conkyctl color1 ${w[-3]} ${i}
        else
            conkyctl default_color ${w[0]} ${i}
            conkyctl color0 ${w[1]} ${i}
            conkyctl color1 ${w[2]} ${i}
        fi
    conkyctl color2 ${1} ${i}
    elif [[ "$i" == *"logo"* ]];then
    :
    else
    ran="$((RANDOM % 8))"
    rand=$((ran + 1))
    BG=$(pastel mix -f 0.${rand} ${1} ${2}|pastel format hex)
    conkyctl win_bgcolor ${BG} ${i}
    FG_COLOR=$(pastel textcolor ${BG}|pastel format hex)
        if [[ "${FG_COLOR}" == *"ffffff"* ]]; then
            conkyctl default_color ${w[-1]} ${i}
            conkyctl color0 ${w[-2]} ${i}
            conkyctl color1 ${w[-3]} ${i}
        else
            conkyctl default_color ${w[0]} ${i}
            conkyctl color0 ${w[1]} ${i}
            conkyctl color1 ${w[2]} ${i}
        fi
    fi
    done
}
obtheme () {
case "$1" in
    dark)
   obtctl clractivebg ${w[2]}
    TITLE_FG=$(pastel textcolor ${w[2]}|pastel format hex)
    if [[ "$TITLE_FG" == *"ffffff"* ]];then
        obtctl activetextcolor ${w[-1]}
        obtctl activebuttonscolors ${w[-1]}
    else
        obtctl activetextcolor ${w[0]}
        obtctl activebuttonscolors ${w[0]}
    fi
    obtctl clractivebgto ${w[4]}
    obtctl clrnormalbg ${w[0]}
    obtctl clrnormalbgto "$(pastel darken 0.1 "${w[0]}"|pastel format hex)" 
    ;;
    light)
    obtctl clractivebg ${w[-2]}
    TITLE_FG=$(pastel textcolor ${w[-2]}|pastel format hex)
    if [[ "$TITLE_FG" == *"ffffff"* ]];then
        obtctl activetextcolor ${w[-1]}
        obtctl activebuttonscolors ${w[-1]}
    else
        obtctl activetextcolor ${w[0]}
        obtctl activebuttonscolors ${w[0]}
    fi
    obtctl clractivebgto ${w[-4]}
    obtctl clrnormalbg ${w[1]}
    obtctl clrnormalbgto "$(pastel darken 0.1 "${w[1]}"|pastel format hex)" 
    ;;
    esac
}
colorize () {
    source ~/.config/colorizer/colorizer.conf
    # CONKY
    case "$wall2themes_conky" in
        dark) conky_colors dark;;
        light) conky_colors light;;
        *) : ;;
    esac
    # ACCENT_COLOR
    case "$wall2themes_accent" in
        dark) 
        obtctl theme MBcolors
        accent ${w[1]}
        ;;
        light)
        obtctl theme MBcolors
        accent ${w[-1]}
        ;;
        *) : ;;
    esac
    # MENUS
    case "$wall2themes_menu" in
        dark) menu_theme dark;;
        light) menu_theme light;;
        *) : ;;
    esac
    # Openbox (TUTAJ WARUNEK)
    case "$wall2themes_ob" in
        dark) obtheme dark;;
        light) obtheme light;;
        *) : ;;
    esac
    
    # PYRADIO THEME
    case "$wall2themes_pyradio" in
        dark) pyradio_theme dark;;
        light) pyradio_theme light;;
        *) : ;;
    esac
}

reverse () {
    source ~/.config/colorizer/colorizer.conf
    # CONKY
    case "$wall2themes_conky" in
        dark) conky_colors light
        ;;
        light) conky_colors dark
        ;;
        *) : ;;
    esac
    # ACCENT_COLOR
    case "$wall2themes_accent" in
        dark) 
        obtctl theme MBcolors
        accent ${w[-1]}
        ;;
        light)
        obtctl theme MBcolors
        accent ${w[1]}
        ;;
        *) : ;;
    esac
    # MENUS
    case "$wall2themes_menu" in
        dark) menu_theme light;;
        light) menu_theme dark;;
        *) : ;;
    esac
    # Openbox (TUTAJ WARUNEK)
    case "$wall2themes_ob" in
        dark) obtheme light;;
        light) obtheme dark;;
        *) : ;;
    esac
    # PYRADIO THEME
    case "$wall2themes_pyradio" in
        dark) pyradio_theme light;;
        light) pyradio_theme dark;;
        *) : ;;
    esac
}

genthumb () {
    #$1 - filename with full path
    NAME=${1////_}
    if [[ "${NAME}" =~ ^_home_.* ]]; then
        n=${#HOME}
        ((n++))
        NAME=${NAME:${n}}
    fi
    # GENERATE THUMBNAIL
    THUMBDIR="$HOME/.cache/colorizer/thumbs"
    mkdir -p ${THUMBDIR}
    THUMB="${THUMBDIR}/${NAME}.png"
    if [[ ! -f "$THUMB" ]]; then
        convert ${1} -resize 270x150^ -gravity center -extent 270x150 ${THUMB}
    fi
}
pin () {
#get wallpaper path and filename without ext

# mkdir for colorscheme
mkdir -p ~/.config/colorizer/pinned/$WALLNAME
# if dir exist then action is update so remove old content
rm -rf ~/.config/colorizer/pinned/$WALLNAME

# get menu themename jgmenu_theme=  ( it is always MBcolors ????)
. ~/.config/mabox/mabox.conf

}

case "$1" in 
    ob) obtheme "$2";;
    menu) menu_theme "$2" "$3";;
    conky) conky_colors "$2";;
    conkyone) conky_colorsone "$2" "$3";;
    pyradio) pyradio_theme "$2";;
    accent) accent "$2";;
    gradient) gradient "$2" "$3";;
    colorize) colorize;;
    reverse) reverse;;
    genthumb)genthumb "$2";;
    pin) pin;;
    *) ;;
esac