import {client, processFileDL, processResponse} from '../client';
import {apiUrls} from "app/src/api/api-urls";
import qs from "qs";

const scoreApi = {
  exportScoreExcel,
  student: {
    getStudentAllScore
  },
  teacher: {
    getScheduleStudentScore, updateScore
  }
}

export default scoreApi

async function getStudentAllScore() {
  const response = await client.get(apiUrls.student.score.all)
  return processResponse(response)
}

async function getScheduleStudentScore(courseId, term) {
  const response = await client.get(apiUrls.teacher.score.scheduleScore, {
    params: {
      course: courseId,
      term: term
    }
  })

  return processResponse(response)
}

async function updateScore(courseId, term, scoreData) {
  const response = await client.post(apiUrls.teacher.score.update, scoreData, {
    params: {
      course: courseId,
      term: term,
      scoreId: scoreData.id,
    }
  })

  return processResponse(response)
}

async function exportScoreExcel(courseId, term) {
  const response = await client.get(apiUrls.score.export, {
    params: {
      course: courseId,
      term: term
    },
    responseType: "blob"
  })

  let data = response.data

  if (response.headers.getContentType() !== 'application/octet-stream' || data === undefined) {
    return processResponse(response)
  }

  processFileDL(response)
}
