import type { WorkbenchList, SelectionDetail } from '@/types/workbench'
import { CARS } from './cars'
import { MISSIONS } from './missions'
import { SITES } from './sites'
import { TRACKS } from './tracks'

export function mockWorkbenchList(nav: string): WorkbenchList {
  switch (nav) {
    case 'map':
      return {
        nav: 'map',
        columns: ['ID', '名称', '类型', '图层'],
        rows: [
          { id: '1', name: 'factory_floor', type: 'Map', status: 'g', cells: { 图层: 'g' } },
          { id: '2', name: 'grid_overlay', type: 'Map', status: 'overlay', cells: { 图层: 'overlay' } }
        ]
      }
    case 'process':
      return {
        nav: 'process',
        columns: ['名称', '状态', '类型'],
        rows: MISSIONS.map((m) => ({
          id: m.id.replace('M', ''),
          name: m.name,
          type: m.typeName.split('.').pop() ?? m.typeName,
          status: m.status,
          cells: { autoStart: 'true' }
        }))
      }
    case 'vehicle':
      return {
        nav: 'vehicle',
        columns: ['ID', '地址', '名称', '概况'],
        rows: CARS.map((c, i) => ({
          id: String(i + 1),
          name: c.name,
          type: c.typeName.split('.').pop() ?? c.typeName,
          status: c.state,
          overview: c.state,
          cells: { 地址: c.ip ?? '-', 图层: c.group ?? 'g' }
        }))
      }
    case 'script':
      return {
        nav: 'script',
        columns: ['车辆', '脚本名', '状态'],
        rows: CARS.map((c, i) => ({
          id: String(i + 1),
          name: `${c.name} (#${i + 1})`,
          type: c.missionId ? 'MoveProgram' : '(无)',
          status: c.state === 'running' ? 'Running' : '-',
          cells: { 脚本: c.missionId ? 'MoveProgram' : '(无)', 状态: c.state }
        }))
      }
    case 'scene':
      return {
        nav: 'scene',
        columns: ['ID', '名称', '类型', '信息'],
        rows: [
          ...SITES.map((s) => ({
            id: s.id.replace('S', ''),
            name: s.name,
            type: 'UISite',
            status: `(${s.x}, ${s.y})`,
            cells: { 坐标: `(${s.x}, ${s.y})` }
          })),
          ...TRACKS.map((t) => ({
            id: t.id.replace('T', ''),
            name: t.id,
            type: t.kind,
            status: `${t.fromSiteId} → ${t.toSiteId}`,
            cells: { 方向: 'Forward' }
          }))
        ]
      }
    default:
      return { nav: nav as WorkbenchList['nav'], columns: [], rows: [] }
  }
}

export function mockSelectionDetail(kind: string, id: string, tab: string): SelectionDetail {
  const car = CARS.find((_, i) => String(i + 1) === id || _.id === `C${id.padStart(2, '0')}`)
  const mission = MISSIONS.find((m) => m.id.replace('M', '') === id || m.id === `M${id.padStart(2, '0')}`)
  const base = {
    objectKind: kind,
    objectId: id,
    tab: tab as SelectionDetail['tab'],
    memberFields: [] as SelectionDetail['memberFields'],
    customFields: [] as SelectionDetail['customFields'],
    statusLines: [] as SelectionDetail['statusLines'],
    actions: [] as SelectionDetail['actions']
  }

  if ((kind === 'vehicle' || kind === 'car' || kind === 'script') && car) {
    if (tab === 'status') {
      return {
        ...base,
        name: car.name,
        typeName: car.typeName,
        layer: car.group,
        summary: [{ key: 'ID', value: car.id }, { key: '类型', value: car.typeName }],
        statusLines: [
          { key: '地址', value: car.ip ?? '-' },
          { key: '概况', value: car.state },
          { key: '电量', value: `${Math.round(car.batterySoc * 100)}%` },
          { key: '位姿', value: `(${car.x}, ${car.y}, ${car.theta}°)` }
        ]
      }
    }
    if (tab === 'action') {
      return {
        ...base,
        name: car.name,
        typeName: car.typeName,
        summary: [{ key: 'ID', value: car.id }],
        actions: [
          { id: 'pause', label: '暂停' },
          { id: 'resume', label: '恢复' },
          { id: 'gohome', label: '回充' }
        ]
      }
    }
    return {
      ...base,
      name: car.name,
      typeName: car.typeName,
      layer: car.group,
      summary: [
        { key: '站点', value: '—' },
        { key: '概况', value: car.state },
        { key: '速度', value: '0.80' },
        { key: '位姿', value: `(${car.x}, ${car.y}, ${car.theta}°)` }
      ],
      memberFields: [
        { key: 'name', value: car.name, locked: false },
        { key: 'address', value: car.ip ?? '', locked: false }
      ],
      customFields: [{ key: 'group', value: car.group ?? '', locked: false }]
    }
  }

  if ((kind === 'process' || kind === 'mission') && mission) {
    return {
      ...base,
      name: mission.name,
      typeName: mission.typeName,
      summary: [
        { key: '显示名', value: mission.typeName.split('.').pop() ?? mission.typeName },
        { key: 'autoStart', value: 'true' },
        { key: '状态', value: mission.status }
      ],
      statusLines: tab === 'status'
        ? [{ key: '状态', value: mission.status }, { key: '优先级', value: `${mission.priority}` }]
        : [],
      actions: tab === 'action' ? [{ id: 'start', label: '启动' }, { id: 'stop', label: '停止' }] : []
    }
  }

  return {
    ...base,
    name: id,
    typeName: kind,
    summary: [{ key: '提示', value: '未找到对象或 Mock 数据不完整' }]
  }
}
