/**
 * API 封装层 - 迎宾机器人屏幕端
 *
 * 所有接口统一在此文件封装，一期使用 Mock 数据返回 Promise
 * 后续替换为真实接口时，只需修改本文件中对应的方法实现
 */

import {
  mockRobotStatus,
  mockScreenConfig,
  mockPlayPlan,
  mockWelcomeContent,
  mockBroadcastState,
  mockBroadcastContent,
  mockCommands,
  mockAppointments,
  mockIdCardResult,
  mockRecognitionResult,
  mockDestinations,
  mockNotices,
  mockNavigationStatus,
  mockSystemInfo,
  mockScreenTheme
} from '@/mock/screen'

// API 基础地址（后续替换为真实后端地址）
const BASE_URL = '/api'

// 模拟网络延迟
const delay = (ms = 300) => new Promise(resolve => setTimeout(resolve, ms))

// ============================================
// 屏幕配置与状态
// ============================================

/**
 * 获取屏幕配置
 */
export async function getScreenConfig() {
  await delay()
  return { ...mockScreenConfig }
}

/**
 * 获取机器人状态
 */
export async function getRobotStatus() {
  await delay()
  return { ...mockRobotStatus }
}

/**
 * 获取播放方案
 */
export async function getPlayPlan() {
  await delay()
  return { ...mockPlayPlan }
}

/**
 * 获取欢迎页兜底内容
 */
export async function getWelcomeContent() {
  await delay()
  return { ...mockWelcomeContent }
}

/**
 * 获取屏幕主题配置
 */
export async function getScreenTheme() {
  await delay()
  return { ...mockScreenTheme }
}

/**
 * 获取播报状态
 */
export async function getBroadcastState() {
  await delay()
  return { ...mockBroadcastState }
}

/**
 * 获取播报内容
 */
export async function getBroadcastContent() {
  await delay()
  return { ...mockBroadcastContent }
}

// ============================================
// 语音指令
// ============================================

/**
 * 获取最新指令
 */
export async function getLatestCommand() {
  await delay(100)
  // 模拟随机返回指令
  if (Math.random() > 0.9) {
    return mockCommands[Math.floor(Math.random() * mockCommands.length)]
  }
  return null
}

/**
 * 指令回执
 */
export async function ackCommand(commandId) {
  await delay()
  return { success: true, commandId }
}

// ============================================
// 访客登记
// ============================================

/**
 * 读取身份证
 */
export async function readIdCard() {
  await delay(1000)
  return { ...mockIdCardResult }
}

/**
 * 预约查询
 */
export async function queryAppointment(params) {
  await delay(800)
  const { mobile, idCardNo } = params
  // 根据手机号或身份证号查询
  const appointment = mockAppointments.find(
    a => a.mobile === mobile || a.idCardNo === idCardNo
  )
  if (appointment) {
    return appointment
  }
  throw new Error('未查询到预约记录')
}

/**
 * 提交访客登记
 */
export async function submitVisitorRegistration(data) {
  await delay(1000)
  // 模拟成功返回
  return {
    success: true,
    visitorId: 'V' + Date.now(),
    registrationNo: 'REG' + Date.now(),
    visitorName: data.visitorName,
    registrationTime: new Date().toISOString()
  }
}

/**
 * 获取最新识别结果
 */
export async function getRecognitionResult() {
  await delay(500)
  return { ...mockRecognitionResult }
}

// ============================================
// 路线引导
// ============================================

/**
 * 获取目的地列表
 */
export async function getDestinations() {
  await delay()
  return [...mockDestinations]
}

/**
 * 开始导航
 */
export async function startNavigation(params) {
  await delay(500)
  return {
    taskId: 'nav_' + Date.now(),
    destinationId: params.destinationId,
    status: 'starting'
  }
}

/**
 * 获取导航状态
 */
export async function getNavigationStatus(params) {
  await delay()
  return { ...mockNavigationStatus, taskId: params.taskId }
}

/**
 * 取消导航
 */
export async function cancelNavigation(params) {
  await delay(300)
  return { success: true, taskId: params.taskId }
}

// ============================================
// 通知公告
// ============================================

/**
 * 获取通知公告列表
 */
export async function getNotices() {
  await delay()
  return [...mockNotices]
}

/**
 * 获取通知详情
 */
export async function getNoticeDetail(id) {
  await delay()
  const notice = mockNotices.find(n => n.id === id)
  if (notice) {
    return { ...notice }
  }
  throw new Error('公告不存在')
}

// ============================================
// 呼叫工作人员
// ============================================

/**
 * 呼叫工作人员
 */
export async function callStaff(params) {
  await delay(1000)
  return {
    success: true,
    callId: 'CALL' + Date.now(),
    message: '已通知工作人员，请稍候'
  }
}

// ============================================
// 系统信息
// ============================================

/**
 * 获取系统信息
 */
export async function getSystemInfo() {
  await delay()
  return { ...mockSystemInfo }
}

// ============================================
// 摄像头
// ============================================

/**
 * 获取摄像头预览地址
 */
export async function getCameraPreviewUrl() {
  await delay()
  return {
    streamUrl: 'rtsp://localhost:8554/camera',
    streamType: 'rtsp',
    expireTime: Date.now() + 300000
  }
}

// ============================================
// 音量控制
// ============================================

/**
 * 音量控制
 */
export async function setAudioControl(params) {
  await delay()
  return { success: true, ...params }
}

// ============================================
// 事件上报
// ============================================

/**
 * 上报屏幕事件
 */
export async function reportEvent(data) {
  await delay(100)
  console.log('[Event Report]', data)
  return { success: true }
}

// 导出所有 API 方法
export default {
  getScreenConfig,
  getRobotStatus,
  getPlayPlan,
  getWelcomeContent,
  getScreenTheme,
  getBroadcastState,
  getBroadcastContent,
  getLatestCommand,
  ackCommand,
  readIdCard,
  queryAppointment,
  submitVisitorRegistration,
  getRecognitionResult,
  getDestinations,
  startNavigation,
  getNavigationStatus,
  cancelNavigation,
  getNotices,
  getNoticeDetail,
  callStaff,
  getSystemInfo,
  getCameraPreviewUrl,
  setAudioControl,
  reportEvent
}
