import { pigeonHttp, cowHttp, frogHttp } from '@/lib/http/instances'
import {
  AiUserImBaseInfoOutput,
  GetIMAccountResponse,
  PageParams,
  GiftListInput,
  HeartbeatLevelOutput,
  ChatSetOutput,
  ChatBubbleListOutput,
  RtcOperation,
  BackgroundImgListOutput,
  HeartbeatRelationListInput,
  PageHeartbeatRelationListOutput,
  ChatModelDictOutput,
} from './types'

export const imService = {
  // 获取账号登录信息信息
  getIMAccount: (): Promise<GetIMAccountResponse> => {
    return pigeonHttp.post('/web/im-user/get-account')
  },

  // 语音转文本
  getTextFromAsrVoice: ({
    data,
    aiId,
  }: {
    data: string
    aiId: number
  }): Promise<{ content: string }> => {
    return cowHttp.post('/web/voice/asr-v3', { data, aiId, suffix: '.webm' })
  },

  // 获取IM用户信息
  getIMUserInfo: ({ aiId }: { aiId: number }): Promise<AiUserImBaseInfoOutput> => {
    return frogHttp.post('/web/ai-user-search/im-base-info', { aiId }, { ignoreError: true })
  },

  getShareUserInfo: ({ aiId }: { aiId: number }): Promise<AiUserImBaseInfoOutput> => {
    return frogHttp.post('/web/ai-user-search/h5-info', { aiId }, { ignoreError: true })
  },

  // 获取礼物列表
  getGiftList: (params: PageParams): Promise<GiftListInput> => {
    return frogHttp.post('/web/gift/dict-list', { page: params })
  },

  // 发送礼物
  sendGift: (data: { giftId: string; aiId: number; num: number }): Promise<void> => {
    return frogHttp.post('/web/ai-user-gift/send', { ...data, scene: 'IM' })
  },

  // 展示关系开关
  setShowRelationship: (data: { aiId: number; isShow: boolean }): Promise<void> => {
    return frogHttp.post('/web/ai-user/heartbeat-relation-switch', data)
  },

  // 获取心动等级
  getHeartbeatLevel: (aiId: number): Promise<HeartbeatLevelOutput> => {
    return frogHttp.post('/web/ai-user/heartbeat-level', { aiId })
  },

  // 购买已扣减的心动值
  buyHeartbeat: (data: { aiId: number; heartbeatVal: number }): Promise<void> => {
    return frogHttp.post('/web/ai-user/buy-heartbeat-val', data)
  },

  // 获取我的聊天设置
  getMyChatSetting: (aiId: number): Promise<ChatSetOutput> => {
    return frogHttp.post('/web/chat-set/get-my', { aiId })
  },

  // 设置我的聊天设定
  setMyChatSetting: (data: {
    aiId: number
    nickname: string
    birthday: number
    whoAmI: string
  }): Promise<void> => {
    return frogHttp.post('/web/chat-set/set', data)
  },

  // 设置聊天气泡字典列表
  setChatBubble: (data: { aiId: number; code: string }): Promise<void> => {
    console.log('data', data)
    return frogHttp.post('/web/chat-set/set-chat-bubble', data)
  },

  // 获取聊天气泡字典列表
  getChatBubbleDictList: (aiId: number): Promise<ChatBubbleListOutput[]> => {
    return frogHttp.post('/web/chat-set/get-chat-bubble-list', { aiId })
  },

  // 获取聊天背景列表
  getChatBackgroundList: (aiId: number): Promise<BackgroundImgListOutput[]> => {
    return frogHttp.post('/web/chat-background/list', { aiId })
  },

  // 批量添加背景
  batchAddBackground: (data: {
    aiId: number
    images: { url: string; width: number; height: number }[]
  }): Promise<void> => {
    return frogHttp.post('/web/chat-background/batch-add', data)
  },

  // 设置聊天背景
  setChatBackground: (data: { aiId: number; backgroundId?: number | string }): Promise<void> => {
    return frogHttp.post('/web/chat-background/set-background', data)
  },

  // 删除聊天背景
  delChatBackground: (data: { backgroundId: number }): Promise<void> => {
    return frogHttp.post('/web/chat-background/del', data)
  },

  // 获取RTC token
  getRtcToken: (data: { roomId: string }): Promise<{ token: string }> => {
    return cowHttp.post('/web/voice-chat/gen-rtc-tk', data)
  },

  // 开始，打断，结束语音通话
  doRtcOperation: (
    data: {
      roomId: string
      optType: RtcOperation
      aiId: number
      taskId: string
      duration?: number
    },
    signal?: AbortSignal
  ): Promise<void> => {
    return cowHttp.post('/web/voice-chat/opt', data, { ignoreError: true, signal })
  },

  // 点赞点踩反馈
  doFeedback: (data: {
    aiId: number
    messageId: string
    content: string
    optType: 1 | 2 | 0
  }): Promise<void> => {
    return pigeonHttp.post('/web/fb/v1', data, { ignoreError: true })
  },

  // 生成辅助对话内容-V2版本
  genSupContentV2: (data: {
    aiId: number
    batchNo?: string
    excContentList?: string[]
  }): Promise<{ batchNo: string; contentList: string[] }> => {
    return cowHttp.post('/web/gen/sup-content', data, { ignoreError: true })
  },

  // 获取心动关系列表
  getHeartbeatRelationList: (
    data: HeartbeatRelationListInput
  ): Promise<PageHeartbeatRelationListOutput> => {
    return frogHttp.post('/web/ai-user/heartbeat-relation-list', data)
  },

  // 发送对话开场白消息
  sendDialoguePrologueMessage: (data: { aiId: number }): Promise<void> => {
    return frogHttp.post('/web/chat/send-dialogue-prologue-message', data)
  },

  // 获取当前用户的心动值排名
  getHeartbeatRank: (): Promise<number> => {
    return frogHttp.post('/web/ai-user/heartbeat-rank', {})
  },

  // 获取聊天模型字典列表
  getChatModelDictList: (): Promise<ChatModelDictOutput[]> => {
    return frogHttp.post('/web/chat-model/dict-list', {})
  },

  // 删除对话
  deleteConversations: (data: { aiIdList: number[] }): Promise<void> => {
    return cowHttp.post('/web/ai-message/del', data)
  },

  // 用户超过3分钟停留没有发消息了生成自动聊天内容并发送IM消息
  genAutoChatContent: (data: { aiId: number }): Promise<void> => {
    return cowHttp.post('/web/g/at-ct', data, { ignoreError: true })
  },
}
