#!/bin/bash
# ============================================================
#  get_themes.sh v2 — Thèmes Tabulator autonomes (sans @import)
#  Usage : cd THEMES/ && bash get_themes.sh
# ============================================================

THEMES_DIR="$(cd "$(dirname "$0")" && pwd)"
BASE="$THEMES_DIR/tabulator_modern.min.css"
CDN="https://unpkg.com/tabulator-tables@5.5.0/dist/css"

echo "============================================================"
echo " Tabulator Theme Installer v2 (thèmes autonomes)"
echo "============================================================"
echo ""

# Vérification du fichier de base
if [ ! -f "$BASE" ] || [ ! -s "$BASE" ]; then
    echo "[!] tabulator_modern.min.css manquant, téléchargement..."
    wget -4 -O "$BASE" "$CDN/tabulator_modern.min.css" && echo "  OK" || { echo "  ERREUR reseau"; exit 1; }
fi
BASE_CSS=$(cat "$BASE")
echo "[OK] Base CSS chargee ($(wc -c < "$BASE") octets)"
echo ""

# Téléchargement Bootstrap4
echo "[1/3] Telechargement Bootstrap 4..."
wget -4 -O "$THEMES_DIR/tabulator_bootstrap4.min.css" "$CDN/tabulator_bootstrap4.min.css" \
    && echo "  OK tabulator_bootstrap4.min.css" \
    || echo "  Echec Bootstrap4 (reseau?)"
echo ""

# Génération thèmes colorés autonomes
echo "[2/3] Generation des themes colores autonomes..."

generate_theme() {
    local NAME="$1"
    local FILE="$THEMES_DIR/$2"
    local PRIMARY="$3"
    local PRIMARY_DARK="$4"
    local HEADER_BG="$5"
    local HEADER_FG="$6"
    local ROW_EVEN="$7"
    local ROW_HOVER="$8"
    local BORDER="$9"

    printf '%s\n' "${BASE_CSS}" > "$FILE"
    cat >> "$FILE" << CSSEOF

/* == Override couleur : ${NAME} == */
.tabulator { border: 1px solid ${BORDER} !important; border-radius: 6px; overflow: hidden; }
.tabulator .tabulator-header { background-color: ${HEADER_BG} !important; color: ${HEADER_FG} !important; border-bottom: 2px solid ${PRIMARY} !important; }
.tabulator .tabulator-header .tabulator-col { background-color: ${HEADER_BG} !important; color: ${HEADER_FG} !important; border-right: 1px solid ${BORDER} !important; }
.tabulator .tabulator-header .tabulator-col.tabulator-sortable:hover { background-color: ${PRIMARY_DARK} !important; cursor: pointer; }
.tabulator .tabulator-header .tabulator-col.tabulator-sortable[aria-sort=ascending]  .tabulator-col-content .tabulator-col-sorter .tabulator-arrow { border-bottom: 6px solid ${HEADER_FG} !important; border-top: none !important; }
.tabulator .tabulator-header .tabulator-col.tabulator-sortable[aria-sort=descending] .tabulator-col-content .tabulator-col-sorter .tabulator-arrow { border-top: 6px solid ${HEADER_FG} !important; border-bottom: none !important; }
.tabulator-row { background-color: #ffffff !important; border-bottom: 1px solid ${BORDER} !important; }
.tabulator-row.tabulator-row-even { background-color: ${ROW_EVEN} !important; }
.tabulator-row:hover, .tabulator-row.tabulator-selectable:hover { background-color: ${ROW_HOVER} !important; cursor: pointer; }
.tabulator-row.tabulator-selected { background-color: ${PRIMARY}44 !important; }
.tabulator-row .tabulator-cell.tabulator-editing { border: 1px solid ${PRIMARY} !important; outline: none; }
.tabulator .tabulator-footer { background-color: ${HEADER_BG} !important; color: ${HEADER_FG} !important; border-top: 2px solid ${PRIMARY} !important; }
.tabulator .tabulator-footer .tabulator-page { border: 1px solid ${BORDER} !important; color: ${HEADER_FG} !important; background: transparent !important; }
.tabulator .tabulator-footer .tabulator-page.active { background-color: ${PRIMARY} !important; border-color: ${PRIMARY} !important; color: #ffffff !important; font-weight: bold; }
.tabulator .tabulator-footer .tabulator-page:not([disabled]):hover { background-color: ${PRIMARY_DARK} !important; border-color: ${PRIMARY_DARK} !important; color: #ffffff !important; }
.tabulator .tabulator-footer .tabulator-page[disabled] { opacity: 0.4; }
CSSEOF
    echo "  OK $2 (${NAME})"
}

#              NOM        FICHIER                      PRIMARY    DARK       HEADER_BG  HEADER_FG  ROW_EVEN   ROW_HOVER  BORDER
generate_theme "Ocean"    "tabulator_ocean.css"        "#0077b6"  "#023e8a"  "#03045e"  "#caf0f8"  "#f0f7ff"  "#ade8f4"  "#90e0ef"
generate_theme "Forest"   "tabulator_forest.css"       "#2d6a4f"  "#1b4332"  "#1b4332"  "#d8f3dc"  "#f0faf3"  "#b7e4c7"  "#74c69d"
generate_theme "Sunset"   "tabulator_sunset.css"       "#e85d04"  "#9c2c00"  "#370617"  "#ffedd5"  "#fff8f0"  "#fed7aa"  "#fb8500"
generate_theme "Violet"   "tabulator_violet.css"       "#7c3aed"  "#4c1d95"  "#2e1065"  "#ede9fe"  "#faf5ff"  "#ddd6fe"  "#a78bfa"
generate_theme "Slate"    "tabulator_slate.css"        "#0f172a"  "#020617"  "#1e293b"  "#e2e8f0"  "#f8fafc"  "#cbd5e1"  "#475569"
generate_theme "Rose"     "tabulator_rose.css"         "#e11d48"  "#9f1239"  "#4c0519"  "#ffe4e6"  "#fff1f2"  "#fecdd3"  "#fb7185"
generate_theme "Teal"     "tabulator_teal.css"         "#0d9488"  "#134e4a"  "#042f2e"  "#ccfbf1"  "#f0fdfa"  "#99f6e4"  "#2dd4bf"
generate_theme "Amber"    "tabulator_amber.css"        "#d97706"  "#78350f"  "#451a03"  "#fef3c7"  "#fffbeb"  "#fde68a"  "#f59e0b"
generate_theme "Indigo"   "tabulator_indigo.css"       "#4338ca"  "#1e1b4b"  "#1e1b4b"  "#e0e7ff"  "#f5f3ff"  "#c7d2fe"  "#818cf8"
generate_theme "Crimson"  "tabulator_crimson.css"      "#dc2626"  "#7f1d1d"  "#450a0a"  "#fee2e2"  "#fff5f5"  "#fca5a5"  "#f87171"

echo ""

# Mise à jour themes.css
echo "[3/3] Mise a jour de themes.css..."
cat > "$THEMES_DIR/themes.css" << EOF
Modern (defaut):tabulator_modern.min.css
Bootstrap 5:tabulator_bootstrap5.min.css
Bootstrap 4:tabulator_bootstrap4.min.css
Bulma:tabulator_bulma.min.css
Materialize:tabulator_materialize.min.css
Midnight (sombre):tabulator_midnight.min.css
Semantic UI:tabulator_semanticui.min.css
Simple:tabulator_simple.min.css
Ocean:tabulator_ocean.css
Forest:tabulator_forest.css
Sunset:tabulator_sunset.css
Violet:tabulator_violet.css
Slate:tabulator_slate.css
Rose:tabulator_rose.css
Teal:tabulator_teal.css
Amber:tabulator_amber.css
Indigo:tabulator_indigo.css
Crimson:tabulator_crimson.css
EOF

echo "  OK themes.css mis a jour (18 themes)"
echo ""
echo "============================================================"
echo " Termine !"
ls -lh "$THEMES_DIR"/tabulator_*.css | awk '{printf "  %6s  %s\n", $5, $NF}'
echo "============================================================"
