#!/usr/bin/env bash
set -Eeuo pipefail

TASKBAR_VERSION="6"
TASKBAR_MARKER="$HOME/.config/.celita-taskbar-v${TASKBAR_VERSION}"
VISUAL_MARKER="$HOME/.config/.celita-visual-v4"

if [[ "${XDG_CURRENT_DESKTOP:-}" != *XFCE* && "${DESKTOP_SESSION:-}" != *xfce* ]]; then
    exit 0
fi

if [[ -f "$TASKBAR_MARKER" ]]; then
    exit 0
fi

for _attempt in {1..30}; do
    if xfconf-query -c xfce4-panel -p /configver >/dev/null 2>&1; then
        break
    fi
    sleep 1
done

# No pacote completo, o perfil visual também ajusta o painel. Esperar por ele
# garante que esta composição mínima seja sempre a configuração final.
if [[ -x /usr/local/bin/celita-visual-apply && ! -f "$VISUAL_MARKER" ]]; then
    for _attempt in {1..50}; do
        [[ -f "$VISUAL_MARKER" ]] && break
        sleep 0.2
    done
fi

xfset() {
    local channel="$1" property="$2" type="$3" value="$4"
    if xfconf-query -c "$channel" -p "$property" >/dev/null 2>&1; then
        xfconf-query -c "$channel" -p "$property" -s "$value" >/dev/null 2>&1 || true
    else
        xfconf-query -c "$channel" -n -p "$property" -t "$type" -s "$value" >/dev/null 2>&1 || true
    fi
}

xfarray_int() {
    local channel="$1" property="$2"
    shift 2
    xfconf-query -c "$channel" -p "$property" -r -R >/dev/null 2>&1 || true
    local args=() value
    for value in "$@"; do
        args+=( -t int -s "$value" )
    done
    xfconf-query -c "$channel" -n -a -p "$property" "${args[@]}" >/dev/null 2>&1 || true
}

xfarray_string() {
    local channel="$1" property="$2"
    shift 2
    xfconf-query -c "$channel" -p "$property" -r -R >/dev/null 2>&1 || true
    local args=() value
    for value in "$@"; do
        args+=( -t string -s "$value" )
    done
    xfconf-query -c "$channel" -n -a -p "$property" "${args[@]}" >/dev/null 2>&1 || true
}

mkdir -p "$HOME/.config/xfce4/panel/launcher-1" "$HOME/.config/gtk-3.0"

SCREEN_SIZE="$(xrandr --current 2>/dev/null | awk '/\*/ { print $1; exit }')"
if [[ "$SCREEN_SIZE" =~ ^([0-9]+)x([0-9]+)$ ]]; then
    PANEL_X=$((BASH_REMATCH[1] / 2))
    PANEL_Y=$((BASH_REMATCH[2] - 24))
else
    PANEL_X=960
    PANEL_Y=1059
fi

xfconf-query -c xfce4-panel -p /plugins -r -R >/dev/null 2>&1 || true
xfconf-query -c xfce4-panel -p /panels -r -R >/dev/null 2>&1 || true

xfset xfce4-panel /configver int 2
xfarray_int xfce4-panel /panels 1
xfset xfce4-panel /panels/panel-1/position string "p=8;x=${PANEL_X};y=${PANEL_Y}"
xfset xfce4-panel /panels/panel-1/position-locked bool true
xfset xfce4-panel /panels/panel-1/length uint 100
xfset xfce4-panel /panels/panel-1/length-adjust bool false
xfset xfce4-panel /panels/panel-1/size uint 48
xfset xfce4-panel /panels/panel-1/icon-size uint 26
xfset xfce4-panel /panels/panel-1/nrows uint 1
xfset xfce4-panel /panels/panel-1/mode uint 0
xfset xfce4-panel /panels/panel-1/autohide-behavior uint 0
xfset xfce4-panel /panels/panel-1/background-style uint 0
xfset xfce4-panel /panels/panel-1/enter-opacity uint 100
xfset xfce4-panel /panels/panel-1/leave-opacity uint 100
xfset xfce4-panel /panels/panel-1/span-monitors bool false
xfarray_int xfce4-panel /panels/panel-1/plugin-ids 1 2 3 4 5

xfset xfce4-panel /plugins/plugin-1 string "launcher"
install -m 0644 /usr/local/share/applications/celita-start-menu.desktop \
    "$HOME/.config/xfce4/panel/launcher-1/celita-start-menu.desktop"
sed -i '/^NoDisplay=/d' \
    "$HOME/.config/xfce4/panel/launcher-1/celita-start-menu.desktop"
xfarray_string xfce4-panel /plugins/plugin-1/items "celita-start-menu.desktop"
xfset xfce4-panel /plugins/plugin-1/disable-tooltips bool true

xfset xfce4-panel /plugins/plugin-2 string "tasklist"
xfset xfce4-panel /plugins/plugin-2/show-labels bool true
xfset xfce4-panel /plugins/plugin-2/show-handle bool false
xfset xfce4-panel /plugins/plugin-2/flat-buttons bool true
xfset xfce4-panel /plugins/plugin-2/grouping uint 1
xfset xfce4-panel /plugins/plugin-2/sort-order uint 0
xfset xfce4-panel /plugins/plugin-2/include-all-workspaces bool false
xfset xfce4-panel /plugins/plugin-2/max-button-length uint 190

xfset xfce4-panel /plugins/plugin-3 string "separator"
xfset xfce4-panel /plugins/plugin-3/expand bool true
xfset xfce4-panel /plugins/plugin-3/style uint 0

xfset xfce4-panel /plugins/plugin-4 string "systray"
xfset xfce4-panel /plugins/plugin-4/show-frame bool false
xfset xfce4-panel /plugins/plugin-4/square-icons bool true
xfset xfce4-panel /plugins/plugin-4/symbolic-icons bool true
xfset xfce4-panel /plugins/plugin-4/icon-size int 18
xfset xfce4-panel /plugins/plugin-4/single-row bool true

xfset xfce4-panel /plugins/plugin-5 string "clock"
xfset xfce4-panel /plugins/plugin-5/mode uint 2
xfset xfce4-panel /plugins/plugin-5/digital-format string "%H:%M"
xfset xfce4-panel /plugins/plugin-5/digital-date-format string "%d/%m/%Y"
xfset xfce4-panel /plugins/plugin-5/digital-time-format string "%H:%M"
xfset xfce4-panel /plugins/plugin-5/digital-layout uint 1
xfset xfce4-panel /plugins/plugin-5/digital-time-font string "Sans Semi-Bold 11"
xfset xfce4-panel /plugins/plugin-5/digital-date-font string "Sans 8"
xfset xfce4-panel /plugins/plugin-5/tooltip-format string "%A, %d de %B de %Y"
xfset xfce4-panel /plugins/plugin-5/command string "/usr/local/bin/celita-calendar"

/usr/local/bin/celita-theme aplicar >/dev/null 2>&1 || xfce4-panel -r >/dev/null 2>&1 || true
if command -v nm-applet >/dev/null 2>&1 && ! pgrep -x nm-applet >/dev/null 2>&1; then
    nm-applet >/dev/null 2>&1 &
fi
mkdir -p "$(dirname "$TASKBAR_MARKER")"
touch "$TASKBAR_MARKER"
