function getCurrentTimeLabel() {
  const now = new Date()
  const hours = String(now.getHours()).padStart(2, '0')
  const minutes = String(now.getMinutes()).padStart(2, '0')
  return `${hours}:${minutes}`
}

function createBaseMessage(payload = {}) {
  return {
    direction: payload.direction || 'right',
    department: payload.department || '家长',
    name: payload.name || '我',
    time: payload.time || getCurrentTimeLabel(),
    ...payload
  }
}

export function createTextMessage(payload = {}) {
  return createBaseMessage({
    type: 'text',
    content: payload.content || '',
    needReceipt: Boolean(payload.needReceipt),
    receiptStatus: payload.receiptStatus || 'unread',
    showReceiptToggle: payload.showReceiptToggle ?? false,
    ...payload
  })
}

export function createVoiceMessage(payload = {}) {
  return createBaseMessage({
    type: 'voice',
    duration: payload.duration || '00',
    isRecording: Boolean(payload.isRecording),
    voicePreview: payload.voicePreview || '',
    voiceText: payload.voiceText || '',
    needReceipt: Boolean(payload.needReceipt),
    receiptStatus: payload.receiptStatus || 'unread',
    ...payload
  })
}

export function createImageMessage(payload = {}) {
  return createBaseMessage({
    type: 'image',
    imageUrl: payload.imageUrl || '/static/logo.png',
    ...payload
  })
}

export function createFileMessage(payload = {}) {
  return createBaseMessage({
    type: 'file',
    fileName: payload.fileName || '未命名文件',
    fileUrl: payload.fileUrl || '',
    ...payload
  })
}

export function createVideoMessage(payload = {}) {
  return createBaseMessage({
    type: 'video',
    coverUrl: payload.coverUrl || '/static/logo.png',
    videoUrl: payload.videoUrl || '',
    duration: payload.duration || '',
    ...payload
  })
}

export function collectImageUrls(messages = []) {
  return messages
    .filter((item) => item.type === 'image' && item.imageUrl)
    .map((item) => item.imageUrl)
}

export function getInitialParentMessages() {
  return [
    createTextMessage({
      direction: 'left',
      department: '一年级A班',
      name: '张三',
      time: '15:25',
      content: '你好啊',
      needReceipt: true,
      receiptStatus: 'unread'
    }),
    createBaseMessage({
      type: 'call',
      direction: 'left',
      department: '一年级A班',
      name: '张三',
      time: '15:26',
      duration: '00:25'
    }),
    createBaseMessage({
      type: 'call',
      direction: 'right',
      department: '家长',
      name: '李四',
      time: '15:30',
      duration: '00:25'
    }),
    createVoiceMessage({
      direction: 'left',
      department: '一年级A班',
      name: '张珊',
      time: '15:23',
      duration: '24',
      voicePreview: '这段音频前面的字',
      voiceText: '这是转写这段音频的所有内容，但转成文字的功能暂时不够，设计图先有，转成文字后，"确认阅读"跟着。',
      needReceipt: true,
      receiptStatus: 'unread'
    }),
    createVoiceMessage({
      direction: 'right',
      department: '家长',
      name: '李四',
      time: '18:28',
      duration: '15',
      voicePreview: '这是我发的语音',
      voiceText: '这是我发的语音消息的完整转写内容。',
      needReceipt: true,
      receiptStatus: 'read',
      showReceiptToggle: false
    }),
    createVoiceMessage({
      direction: 'left',
      department: '一年级A班',
      name: '张三',
      time: '17:25',
      duration: '16',
      isRecording: true,
      voicePreview: '正在录音中'
    }),
    createTextMessage({
      direction: 'right',
      department: '家长',
      name: '王五',
      time: '19:30',
      content: '这是我刚发的消息',
      showReceiptToggle: true
    }),
    createTextMessage({
      direction: 'right',
      department: '家长',
      name: '李四',
      time: '19:25',
      content: '这是我之前发的消息，已经勾选了带回执',
      needReceipt: true,
      receiptStatus: 'read',
      showReceiptToggle: false
    }),
    createImageMessage({
      direction: 'left',
      department: '一年级A班',
      name: '班主任',
      time: '19:40',
      imageUrl: '/static/logo.png'
    }),
    createImageMessage({
      direction: 'right',
      department: '家长',
      name: '李四',
      time: '19:40',
      imageUrl: '/static/images/deviceunlogin.png'
    }),
    createFileMessage({
      direction: 'right',
      department: '家长',
      name: '李四',
      time: '19:41',
      fileName: '归档.zip',
      fileUrl: 'https://www.iconfont.cn/api/project/download.zip?spm=a313x.manage_type_myprojects.i1.d7543c303.6a0d3a81LqAtP4&pid=5081296&ctoken=null'
    }),
    createVideoMessage({
      direction: 'left',
      department: '一年级A班',
      name: '班主任',
      time: '19:42',
      coverUrl: '/static/images/deviceunlogin.png',
      videoUrl: 'https://v2.cri.cn/c89e53c6-0bc7-45ca-ac11-a385002d7d11/cb5a6d96-d0c4-4fd0-a895-b6135667d84a/video/2df23555-d075-4988-9d06-0f3564517974.mp4'
    })
  ]
}
