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

# Set default modes
json_mode=0
alpha_mode=0
name_mode=0

# Function to display help message
show_help() {
  cat <<EOF
Usage: ${0##*/} [OPTIONS] [HEX_COLOR ...]

Convert hex color codes to RGB/RGBA and show a preview.

Options:
  --pick         Pick a color using wl-colorpicker-plasma
  --alpha        Interpret 8-digit hex codes (RRGGBBAA) and include alpha
  --json         Output in JSON format: {"hex":"#rrggbb","rgb":[r,g,b]}
  --name         Lookup and display nearest color name using thecolorapi.com
                 Requires: curl, jq, internet access
  --help, -h     Show this help message

Examples:
  ${0##*/} "#534145" "#433f43"
  echo "#123456" | ${0##*/}
  ${0##*/} --pick --alpha --json --name

EOF
}

hex_to_rgba() {
  local hex="$1"
  local clean_hex="${hex#\#}"

  # Validate and normalize hex
  if [[ "$clean_hex" =~ ^[0-9A-Fa-f]{6}$ ]]; then
    clean_hex="${clean_hex}FF" # Assume full opacity
  elif [[ "$clean_hex" =~ ^[0-9A-Fa-f]{8}$ ]]; then
    : # already has alpha
  else
    echo "Invalid hex color: $hex" >&2
    return 1
  fi

  local r=$((16#${clean_hex:0:2}))
  local g=$((16#${clean_hex:2:2}))
  local b=$((16#${clean_hex:4:2}))
  local a=$((16#${clean_hex:6:2}))
  local name=""

  if [[ $name_mode -eq 1 ]]; then
    local api="https://www.thecolorapi.com/id?hex=${clean_hex:0:6}"
    name=$(curl -s "$api" | jq -r '.name.value // empty')
  fi

  if [[ $json_mode -eq 1 ]]; then
    if [[ $alpha_mode -eq 1 ]]; then
      printf '{"hex":"#%s","rgba":[%d,%d,%d,%d]' "$clean_hex" "$r" "$g" "$b" "$a"
    else
      printf '{"hex":"#%s","rgb":[%d,%d,%d]' "${clean_hex:0:6}" "$r" "$g" "$b"
    fi
    [[ -n "$name" ]] && printf ',"name":"%s"' "$name"
    echo "}"
  else
    if [[ $alpha_mode -eq 1 ]]; then
      printf "\033[2mHEX:\033[0m #%s \033[2m→ RGBA(\033[0m%3d, %3d, %3d, %3d\033[2m)\033[0m → \033[48;2;%d;%d;%dm  \033[0m" \
        "$clean_hex" "$r" "$g" "$b" "$a" "$r" "$g" "$b"
    else
      printf "\033[2mHEX:\033[0m #%s \033[2m→ RGB(\033[0m%3d, %3d, %3d\033[2m)\033[0m → \033[48;2;%d;%d;%dm  \033[0m" \
        "${clean_hex:0:6}" "$r" "$g" "$b" "$r" "$g" "$b"
    fi
    [[ -n "$name" ]] && printf " \033[2m→ Name:\033[0m %s" "$name"
    echo
  fi
}

# Main
if [[ $# -eq 0 ]] && [[ -t 0 ]]; then
  show_help
  exit 0
fi

args=()
while [[ $# -gt 0 ]]; do
  case "$1" in
  --help | -h)
    show_help
    exit 0
    ;;
  --pick)
    if ! command -v wl-colorpicker-plasma &>/dev/null; then
      echo "Error: wl-colorpicker-plasma not found in PATH" >&2
      exit 1
    fi
    picked="$(wl-colorpicker-plasma)"
    [[ -n "$picked" ]] && args+=("$picked")
    ;;
  --json)
    json_mode=1
    ;;
  --alpha)
    alpha_mode=1
    ;;
  --name)
    name_mode=1
    ;;
  -*)
    echo "Unknown option: $1" >&2
    exit 1
    ;;
  *)
    args+=("$1")
    ;;
  esac
  shift
done

# If input is piped in, add to args
if [[ ! -t 0 ]]; then
  while read -r line; do
    [[ -n "$line" ]] && args+=("$line")
  done
fi

# Run conversion
for hex in "${args[@]}"; do
  hex_to_rgba "$hex"
done
