/**
 * UI Type Definitions — Hevy Companion Device App
 *
 * Maps Fitbit SVG GUI elements to the live workout state.
 * Uses Fitbit's document.getElementById() pattern for GUI manipulation.
 */

// ─── GUI Element IDs ─────────────────────────────────────────────────
// These must match the id attributes in resources/index.gui

export enum GuiElementId {
  // Screens (managed via display="inline"/"none")
  SCREEN_IDLE = 'screen-idle',
  SCREEN_LOADING = 'screen-loading',
  SCREEN_ACTIVE = 'screen-active',
  SCREEN_RESTING = 'screen-resting',
  SCREEN_DONE = 'screen-done',
  SCREEN_SYNCING = 'screen-syncing',
  SCREEN_COMPLETE = 'screen-complete',

  // Active workout elements
  EXERCISE_NAME = 'lbl-exercise-name',
  EXERCISE_NOTES = 'lbl-exercise-notes',
  SET_PROGRESS = 'lbl-set-progress',           // "Set 2 / 4"
  TARGET_WEIGHT = 'lbl-target-weight',          // "40 kg"
  TARGET_REPS = 'lbl-target-reps',              // "8–12"
  TARGET_RIR = 'lbl-target-rir',                // "RIR 2"

  // Input controls
  ACTUAL_WEIGHT = 'lbl-actual-weight',          // user-adjusted weight
  ACTUAL_REPS = 'lbl-actual-reps',              // user-adjusted reps
  ACTUAL_RIR = 'lbl-actual-rir',                // user-adjusted RIR
  BTN_WEIGHT_UP = 'btn-weight-up',
  BTN_WEIGHT_DOWN = 'btn-weight-down',
  BTN_REPS_UP = 'btn-reps-up',
  BTN_REPS_DOWN = 'btn-reps-down',
  BTN_RIR_0 = 'btn-rir-0',
  BTN_RIR_1 = 'btn-rir-1',
  BTN_RIR_2 = 'btn-rir-2',
  BTN_RIR_3 = 'btn-rir-3',

  // Action buttons
  BTN_SET_DONE = 'btn-set-done',
  BTN_SKIP_SET = 'btn-skip-set',
  BTN_SKIP_EXERCISE = 'btn-skip-exercise',
  BTN_PREV_EXERCISE = 'btn-prev-exercise',
  BTN_NEXT_EXERCISE = 'btn-next-exercise',
  BTN_FINISH = 'btn-finish',

  // Rest screen
  REST_TIMER = 'lbl-rest-timer',
  REST_NEXT_EXERCISE = 'lbl-rest-next-exercise',
  BTN_SKIP_REST = 'btn-skip-rest',

  // Done / Sync screens
  DONE_MESSAGE = 'lbl-done-message',
  SYNC_STATUS = 'lbl-sync-status',
  BTN_RETRY_SYNC = 'btn-retry-sync',
  BTN_NEW_WORKOUT = 'btn-new-workout',

  // Status bar
  STATUS_OFFLINE = 'img-offline-indicator',
  STATUS_BATTERY = 'lbl-battery',
  STATUS_TIME = 'lbl-clock',

  // Toast
  TOAST = 'lbl-toast',
}

// ─── Screen Visibility ───────────────────────────────────────────────

export function getVisibleScreen(state: string): GuiElementId {
  switch (state) {
    case 'IDLE': return GuiElementId.SCREEN_IDLE;
    case 'LOADING': return GuiElementId.SCREEN_LOADING;
    case 'ACTIVE': return GuiElementId.SCREEN_ACTIVE;
    case 'RESTING': return GuiElementId.SCREEN_RESTING;
    case 'DONE': return GuiElementId.SCREEN_DONE;
    case 'SYNCING': return GuiElementId.SCREEN_SYNCING;
    case 'COMPLETE': return GuiElementId.SCREEN_COMPLETE;
    default: return GuiElementId.SCREEN_IDLE;
  }
}

// ─── Update function signature ───────────────────────────────────────

export interface UiUpdater {
  /** Sets text content of a GUI element */
  setText(elementId: GuiElementId, text: string): void;
  /** Shows/hides a screen or element */
  setVisibility(elementId: GuiElementId, visible: boolean): void;
  /** Updates progress bar (set index / total) */
  setProgress(elementId: GuiElementId, current: number, total: number): void;
  /** Shows a toast message briefly */
  showToast(message: string, durationMs?: number): void;
}
