import { ApiResponse, ApiResponseList } from "@/types";
import flyio from "../flyio";
import type { Batch, ClassKnowledgeRecord, DirectedSchool, Examinee, ExamPaper, ExamPaperSubmit, FavoriteQuestion, FavoriteQuestionListRequestDTO, GetExamPaperRequestDTO, Knowledge, KnowledgeListRequestDTO, KnowledgeRecord, OpenExamineeRequestDTO, PaperWork, PaperWorkRecord, PaperWorkRecordDetail, PaperWorkRecordQuery, PracticeHistory, PracticeRecord, SimulatedRecord, SimulationExamSubject, SimulationTestInfo, StudentExamRecord, StudentPlanStudyRecord, StudentSubject, StudentVideoRecord, StudyPlan, Subject, SubjectListRequestDTO, TeachClass, VHSPaper, VHSPaperListRequestDTO, VideoCourse, VideoCourseKnowledge, VideoCoursePlayInfo, VideoCourseRecordDTO, VideoCourseRequestDTO, VideoCourseSubject, VideoCourseSubjectRequestDTO, VideoStudy, WrongBookQuestion, WrongBookQuestionRequestDTO } from "@/types/study";
import { EnumPaperWorkState } from "@/common/enum";

/**
 * 获取学习计划
 * @param params 
 * @returns 
 */
export function getStudyPlan() {
  return flyio.get('/front/student/plan') as Promise<ApiResponse<StudyPlan>>;
}

/**
 * 保存学习计划
 * @param params 
 * @returns 
 */
export function saveStudyPlan(params: StudyPlan) {
  return flyio.post('/front/student/plan', params) as Promise<ApiResponse<any>>;
}

/**
 * 获取学习计划统计
 * @param params 
 * @returns 
 */
export function getStudyPlanStats(params: any) {
  return flyio.get('/front/student/plan/stats', params) as Promise<ApiResponse<any>>;
}


/**
 * 获取定向学校列表
 * @param params 
 * @returns 
 */
export function getDirectedSchool() {
  return flyio.get('/front/student/directed/school') as Promise<ApiResponse<DirectedSchool[]>>;
}


/**
 * 保存定向学校
 * @param params 
 * @returns 
 */
export function saveDirectedSchool(params: DirectedSchool[]) {
  return flyio.post('/front/student/directed/school', params) as Promise<ApiResponse<any>>;
}


/**
 * 获取科目列表
 * @param params 
 * @returns 
 */
export function getSubjectList(params: SubjectListRequestDTO) {
  return flyio.get('/front/paper/subject', params) as Promise<ApiResponse<Subject[]>>;
}

/**
 * 获取科目下的知识点
 * @param params 
 * @returns 
 */
export function getKnowledgeList(params: KnowledgeListRequestDTO) {
  return flyio.get('/front/paper/knowledge', params) as Promise<ApiResponse<Knowledge[]>>;
}

/**
 * 获取教材同步知识点
 * @param params 
 * @returns 
 */
export function getTextbooksKnowledgeList() {
  return flyio.get('/front/paper/courseKnowledge', {}) as Promise<ApiResponse<Knowledge[]>>;
}

/**
 * 开卷
 * @param params 
 * @returns 
 */
export function getOpenExaminee(params: OpenExamineeRequestDTO) {
  return flyio.post('/front/exam/openExaminee', null, { params }, {
    headers: {
      'Content-Type': 'application/www-form-urlencoded'
    }
  }) as Promise<ApiResponse<Examinee>>;
}

/**
 * 获取试卷
 * @param params 
 * @returns 
 */
export function getPaper(params: GetExamPaperRequestDTO) {
  return flyio.get('/front/paper/paper', params) as Promise<ApiResponse<ExamPaper>>;
}

/**
 * 提交试卷
 * @param params 
 * @returns 
 */
export function commitExamineePaper(params: ExamPaperSubmit) {
  return flyio.post('/front/exam/commitExamineePaper', params) as Promise<ApiResponse<any>>;
}

/**
 * 获取试卷结果
 */
export function getExamineeResult(examineeId: number) {
  return flyio.get('/front/exam/loadExaminee', { examineeId }) as Promise<ApiResponse<Examinee>>;
}

/**
 * 对口升学-获取真题&模拟试卷
 * @param params 
 * @returns 
 */
export function getVHSPaperList(params: VHSPaperListRequestDTO) {
  return flyio.get('/front/paper/list', params) as Promise<ApiResponse<VHSPaper[]>>;
}

/**
 * 获取模拟考试信息
 * @param params 
 * @returns 
 */
export function getSimulationInfo() {
  return flyio.get('/front/student/simulated/info') as Promise<ApiResponse<SimulationTestInfo>>;
}


/**
 * 收藏题目
 */
export function collectQuestion(questionId: number) {
  return flyio.post('/front/questions/collect', null, { params: { questionId } }) as Promise<ApiResponse<any>>;
}

/**
 * 取消收藏题目
 */
export function cancelCollectQuestion(questionId: number) {
  return flyio.post('/front/questions/cancelCollect', null, { params: { questionId } }) as Promise<ApiResponse<any>>;
}

/**
 * 获取知识点学习记录
 * @param params 
 * @returns 
 */
export function getKnowledgeRecord() {
  return flyio.get('/front/student/record/knowledge') as Promise<ApiResponseList<KnowledgeRecord>>;
}


/**
 * 获取模拟考试记录
 * @param params 
 * @returns 
 */
export function getSimulatedRecord() {
  return flyio.get('/front/student/record/simulated') as Promise<ApiResponse<SimulatedRecord[]>>;
}


/**
 * 获取计划学习记录
 * @param params 
 * @returns 
 */
export function getPlanStudyRecord(params: { year: number, month: number }) {
  return flyio.get('/front/student/record/planStudy', params) as Promise<ApiResponse<PracticeRecord>>;
}

/**
 * 获取视频学习记录
 * @param params 
 * @returns 
 */
export function getVideoStudyRecord() {
  return flyio.get('/front/student/record/video') as Promise<ApiResponse<VideoStudy>>;
}


/**
 * 获取模拟考试科目
 * @param params 
 * @returns 
 */
export function getSimulationExamSubjects() {
  return flyio.get('/front/exam/subjects') as Promise<ApiResponse<SimulationExamSubject[]>>;
}

/**
 * 开始考试
 * @param paperId 
 * @returns 
 */
export function beginExaminee(examineeId: number) {
  return flyio.get('/front/exam/beginExaminee', { examineeId }) as Promise<ApiResponse<Examinee>>;
}


/**
 * 纠错
 * @param params 
 * @returns 
 */
export function correctQuestion(params: { questionid: number, remark: string }) {
  return flyio.post('/front/adjustWrong/correctQuestion', params) as Promise<ApiResponse<any>>;
}

/**
 * 获取计划学习记录
 * @param params 
 * @returns 
 */
export function getPracticeHistory({ pageNum, pageSize }: { pageNum: number, pageSize: number }) {
  return flyio.get('/front/student/record/practice', {
    pageNum,
    pageSize
  }) as Promise<ApiResponseList<PracticeHistory>>;
}

/**
 * 获取教材同步练习记录
 * @param params 
 * @returns 
 */
export function getTextbooksPracticeHistory() {
  return flyio.get('/front/student/record/coursePractice', {}) as Promise<ApiResponseList<PracticeHistory>>;
}

export function getPaperWorkList(parmas: { state?: EnumPaperWorkState }) {
  return flyio.get('/front/student/record/test', parmas) as Promise<ApiResponseList<PaperWork>>;
}

export function getPaperWorkDetail(id: number) {
  return flyio.get(`/front/student/record/test/${id}`) as Promise<ApiResponse<Examinee>>;
}

export function getPaperWorkStatistic() {
  return flyio.get('/front/student/paperStats', {}) as Promise<ApiResponse<Record<string, number>[]>>;
}

// 老师端 API
/**
 * 获取教学班级列表
 */
export function getTeachClassList(params: any) {
  return flyio.get('/front/teacher/classes', params) as Promise<ApiResponse<TeachClass[]>>;
}

export function getClassKnowledgeRecord(params: any) {
  return flyio.get('/front/teacher/record/knowledge', params) as Promise<ApiResponse<ClassKnowledgeRecord>>;
}

export function getStudentKnowledgeRecord(params: any) {
  return flyio.get(`/front/teacher/record/knowledge/${params.recordId}`, params) as Promise<ApiResponseList<KnowledgeRecord>>;
}

export function getClassPlanStudyRecord(params: any) {
  return flyio.get('/front/teacher/record/planStudy', params) as Promise<ApiResponseList<StudentPlanStudyRecord>>;
}

export function getStudentPracticeRecord(params: any) {
  return flyio.get(`/front/teacher/record/planStudy/${params.recordId}`, params) as Promise<ApiResponse<PracticeRecord>>;
}

export function getClassVideoStudyRecord(params: any) {
  return flyio.get('/front/teacher/record/video', params) as Promise<ApiResponseList<StudentVideoRecord>>;
}

export function getStudentVideoStudyRecord(params: any) {
  return flyio.get(`/front/teacher/record/video/${params.recordId}`, params) as Promise<ApiResponse<VideoStudy>>;
}

export function getClassExamRecord(params: any) {
  return flyio.get('/front/teacher/record/simulated', params) as Promise<ApiResponse<StudentExamRecord[]>>;
}

export function getStudentExamRecord(params: any) {
  return flyio.get(`/front/teacher/record/simulated/${params.classId}`, params) as Promise<ApiResponse<StudentExamRecord[]>>;
}

export function getStudentExamSubjectRecord(params: any) {
  return flyio.get(`/front/teacher/record/simulated/subject/${params.studentId}`, params) as Promise<ApiResponse<StudentExamRecord[]>>;
}

export function getStudentExamSubjectDetail(params: any) {
  return flyio.get('/front/teacher/record/simulated/knowledge', params) as Promise<ApiResponse<StudentExamRecord[]>>;
}

export function getBatchList(params: any) {
  return flyio.get('/front/teacher/batchs', params) as Promise<ApiResponse<Batch[]>>;
}

/**
 * 获取教师科目列表
 * @param params 
 * @returns 
 */
export function getTeacherSubjectList(params: any) {
  return flyio.get('/front/teacher/subjects', params) as Promise<ApiResponse<Subject[]>>;
}

export function getTeacherTestRecord(params: any) {
  return flyio.get('/front/teacher/record/test', params) as Promise<ApiResponse<PaperWorkRecord[]>>;
}


export function getTeacherTestRecordDetail(params: any) {
  return flyio.get('/front/teacher/record/test/detail', params) as Promise<ApiResponse<PaperWorkRecordDetail[]>>;
}

export function getTeacherTestRecordCondition(params: any) {
  return flyio.get('/front/teacher/record/test/cond', params) as Promise<ApiResponse<PaperWorkRecordQuery>>;
}

/**
 * 获取收藏题目列表
 * @param params 
 * @returns 
 */
export function getFavoriteQuestionList(params: FavoriteQuestionListRequestDTO) {
  return flyio.get('/front/favorites/questions', params) as Promise<ApiResponseList<FavoriteQuestion>>;
}

/**
 * 获取学生科目
 * @param params 
 * @returns 
 */
export function getStudentSubject() {
  return flyio.get('/front/student/subject') as Promise<ApiResponse<StudentSubject[]>>;
}

/**
 * 获取错题本列表
 * @param params 
 * @returns 
 */
export function getWrongBookList(params: WrongBookQuestionRequestDTO) {
  return flyio.get('/front/v2/wrongBook/wrongQuestions', params) as Promise<ApiResponseList<WrongBookQuestion>>;
}

/**
 * 删除错题
 * @param params 
 * @returns 
 */
export function deleteWrongQuestion(questionId: number) {
  return flyio.post('/front/v2/wrongBook/deleteWrongQuestion', null, { params: { questionId } }) as Promise<ApiResponse<any>>;
}


/**
 * 获取视频课程科目列表
 * @param params 
 * @returns 
 */
export function getVideoCourseSubjects(params: VideoCourseSubjectRequestDTO) {
  return flyio.get('/front/videoCourse/subjects', params) as Promise<ApiResponseList<VideoCourseSubject>>;
}

/**
 * 获取视频课程知识点
 * @param params 
 * @returns 
 */
export function getVideoCourseKnowledges(subject: number) {
  return flyio.get('/front/videoCourse/knowledges', { subject }) as Promise<ApiResponseList<VideoCourseKnowledge[]>>;
}

/**
 * 获取视频课程列表
 * @param params 
 * @returns 
 */
export function getVideoCourseList(params: VideoCourseRequestDTO) {
  return flyio.get('/front/videoCourse/video/list', params) as Promise<ApiResponseList<VideoCourse>>;
}

/**
 * 获取视频课程播放信息
 * @param videoId 
 * @returns 
 */
export function getVideoCoursePlayInfo(videoId: string) {
  return flyio.get('/front/comm/vod/getVideoPlayInfo', { videoId }) as Promise<ApiResponse<VideoCoursePlayInfo>>;
}

/**
 * 保存视频课程学习记录
 * @param params 
 * @returns 
 */
export function saveVideoCourseRecord(params: VideoCourseRecordDTO) {
  return flyio.post('/front/videoCourse/saveWatchRecord', null, { params }, {
    headers: {
      'Content-Type': 'application/www-form-urlencoded'
    }
  }) as Promise<ApiResponse<any>>;
}