import { http } from '../http'
import { get, post, put } from '../request'

/**
 * @description: 单词测试-保存单词测试结果
 * @param {*} params：
 * @return {*}
 */
interface SaveWordTestData {
  testType?: number
  textbookId?: string
  chapterId?: string
  [key: string]: any
}

interface CompleteWordTestData {
  id: any
  score?: number
  [key: string]: any
}

interface GetWordTestData {
  textbookId?: string
  chapterId?: string
  testType?: number
  questionCount?: number
  [key: string]: any
}

interface WordTestRecordParams {
  current?: number
  size?: number
  [key: string]: any
}

const processMemoryTips = (tips: any) => {
  if (!tips) return
  for (const tip of tips) {
    if (!tip.details) continue
    let text = ''
    let begin = false
    for (const char of tip.details) {
      if (char === "'") {
        if (begin) {
          begin = false
          text += '</strong>'
          text += char
        } else {
          begin = true
          text += char
          text += '<strong>'
        }
      } else {
        text += char
      }
    }
    tip.details = text
  }
}

const _processMemoryTipsArray = (memoryTips: any[]) => {
  if (!memoryTips?.length) return
  for (const item of memoryTips) {
    item.memoryTips = JSON.parse(item.memoryTips)
    processMemoryTips(item.memoryTips)
  }
}

const wordTestApi = {
  /**
   * @description: 单词测试-保存单词测试结果
   * @param {*} params：
   * @return {*}
   */
  saveWordTest: (data: SaveWordTestData) => {
    return http.request({
      url: `/wordTestRecord/saveWordTest`,
      method: 'POST',
      data,
    })
  },

  /**
   * @description: 单词测试-结束单词测试
   * @param {*} params：
   * @return {*}
   */
  completeWordTest: async (data: CompleteWordTestData) => {
    return await put(`/wordTestRecord/completeWordTest`, data)
  },

  /**
   * @description: 单词测试-获取题目
   * @param {GetWordTestData} data：测试参数
   * @return {*}
   */
  getWordTestWordsV2: async (data: GetWordTestData): Promise<any> => {
    const res = (await get<any>('/wordExamRecord/getWords', data)) as any
    _processMemoryTipsArray(res.chooseTitle)
    _processMemoryTipsArray(res.spellTitle)
    return res
  },

  /**
   * @description: 单词测试-获取题目
   * @param {GetWordTestData} data：测试参数
   * @return {*}
   */
  getWordTestWords: async (data: GetWordTestData): Promise<any> => {
    const res = (await post('/wordTestRecord/getWordTestWords2', data)) as any
    _processMemoryTipsArray(res.chooseTitle)
    _processMemoryTipsArray(res.spellTitle)
    return res
  },

  /**
   * @description: 单词测试-测试记录表-查询列表
   * @param {WordTestRecordParams} params：查询参数
   * @return {*}
   */
  getWordTestRecordList: (params: WordTestRecordParams) => {
    return http.request({
      url: `/wordTestRecord/list`,
      params,
    })
  },

  /**
   * @description: 单词测试-测试记录表-详情
   * @param {*} params：
   * @return {*}
   */
  getWordTestRecordDetail: (id: any) => {
    return http.request({
      url: `/wordTestRecord/${id}`,
    })
  },

  checkMedalStatus: (data: { checkFlag: 0 | 1; ids: string }) => {
    return http.request({
      url: `/userMedal/updateCheck`,
      method: 'POST',
      data,
    })
  },
}

export default wordTestApi

export type { SaveWordTestData, CompleteWordTestData, GetWordTestData, WordTestRecordParams }
