export const FRAME_MAGIC = new Uint8Array([0x68, 0x68, 0xff, 0xff]);
export const FRAME_FLAG_2BYTE = 0xff;
export const FRAME_FLAG_4BYTE = 0xfe;
export const FRAME_TAIL = new Uint8Array([0x68, 0x68]);
export const DEFAULT_DEVICE_IP = '192.168.4.1';
export const DEFAULT_TCP_PORT = 4321;
export const MAX_PAYLOAD_SIZE = 400 * 1024; // 400KB sanity limit

export const FuncCode = {
  // App → Device
  SETUP_REQ: 0x01,
  CONTINUOUS_REQ: 0x02,
  SINGLE_REQ: 0x03,
  STOP_REQ: 0x04,
  SPLITFRAME_REQ: 0x08,
  // Device → App
  SETUP_ACK: 0x81,
  CONTINUOUS_ACK: 0x82,
  SINGLE_ACK: 0x83,
  STOP_ACK: 0x84,
  DATA_ACK: 0x85,
  SPLITFRAME_ACK: 0x88,
  COMP_DATA_ACK: 0x95,
} as const;

export const AckResult = { OK: 0x01, FAIL: 0x02 } as const;
export const DataChannel = { USB: 0x01, P900: 0x02, WIFI: 0x03 } as const;

export const SplitFrameCmd = { START: 0x01, REQUEST: 0x02, STOP: 0x03, LAST: 0x82 } as const;

export const SEND_FREQ_TABLE = [
  { code: 0x00, label: '0.5 Hz' },
  { code: 0x01, label: '1 Hz' },
  { code: 0x02, label: '2 Hz' },
  { code: 0x03, label: '4 Hz' },
  { code: 0x04, label: '8 Hz' },
  { code: 0x05, label: '16 Hz' },
  { code: 0x06, label: '25 Hz' },
  { code: 0x07, label: '32 Hz' },
  { code: 0x08, label: '50 Hz' },
  { code: 0x09, label: '64 Hz' },
] as const;

export const SAMPLE_FREQ_TABLE = [
  { code: 0x00, label: '250 kHz' },
  { code: 0x01, label: '125 kHz' },
  { code: 0x02, label: '62.5 kHz' },
  { code: 0x03, label: '31.25 kHz' },
  { code: 0x04, label: '15.6 kHz' },
  { code: 0x05, label: '7.8 kHz' },
  { code: 0x06, label: '3.9 kHz' },
  { code: 0x07, label: '1.95 kHz' },
  { code: 0x08, label: '977 Hz' },
  { code: 0x09, label: '488 Hz' },
  { code: 0x0a, label: '244 Hz' },
  { code: 0x0b, label: '122 Hz' },
  { code: 0x0c, label: '61 Hz' },
] as const;

// Gain table: ampRatio code → multiplier
export const AMP_GAIN = [0.125, 0.25, 0.5, 1, 2, 4, 8, 16, 32, 64, 128, 1];

export const AMP_RATIO_TABLE = [
  { code: 0x00, label: '1/8×', gain: 0.125 },
  { code: 0x01, label: '1/4×', gain: 0.25 },
  { code: 0x02, label: '1/2×', gain: 0.5 },
  { code: 0x03, label: '1×', gain: 1 },
  { code: 0x04, label: '2×', gain: 2 },
  { code: 0x05, label: '4×', gain: 4 },
  { code: 0x06, label: '8×', gain: 8 },
  { code: 0x07, label: '16×', gain: 16 },
  { code: 0x08, label: '32×', gain: 32 },
  { code: 0x09, label: '64×', gain: 64 },
  { code: 0x0a, label: '128×', gain: 128 },
  { code: 0x0b, label: '自动', gain: 1 },
] as const;

export const SOURCE_MODE_TABLE = [
  { code: 0x00, label: '单源 A', sourceNum: 1 },
  { code: 0x03, label: '双源 A-B', sourceNum: 2 },
  { code: 0x04, label: '双源 A-C', sourceNum: 2 },
  { code: 0x06, label: '三源 A-B-C', sourceNum: 3 },
  { code: 0x07, label: '磁源 A', sourceNum: 1 },
] as const;

export const CHANNEL_COLORS = [
  '#FF4444', // CH1 红
  '#44BB44', // CH2 绿
  '#4444FF', // CH3 蓝
  '#FFAA00', // CH4 橙
  '#AA44FF', // CH5 紫
  '#00CCCC', // CH6 青
] as const;
