#!/usr/bin/env zsh

function monitor_input_usage {
  cat <<'EOF'
Usage:
  monitor_input [toggle|dp|usb-c|hdmi1|hdmi2]

Switches the current DDC/CI monitor input. On the MSI MPG341CX OLED:
  dp     -> DisplayPort
  usb-c  -> USB-C / Type-C
  toggle -> DisplayPort <-> USB-C

Environment:
  MONITOR_INPUT_DDC_DISPLAY  ddcutil display number, default: 1
EOF
}

function monitor_input_notify {
  local message="$1"
  if command -v notify-send >/dev/null 2>&1; then
    notify-send "Monitor input" "$message"
  fi
}

function monitor_input_current {
  local display="$1"
  ddcutil --display "$display" getvcp 60 --terse 2>/dev/null \
    | grep -Eo 'x[0-9a-fA-F]+' \
    | tail -1
}

function monitor_input {
  local target="${1:-toggle}"
  local display="${MONITOR_INPUT_DDC_DISPLAY:-1}"
  local value label current

  if [[ "$target" == "-h" || "$target" == "--help" ]]; then
    monitor_input_usage
    return 0
  fi

  if ! command -v ddcutil >/dev/null 2>&1; then
    echo "monitor_input: ddcutil is not available on PATH" >&2
    monitor_input_notify "ddcutil is not available"
    return 1
  fi

  case "${target:l}" in
    toggle)
      current="$(monitor_input_current "$display")"
      case "${current:l}" in
        x0f)
          target="dp"
          ;;
        x10)
          target="usb-c"
          ;;
        *)
          echo "monitor_input: current input is '${current:-unknown}', defaulting to DisplayPort" >&2
          target="dp"
          ;;
      esac
      ;;
  esac

  case "${target:l}" in
    dp|displayport|display-port)
      value="0x10"
      label="DisplayPort"
      ;;
    usb-c|usbc|type-c|typec)
      value="0x0f"
      label="USB-C"
      ;;
    hdmi1|hdmi-1)
      value="0x11"
      label="HDMI 1"
      ;;
    hdmi2|hdmi-2)
      value="0x12"
      label="HDMI 2"
      ;;
    *)
      monitor_input_usage >&2
      return 2
      ;;
  esac

  monitor_input_notify "Switching to $label"
  ddcutil --display "$display" --noverify setvcp 60 "$value"
}

monitor_input "$@"
