#!/bin/bash
# Ocean Heat Content Index
# Tracks integrated ocean heat from Climate Reanalyzer
# Alias: ohc

export DISPLAY=:0

# NOAA Coral Reef Watch SST URLs (working)
# Note: NOAA only provides anomaly maps, not raw SST
url_sst_anom="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_global_current.png"
url_sst_pacific="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_pacific_current.png"
url_sst_atlantic="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_satlantic_current.png"
url_sst_indian="https://coralreefwatch.noaa.gov/data_current/5km/v3.1_op/daily/png/ct5km_ssta_v3.1_indian_current.png"

# NOAA CPC ENSO/Nino monitoring
url_sst_nino="https://www.cpc.ncep.noaa.gov/products/analysis_monitoring/enso_update/sstweek_c.gif"

# Cache locations
cache_dir="$HOME/.cache/maritime/ohc"
cache_sst_anom="$cache_dir/sst_anom.png"
cache_sst_nino="$cache_dir/sst_nino.gif"
cache_sst_pacific="$cache_dir/sst_pacific.png"
cache_sst_atlantic="$cache_dir/sst_atlantic.png"
cache_sst_indian="$cache_dir/sst_indian.png"

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

# Update all data
update() {
    echo "Fetching ocean heat content data..."
    curl -sLo "$cache_sst_anom" "$url_sst_anom" &
    curl -sLo "$cache_sst_nino" "$url_sst_nino" &
    curl -sLo "$cache_sst_pacific" "$url_sst_pacific" &
    curl -sLo "$cache_sst_atlantic" "$url_sst_atlantic" &
    curl -sLo "$cache_sst_indian" "$url_sst_indian" &
    wait
    notify-send -t 3000 -i "$cache_sst_anom" "Ocean Heat Updated" "All data refreshed from NOAA"
}

# Show specific visualization
show() {
    local target="$cache_sst_anom"
    local title="SST Anomaly - Global"

    case "$1" in
        global|g|anom*|a|"")
            target="$cache_sst_anom"
            title="SST Anomaly - Global"
            ;;
        nino|enso|n)
            target="$cache_sst_nino"
            title="Nino 3.4 Region SST"
            ;;
        atlantic|atl)
            target="$cache_sst_atlantic"
            title="Atlantic SST Anomaly"
            ;;
        pacific|pac)
            target="$cache_sst_pacific"
            title="Pacific SST Anomaly"
            ;;
        indian|ind)
            target="$cache_sst_indian"
            title="Indian Ocean SST Anomaly"
            ;;
    esac

    setsid -f mpv --title="$title" --autofit=70% "$target"
}

# Show all in slideshow
show_all() {
    setsid -f mpv --title="Ocean Heat Content Overview" --idle --autofit=60% \
        --loop-playlist --image-display-duration=5 \
        "$cache_sst_anom" "$cache_sst_nino" \
        "$cache_sst_pacific" "$cache_sst_atlantic" "$cache_sst_indian"
}

# Display help
help() {
    cat << EOF
Ocean Heat Content Index
Usage: ohc [OPTION]

Options:
  update, u          Update all ocean heat data
  show, s [TYPE]     Show specific visualization:
    global, g        Global SST anomaly (default)
    nino, n          Nino 3.4 region (ENSO indicator)
    atlantic, atl    Atlantic SST anomaly
    pacific, pac     Pacific SST anomaly
    indian, ind      Indian Ocean SST anomaly
  all                Show all visualizations
  help, h            Show this help

Data Source: NOAA Coral Reef Watch 5km SST
             NOAA CPC ENSO Monitoring

Key Indicators:
  - Global SST anomaly shows deviation from baseline
  - Nino 3.4 is primary ENSO (El Nino/La Nina) indicator
  - Warm anomalies indicate ocean heat content trends
EOF
}

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

# Handle arguments
case "$1" in
    u*) update ;;
    show|s) shift; show "$1" ;;
    all|a) show_all ;;
    h*|--help) help ;;
    "") tobar ;;
    *) show "$1" ;;
esac

# Status bar click handlers
case $BLOCK_BUTTON in
    1) notify-send -t 60000 -i "$cache_sst_anom" "Ocean Heat - SST Anomaly" "Global sea surface temperature anomaly vs 1982-2011 baseline" ;;
    2) update ;;
    3) notify-send -t 60000 -i "$cache_ts_world" "Ocean Heat - Time Series" "Historical SST trends" ;;
    4) show "nino" ;;
    5) show "ts" ;;
    6) setsid -f "$TERMINAL" -e "$EDITOR" "$0" ;;
esac
