/**
 * Protocol-aware device image mapper
 * Maps manufacturer and device names to device image filenames based on protocol
 */

type DeviceImageMap = Record<string, Record<string, string>>;

const meshcoreDeviceMap: DeviceImageMap = {
  heltec: {
    mesh_solar: 'heltec_mesh_solar.svg',
    meshpocket: 'heltec_meshpocket.svg',
    paper: 'heltec_paper.svg',
    t114: 'heltec_t114.svg',
    v2: 'heltec_v2.svg',
    v3: 'heltec_v3.svg',
    v4: 'heltec_v4.svg',
    wp: 'heltec_wp.svg',
    wsl3: 'heltec_wsl3.svg',
    wt3: 'heltec_wt3.svg',
  },
  lilygo: {
    t_beam: 'lilygo_tbeam.svg',
    tbeam: 'lilygo_tbeam.svg',
    t_beam_supreme: 'lilygo_tbeam_supreme.svg',
    tbeam_supreme: 'lilygo_tbeam_supreme.svg',
    t_deck: 'lilygo_tdeck.svg',
    tdeck: 'lilygo_tdeck.svg',
    t_deck_pro: 'lilygo_tdeck_pro.svg',
    tdeck_pro: 'lilygo_tdeck_pro.svg',
    t_display: 'lilygo_tdisplay.svg',
    tdisplay: 'lilygo_tdisplay.svg',
    t_echo: 'lilygo_techo.svg',
    techo: 'lilygo_techo.svg',
    t_echo_lite: 'lilygo_techo_lite.svg',
    techo_lite: 'lilygo_techo_lite.svg',
    t_pager: 'lilygo_pager.svg',
    pager: 'lilygo_pager.svg',
  },
  rak: {
    '4631': 'rak_4631.svg',
    '11300': 'rak_11300.svg',
    wismesh_tag: 'rak_wismesh_tag.svg',
  },
  seeed: {
    wio_tracker_l1: 'wio_tracker_l1.svg',
    wio_tracker_l1_eink: 'wio_tracker_l1_eink.svg',
    sensecap_solar: 'sensecap_solar.svg',
    sensecap_t1000e: 'sensecap_t1000e.svg',
  },
  xiao: {
    esp32c3: 'xiao_esp32c3.svg',
    esp32c6: 'xiao_esp32c6.svg',
    esp32s3: 'xiao_esp32s3.svg',
    nrf52: 'xiao_nrf52.svg',
  },
  ikoka: {
    nano: 'ikoka_nano.svg',
    stick: 'ikoka_stick.svg',
  },
  keepteen: {
    lt1: 'keepteen_lt1.svg',
  },
  thinknode: {
    m1: 'thinknode_m1.svg',
    m2: 'thinknode_m2.svg',
    m3: 'thinknode_m3.svg',
    m5: 'thinknode_m5.svg',
    m6: 'thinknode_m6.svg',
  },
  nano: {
    g2: 'nano_g2.svg',
  },
  station: {
    g2: 'station_g2.svg',
  },
  generic: {
    meshcore: 'meshcore.svg',
    esp32: 'esp32.svg',
    esp_now: 'esp_now.svg',
    lora: 'lora.svg',
    nrf52: 'nrf52.svg',
    rpi: 'rpi.svg',
    rpi_pico: 'rpi_picow.svg',
    picow: 'rpi_picow.svg',
  },
};

const aprsDeviceMap: DeviceImageMap = {
  lilygo: {
    lora32: 'lilygo_tlora_1.6.svg',
    'lora32 v2': 'lilygo_tlora_1.6.svg',
    'lora32 v2.1': 'lilygo_tlora_1.6.svg',
    't3s3': 'lilygo_t3s3.svg',
    't5_pro': 'lilygo_t5_pro.svg',
    't_beam': 'lilygo_tbeam.svg',
    'tbeam_v1.1': 'lilygo_tbeam.svg',
    't_deck': 'lilygo_tdeck.svg',
    't_lora_c6': 'lilygo_tlora_c6.svg',
    't_echo': 'lilygo_techo.svg',
  },
  heltec: {
    'lora32': 'heltec_v2.svg',
    'lora32 v2': 'heltec_v3.svg',
    'lora32 v3': 'heltec_v3.svg',
    'lora32 v4': 'heltec_v4.svg',
    'wireless_paper': 'heltec_paper.svg',
    'wireless_paper_pro': 'heltec_paper.svg',
  },
  rak: {
    '4631': 'rak_4631.svg',
    '11300': 'rak_11300.svg',
  },
  generic: {
    lora: 'lora.svg',
    esp32: 'esp32.svg',
  },
};

export function getDeviceImageURL(
  protocol: string,
  manufacturer?: string | null,
  device?: string | null
): string {
  if (!manufacturer || !device) {
    return '/image/device/unknown.png';
  }

  const protocolLower = protocol.toLowerCase();
  const manufacturerLower = manufacturer.toLowerCase().replace(/\s+/g, '_');
  const deviceLower = device.toLowerCase().replace(/\s+/g, '_');

  let deviceMap: DeviceImageMap | undefined;

  if (protocolLower === 'meshcore' || protocolLower.includes('mesh')) {
    deviceMap = meshcoreDeviceMap;
  } else if (protocolLower === 'aprs') {
    deviceMap = aprsDeviceMap;
  }

  if (!deviceMap) {
    // Unknown protocol, try generic lookup
    return `/image/device/unknown.png`;
  }

  // Try exact match first
  const mfgMap = deviceMap[manufacturerLower];
  if (mfgMap && mfgMap[deviceLower]) {
    return `/image/device/${mfgMap[deviceLower]}`;
  }

  // Try partial match (search for device containing the key)
  if (mfgMap) {
    for (const [key, filename] of Object.entries(mfgMap)) {
      if (deviceLower.includes(key) || key.includes(deviceLower)) {
        return `/image/device/${filename}`;
      }
    }
  }

  // Fall back to looking for a generic manufacturer-based image
  const genericMfgKey = manufacturerLower.split('_')[0];
  const genericMfgImage = `/image/device/${genericMfgKey}.svg`;

  // Check if a manufacturer-specific generic image exists
  if (hasDeviceImage(genericMfgImage)) {
    return genericMfgImage;
  }

  return `/image/device/unknown.png`;
}

/**
 * Helper to check if a device image likely exists
 * (In a real app, this could be from an asset manifest)
 */
function hasDeviceImage(filename: string): boolean {
  const knownImages = [
    'esp32.svg',
    'esp_now.svg',
    'faketec.svg',
    'heltec_mesh_solar.svg',
    'heltec_meshpocket.svg',
    'heltec_paper.svg',
    'heltec_t114.svg',
    'heltec_v2.svg',
    'heltec_v3.svg',
    'heltec_v4.svg',
    'heltec_wp.svg',
    'heltec_wsl3.svg',
    'heltec_wt3.svg',
    'ikoka_nano.svg',
    'ikoka_stick.svg',
    'keepteen_lt1.svg',
    'lilygo_pager.svg',
    'lilygo_t3s3.svg',
    'lilygo_t5_pro.svg',
    'lilygo_tbeam.svg',
    'lilygo_tbeam_supreme.svg',
    'lilygo_tdeck.svg',
    'lilygo_tdeck_pro.svg',
    'lilygo_tdisplay.svg',
    'lilygo_techo.svg',
    'lilygo_techo_lite.svg',
    'lilygo_tlora_1.6.svg',
    'lilygo_tlora_c6.svg',
    'lora.svg',
    'meshcore.svg',
    'nano_g2.svg',
    'nrf52.svg',
    'rak_11300.svg',
    'rak_4631.svg',
    'rak_wismesh_tag.svg',
    'rpi.svg',
    'rpi_picow.svg',
    'sensecap_solar.svg',
    'sensecap_t1000e.svg',
    'station_g2.svg',
    'thinknode_m1.svg',
    'thinknode_m2.svg',
    'thinknode_m3.svg',
    'thinknode_m5.svg',
    'thinknode_m6.svg',
    'wio_tracker_l1.svg',
    'wio_tracker_l1_eink.svg',
    'xiao_esp32c3.svg',
    'xiao_esp32c6.svg',
    'xiao_esp32s3.svg',
    'xiao_nrf52.svg',
    'yeasu_ft817.jpg',
    'unknown.png',
  ];
  return knownImages.some(
    (img) => filename.includes(img) || img.includes(filename.split('/').pop() || '')
  );
}
