/**
 * Pi Multi-FX Pedal — Type Definitions
 *
 * Core types for footswitch modes, pedal state, and UI components.
 */

// ── Footswitch Modes ──────────────────────────────────────────

export type FootswitchMode = 'stomp' | 'preset' | 'snapshot' | 'combo' | 'transport';

export const FOOTSWITCH_MODES: FootswitchMode[] = ['stomp', 'preset', 'snapshot', 'combo'];
export const FOOTSWITCH_MODE_LABELS: Record<FootswitchMode, string> = {
  stomp: 'Stomp',
  preset: 'Preset',
  snapshot: 'Snapshot',
  combo: 'Combo',
  transport: 'Transport',
};

// ── Scribble Strip ────────────────────────────────────────────

export interface ScribbleStripData {
  /** Main label shown on the strip (uppercase track name) */
  label: string;
  /** Optional secondary line below the label */
  sublabel?: string;
  /** LED color for the footswitch ring / indicator */
  color?: ScribbleColor;
  /** Whether the footswitch is active/enabled */
  active?: boolean;
  /** Whether the block is bypassed (stomp mode) */
  bypassed?: boolean;
  /** Unique key for this footswitch */
  key: string;
  /** Optional icon */
  icon?: string;
}

export type ScribbleColor =
  | 'amber'
  | 'green'
  | 'red'
  | 'blue'
  | 'purple'
  | 'cyan'
  | 'white'
  | 'off';

export const SCRIBBLE_COLORS: Record<ScribbleColor, string> = {
  amber: '#E8A030',
  green: '#3AB87A',
  red: '#C84040',
  blue: '#3A7BA8',
  purple: '#9B59B6',
  cyan: '#1ABC9C',
  white: '#F0EDE6',
  off: '#444458',
};

// ── Footswitch Action ─────────────────────────────────────────

export type FootswitchAction =
  | { type: 'toggle_block'; blockId: string }
  | { type: 'bank_up' }
  | { type: 'bank_down' }
  | { type: 'select_preset'; bank: number; program: number }
  | { type: 'recall_snapshot'; snapshotIndex: number }
  | { type: 'global_bypass' }
  | { type: 'tap_tempo' }
  | { type: 'tuner' }
  | { type: 'bank_select'; bank: number };

// ── Pedal State (from API) ────────────────────────────────────

export interface PedalState {
  master_volume?: number;
  bypass?: boolean;
  tuner_enabled?: boolean;
  cpu_percent?: number;
  sample_rate?: number;
  nam_loaded?: boolean;
  nam_model?: string;
  ir_loaded?: boolean;
  ir_name?: string;
  current_preset?: { name: string; bank: number; program: number };
  input_level?: number;
  output_level?: number;
  routing_mode?: string;
  routing_breakpoint?: number;
  wifi?: {
    connected: boolean;
    ssid?: string;
    ip?: string;
    signal?: number;
    hotspot_active: boolean;
    hotspot_ssid?: string;
  };
  bluetooth?: {
    powered: boolean;
    discoverable: boolean;
    midi_enabled: boolean;
    name?: string;
    address?: string;
  };
  blocks?: PedalBlock[];
}

export interface PedalBlock {
  id: number;
  fx_type: string;
  title: string;
  bypassed: boolean;
  params?: BlockParam[];
}

export interface BlockParam {
  name: string;
  key: string;
  default?: number;
  min?: number;
  max?: number;
}

// ── Preset Data ───────────────────────────────────────────────

export interface PresetData {
  name: string;
  bank: number;
  program: number;
  chain?: PedalBlock[];
}

export interface BankData {
  name: string;
  number: number;
  presets: (PresetData | null)[];
}

export interface PresetListResponse {
  banks: BankData[];
}

// ── Snapshot ──────────────────────────────────────────────────

export interface SnapshotData {
  name: string;
  index: number;
  /** Block bypass states at snapshot time */
  blockStates: Record<number, boolean>;
  /** Parameter values at snapshot time (blockId -> paramKey -> value) */
  paramValues: Record<number, Record<string, number>>;
}

// ── WebSocket Messages ────────────────────────────────────────

export type WsMessage =
  | { type: 'connected'; state: PedalState }
  | { type: 'state'; state: PedalState }
  | { type: 'preset_changed'; state: PedalState }
  | { type: 'bypass_changed'; bypass: boolean }
  | { type: 'tuner_changed'; tuner_enabled: boolean }
  | { type: 'routing_changed'; routing_mode: string; routing_breakpoint: number }
  | { type: 'model_loaded' }
  | { type: 'ir_loaded' };
