/* eslint-disable @typescript-eslint/member-ordering */
import str from '@/utils/str'
import { get, post, type PageResponse } from '../request'
import wordTestApi from '../subject/wordTest'
import { BasicService } from './basic'

function getGraspText(val: any, total: any) {
  if (val === 0) return '0'
  return `${val}(${Math.round((val / total) * 100)}%)`
}

class WordTestService extends BasicService<any> {
  private parseJsonItem(item: any) {
    if (typeof item.extra === 'string') {
      item.extra = JSON.parse(item.extra)
    }
    if (typeof item.bonus === 'string') {
      item.bonus = JSON.parse(item.bonus)
    }
    if (typeof item.graspInfo === 'string') {
      const info = JSON.parse(item.graspInfo)
      info.strangeText = getGraspText(info.predictStrangeWordNum, info.totalNum)
      info.knowText = getGraspText(info.predictKnowWordNum, info.totalNum)
      info.knowWellText = getGraspText(info.predictKnowWellWordNum, info.totalNum)
      info.graspText = getGraspText(info.predictGraspWordNum, info.totalNum)
      item.graspInfo = info

      if (typeof item.lastExamWordGraspInfo === 'string') {
        const lastInfo = JSON.parse(item.lastExamWordGraspInfo)
        item.lastExamWordGraspInfo = lastInfo
        info.strangeChange = info.predictStrangeWordNum - lastInfo.predictStrangeWordNum
        if (info.strangeChange > 0) {
          info.strangeChange = `+${info.strangeChange}`
        }
        info.knowChange = info.predictKnowWordNum - lastInfo.predictKnowWordNum
        if (info.knowChange > 0) {
          info.knowChange = `+${info.knowChange}`
        }
        info.knowWellChange = info.predictKnowWellWordNum - lastInfo.predictKnowWellWordNum
        if (info.knowWellChange > 0) {
          info.knowWellChange = `+${info.knowWellChange}`
        }
        info.graspChange = info.predictGraspWordNum - lastInfo.predictGraspWordNum
        if (info.graspChange > 0) {
          info.graspChange = `+${info.graspChange}`
        }
      }
    }

    // 展示的名字
    item.displayName = item.nickName || item.name
    // 打码
    item.redactionDisplayName = str.redaction(item.displayName)
    // 学习的范围
    const stageName = item.extra?.stageName
    if (stageName) {
      item.studyRange = stageName
    } else {
      const book = item.extra?.book
      if (book) {
        item.studyRange = `${book.versionName}-${book.bookName}`
      }
    }
  }

  async getWords(data: any) {
    return await wordTestApi.getWordTestWordsV2(data)
  }

  async getRecordDetail(sessionId: any) {
    const data = await get<any>('/wordExamRecord/getTotalInfoWordsBySessionId', { sessionId })
    this.parseJsonItem(data)
    return data
  }

  // 分页查询
  async findPage<T>(current: number, size: number, condition: any): Promise<PageResponse<T>> {
    const data = (await super.findPage<T>(current, size, condition)) as PageResponse<T>
    for (const item of data.rows as any) {
      this.parseJsonItem(item)
    }
    return data
  }

  async saveWordRecord(data: any) {
    return await post('/wordExamRecord/saveRecord', data)
  }

  async saveTotalRecords(data: any) {
    const res = await post('/wordExamRecord/saveTotalRecord', data)
    this.parseJsonItem(res)
    return res
  }

  /**
   * 获取最近的记录
   */
  async getLatestRecords() {
    const res = await get<any>('/wordExamTotalRecord/getRecentRecords')
    for (const item of res) {
      this.parseJsonItem(item)
    }
    return res
  }

  /**
   * 获得乐贝消耗
   */
  async getConsumeCoin(data: any) {
    return await get<any>('/wordExamRecord/getUserConsume', data)
  }

  protected saveOrUpdateUrl(): string {
    throw new Error('Method not implemented.')
  }

  protected removeUrl(): string {
    throw new Error('Method not implemented.')
  }

  protected findPageUrl(): string {
    return '/wordExamTotalRecord/list'
  }
}

export const wordTestService = new WordTestService()
