import { get, post, postForm } from '#/api/http'

import { BasicService } from '../basic'
import { questionService } from './question'

class QuestionResourceService extends BasicService {
  async fixAll() {
    return post('/exam/questionResource/fixData')
  }
  async generateExam(id: any) {
    return post(`/exam/questionResource/generateExam/${id}`)
  }

  async getJson(fileId: any) {
    let res = await get<any>('/exam/questionResource/getJsonByFileId', {
      fileId,
    })
    if (typeof res === 'string') {
      res = JSON.parse(res)
    }
    for (const item of res) {
      questionService.clean(item as any)
    }
    return res
  }

  async audit(resourceId: any) {
    return await post(`/question/resource/audit`, { resourceId })
  }

  async getText(url: any) {
    return await get<string>('/exam/questionResource/getContentByUrl', { url })
  }

  async listFiles(resourceId: any) {
    const res = await get<any>(`/exam/questionResource/listFiles`, {
      resourceId,
      size: -1,
    })
    if (res.rows?.length) {
      for (const item of res.rows) {
        if (item.jsonData && typeof item.jsonData === 'string') {
          item.jsonData = JSON.parse(item.jsonData)
          for (const question of item.jsonData) {
            questionService.clean(question)
          }
        }
      }
    }
    return res
  }

  async save(data: any) {
    return await post<any>('/exam/questionResource/saveOrUpdate', data)
  }

  async updateFile(fileId: any, totalNum: any, file: any) {
    return await postForm(`/exam/questionResource/updateFile`, {
      fileId,
      totalNum,
      file,
    })
  }

  async updateTotalNum(fileId: any, totalNum: any) {
    return await postForm(`/exam/questionResource/updateTotalNum`, {
      fileId,
      totalNum,
    })
  }

  async uploadFile(data: any) {
    return await postForm<any>('/exam/questionResource/upLoadFile', data)
  }

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

  protected removeUrl(): string {
    return '/exam/questionResource/batch/delete'
  }

  protected saveOrUpdateUrl(): string {
    return ''
  }
}

export const questionResourceService = new QuestionResourceService()
