#!/bin/bash
# Sea Surface Temperature Anomaly Monitor
# Tracks global SST anomalies from NOAA Coral Reef Watch
# Alias: sst

export DISPLAY=:0

# URLs - NOAA Coral Reef Watch 5km SST Anomaly (v3.1)
url_global="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_global_current.png"
url_pacific="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_pacific_current.png"
url_atlantic="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_satlantic_current.png"
url_indian="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_indian_current.png"
url_tropics="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_tropics_current.png"

# Animated GIFs (30-day)
url_global_anim="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/animation/gif/ssta_animation_30day_large.gif"

# Climate Reanalyzer for time series
url_timeseries="https://climatereanalyzer.org/clim/sst_daily/vis/osst_bar_ssta_globe.svg"

# Cache locations
cache_dir="$HOME/.cache/maritime/sst"
cache_global="$cache_dir/global.png"
cache_pacific="$cache_dir/pacific.png"
cache_atlantic="$cache_dir/atlantic.png"
cache_indian="$cache_dir/indian.png"
cache_tropics="$cache_dir/tropics.png"
cache_global_anim="$cache_dir/global_anim.gif"
cache_timeseries="$cache_dir/timeseries.svg"

# Ensure cache dir exists
mkdir -p "$cache_dir"

# Update all SST data
update() {
    echo "Updating SST anomaly data..."
    curl -sLo "$cache_global" "$url_global" &
    curl -sLo "$cache_pacific" "$url_pacific" &
    curl -sLo "$cache_atlantic" "$url_atlantic" &
    curl -sLo "$cache_indian" "$url_indian" &
    curl -sLo "$cache_tropics" "$url_tropics" &
    curl -sLo "$cache_global_anim" "$url_global_anim" &
    curl -sLo "$cache_timeseries" "$url_timeseries" &
    wait
    notify-send -t 3000 -i "$cache_global" "SST Anomaly Updated" "All ocean basins refreshed"
}

# Show images in mpv
images() {
    local target="$cache_global"
    case "$1" in
        p*|P*) target="$cache_pacific" ;;
        a*|A*) target="$cache_atlantic" ;;
        i*|I*) target="$cache_indian" ;;
        t*) target="$cache_timeseries" ;;
    esac
    setsid -f mpv --title="SST Anomaly" --idle --autofit=70% \
        --loop-file=inf "$target"
}

# Show all images
images_all() {
    setsid -f mpv --title="SST Anomaly - All Basins" --idle --autofit=60% \
        --loop-playlist --image-display-duration=5 \
        "$cache_global" "$cache_pacific" "$cache_atlantic" "$cache_indian" "$cache_timeseries"
}

# Display help
help() {
    cat << EOF
Sea Surface Temperature Anomaly Monitor
Usage: sst [OPTION]

Options:
  update, u       Update all SST data from NOAA
  images, i       Show global SST anomaly image
    -p, pacific   Show Pacific basin
    -a, atlantic  Show Atlantic basin
    -i, indian    Show Indian basin
    -t, time      Show time series
  all             Show all basins in slideshow
  help, h         Show this help

Data Source: NOAA Coral Reef Watch 5km SST Anomaly
             Climate Reanalyzer Daily SST
EOF
}

# Status bar output
tobar() {
    echo "SST"
}

# Handle arguments
case "$1" in
    u*) update ;;
    i*)
        shift
        images "$1"
        ;;
    all) images_all ;;
    h*|--help) help ;;
    "") tobar ;;
    *) help ;;
esac

# Status bar click handlers
case $BLOCK_BUTTON in
    1) notify-send -t 60000 -i "$cache_global" "SST Anomaly - Global" "Left-click: Global | Right-click: Pacific | Scroll: Atlantic/Indian" ;;
    2) update ;;
    3) notify-send -t 60000 -i "$cache_pacific" "SST Anomaly - Pacific" "Middle-click to refresh data" ;;
    4) notify-send -t 60000 -i "$cache_atlantic" "SST Anomaly - Atlantic" "" ;;
    5) notify-send -t 60000 -i "$cache_indian" "SST Anomaly - Indian" "" ;;
    6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
