#!/bin/bash
#
# download_team_logos.sh
# Downloads NHL team logos from the NHL assets CDN.
#
# Usage: ./Scripts/download_team_logos.sh [season]
#   season: e.g. 20252026 (defaults to current season)
#
# Downloads SVGs to /tmp, rasterizes to PNGs sized for the macOS notification
# thumbnail (72×72 square, aspect-preserved with transparent padding). Output
# goes to IceGlass/Resources/TeamLogos/ — used as notification attachments via
# Bundle.main.url(forResource:withExtension:subdirectory:"TeamLogos").
#
# Native notification thumbnail size was measured empirically at ~48 physical
# pixels. 72px is a small 1.5x over-sample that stays crisp without any
# visible downsample aliasing (tested at 48/72/96/128 — 48–72 are sharp).
#
# Pipeline: rsvg renders the SVG at a generous intermediate resolution;
# square_logo.swift then trims the transparent margins (which NHL brand SVGs
# pad the viewBox with) so the logo fills the final square, then scales
# aspect-preserved into the 72×72 target.
#

set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
LOGOS_DIR="$PROJECT_DIR/IceGlass/Resources/TeamLogos"
TMP_DIR="$(mktemp -d)"

# Determine season
if [ -n "$1" ]; then
    SEASON="$1"
else
    MONTH=$(date +%m)
    YEAR=$(date +%Y)
    if [ "$MONTH" -lt 7 ]; then
        SEASON="$((YEAR - 1))$YEAR"
    else
        SEASON="$YEAR$((YEAR + 1))"
    fi
fi

TARGET_SIZE=72
INTERMEDIATE_WIDTH=1024   # high-res intermediate so the trim+downscale has detail

echo "Downloading NHL team logos for season $SEASON (target size ${TARGET_SIZE}×${TARGET_SIZE})"

TEAMS=(
    ANA BOS BUF CAR CBJ CGY CHI COL DAL DET
    EDM FLA LAK MIN MTL NJD NSH NYI NYR OTT
    PHI PIT SEA SJS STL TBL TOR UTA VAN VGK
    WPG WSH
)

mkdir -p "$LOGOS_DIR"

for TEAM in "${TEAMS[@]}"; do
    SVG_URL="https://assets.nhle.com/logos/nhl/svg/${TEAM}_light.svg"
    SVG_FILE="$TMP_DIR/${TEAM}_light.svg"
    INTERMEDIATE="$TMP_DIR/${TEAM}_native.png"
    LOGO_PNG="$LOGOS_DIR/${TEAM}.png"

    echo "  Downloading $TEAM..."
    curl -s -o "$SVG_FILE" "${SVG_URL}?season=${SEASON}"

    # Rasterize SVG at high-res intermediate width (aspect-preserved, transparent)
    if command -v rsvg-convert &>/dev/null; then
        rsvg-convert -w "$INTERMEDIATE_WIDTH" --keep-aspect-ratio --background-color=transparent "$SVG_FILE" -o "$INTERMEDIATE" 2>/dev/null
    else
        python3 -c "
try:
    import cairosvg
    cairosvg.svg2png(url='$SVG_FILE', write_to='$INTERMEDIATE', output_width=$INTERMEDIATE_WIDTH)
except ImportError:
    pass
" 2>/dev/null || true
    fi

    if [ ! -f "$INTERMEDIATE" ]; then
        echo "    warning: failed to rasterize $TEAM svg"
        continue
    fi

    # Composite onto a target-size square transparent canvas (aspect-preserved, centered)
    swift "$SCRIPT_DIR/square_logo.swift" "$INTERMEDIATE" "$LOGO_PNG" "$TARGET_SIZE"
done

rm -rf "$TMP_DIR"

echo ""
echo "Done! Downloaded ${#TEAMS[@]} team logos to $LOGOS_DIR (${TARGET_SIZE}×${TARGET_SIZE})"
echo "Season: $SEASON"
