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

# Script Name: radio
# Description: Internet radio station wrapper with curated stations
# Source: Inspired by https://evanhahn.com/scripts-i-wrote-that-i-use-all-the-time/
# Usage: radio <station-name>
#        radio list

# Check for mpv
if ! command -v mpv &>/dev/null; then
  echo "Error: mpv not found. Install with: sudo apt install mpv" >&2
  exit 1
fi

# Curated radio stations
declare -A stations=(
  # Rock & Metal
  ["metal"]="http://listen.181fm.com/181-greatoldies_128k.mp3|Heavy Metal & Hard Rock"
  ["rock"]="http://listen.181fm.com/181-rock_128k.mp3|Classic Rock"

  # Cyberpunk / Synthwave / Darkwave
  ["cyberpunk"]="https://stream.nightride.fm/nightride.mp3|Nightride FM - Synthwave/Retrowave/Outrun"
  ["darksynth"]="https://stream.nightride.fm/darksynth.mp3|Darksynth/Cyberpunk/Synthmetal"
  ["chillsynth"]="https://stream.nightride.fm/chillsynth.mp3|Chillsynth/Chillwave/Instrumental"
  ["datawave"]="https://stream.nightride.fm/datawave.mp3|Glitchy Synthwave/IDM"
  ["spacesynth"]="https://stream.nightride.fm/spacesynth.mp3|Spacesynth/Space Disco"
  ["horrorsynth"]="https://stream.nightride.fm/horrorsynth.mp3|Horrorsynth/Witch House"
  ["industrial"]="https://stream.nightride.fm/ebsm.mp3|Industrial/EBM/Midtempo"

  # Electronic / Techno
  ["techno"]="http://stream.laut.fm/technobase|Techno Base - Coding Beats"
  ["dnb"]="https://stream.nightride.fm/rektfm.mp3|Drum & Bass/Dubstep/Techno/Trance"
  ["lofi"]="https://stream.nightride.fm/chillsynth.mp3|Lo-Fi Chill Beats"

  # Meditation / Ambient / Relaxation
  ["meditation"]="https://streaming.radionomy.com/-zenoflm-|Zen FM - Meditation & Relaxation"
  ["ambient"]="https://somafm.com/dronezone130.pls|SomaFM Drone Zone - Ambient"
  ["sleep"]="https://somafm.com/deepspaceone130.pls|SomaFM Deep Space One"
  ["nature"]="https://streaming.radionomy.com/NatureSoundsRelaxation|Nature Sounds"

  # Classical
  ["classical"]="https://stream.live.vc.bbcmedia.co.uk/bbc_radio_three|BBC Radio 3 - Classical"
  ["piano"]="http://stream.laut.fm/piano-classics|Piano Classics"
)

# List available stations
if [[ $# -eq 0 ]] || [[ "$1" == "list" ]]; then
  echo "📻 Available Radio Stations:"
  echo ""
  echo "🤘 ROCK & METAL:"
  echo "  radio metal      - Heavy Metal & Hard Rock"
  echo "  radio rock       - Classic Rock"
  echo ""
  echo "🌃 CYBERPUNK / SYNTHWAVE:"
  echo "  radio cyberpunk  - Nightride FM (Synthwave/Retrowave)"
  echo "  radio darksynth  - Darksynth/Cyberpunk/Synthmetal"
  echo "  radio chillsynth - Chillsynth/Chillwave"
  echo "  radio datawave   - Glitchy Synthwave/IDM"
  echo "  radio spacesynth - Spacesynth/Space Disco"
  echo "  radio horrorsynth- Horrorsynth/Witch House"
  echo "  radio industrial - Industrial/EBM"
  echo ""
  echo "🎧 ELECTRONIC / TECHNO:"
  echo "  radio techno     - Techno Base (Coding Beats)"
  echo "  radio dnb        - Drum & Bass/Dubstep/Trance"
  echo "  radio lofi       - Lo-Fi Chill Beats"
  echo ""
  echo "🧘 MEDITATION / AMBIENT:"
  echo "  radio meditation - Zen FM"
  echo "  radio ambient    - SomaFM Drone Zone"
  echo "  radio sleep      - Deep Space Ambient"
  echo "  radio nature     - Nature Sounds"
  echo ""
  echo "🎹 CLASSICAL:"
  echo "  radio classical  - BBC Radio 3"
  echo "  radio piano      - Piano Classics"
  echo ""
  exit 0
fi

station="$1"

# Check if station exists
if [[ -z "${stations[$station]:-}" ]]; then
  echo "Error: Station '$station' not found" >&2
  echo "Run 'radio list' to see available stations" >&2
  exit 1
fi

# Parse station URL and description
IFS='|' read -r url description <<< "${stations[$station]}"

echo "📻 Tuning in to: $description"
echo "🔊 Playing... (Ctrl+C to stop)"
echo ""

# Play with mpv (only fallback to VLC if mpv is not installed or stream fails to start)
if ! command -v mpv &>/dev/null; then
  echo "mpv not found. Trying VLC..." >&2
  if command -v vlc &>/dev/null; then
    vlc "$url" 2>/dev/null
    exit $?
  else
    echo "Error: Neither mpv nor VLC is installed" >&2
    exit 1
  fi
fi

# mpv will return non-zero for user interruption (Ctrl+C), which is normal
mpv --no-video --volume=50 "$url"
