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

import { PaperRecord, HomeworkExam, SubjectPaperRecord } from './type'
export * from './type'

const getUnfinishedHomework = async () => {
  const records = await get<PaperRecord[]>('/school/paper/userRecord/listExamRecord')
  // 利用map优化
  const map = new Map<string, SubjectPaperRecord>()
  records.forEach(item => {
    const record = map.get(item.subjectId)
    if (record) {
      record.records.push(item)
    } else {
      map.set(item.subjectId, {
        subjectId: item.subjectId,
        subjectName: item.subjectName,
        records: [item],
      })
    }
  })
  return map
}

const startExam = async (recordId: any) => {
  return post<HomeworkExam>(`/school/paper/exam`, { recordId })
}

const homeworkApi = { getUnfinishedHomework, startExam }
export default homeworkApi

// 保存题目
const saveRecordAnswer = async (data: any) => {
  return post<HomeworkExam>(`/school/paper/saveRecordAnswer`, data)
}
export { saveRecordAnswer }

// 提交作业
const submitExam = async (data: any) => {
  return post<HomeworkExam>(`/school/paper/submit`, data)
}
export { submitExam }

// 作业记录列表
const getListCompleteRecord = async (data: any) => {
  return get<HomeworkExam>(`/school/paper/userRecord/listCompleteRecord`, data)
}
export { getListCompleteRecord }
