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

const courseApi = {
  list, listCourseNames, add, getInfo, update, deleteCourse, exportStudentExcel,
  student: {
    getCandidate, getSelected, getOfficial, checkSelected, select, unselect
  },
  teacher: {
    getTeacherCourseSchedule, getCourseForTeacher,
  }
}

export default courseApi

/**
 *
 * @param page
 * @param pageSize
 * @returns {Promise<*>}
 */
async function list(page, pageSize) {
  const response = await client.get(apiUrls.course.list, {
    params: {
      page: page,
      pageSize: pageSize
    }
  })

  return processResponse(response)
}

/**
 *
 * @param page
 * @param pageSize
 * @returns {Promise<{
 *     "current": number,
 *     "pageSize": number,
 *     "total": number,
 *     "result": {"id": number, "name": string}[]
 *   }>}
 */
async function listCourseNames(page, pageSize) {
  const response = await client.get(apiUrls.course.listNames, {
    params: {
      page: page,
      pageSize: pageSize
    }
  })

  return processResponse(response)
}

async function getInfo(id) {
  const response = await client.get(apiUrls.course.info, {
    params: {id: id}
  })

  return processResponse(response)
}

async function add(data) {
  const request = {
    name: data.name ? data.name: 'undefined',
    courseHours: data.courseHours ? data.courseHours: '0',
    examRatio: data.examRatio ? data.examRatio: '1.0',
    springTerm: data.springTerm ? data.springTerm: 'false',
  }

  const response = await client.post(apiUrls.admin.course.add, request)

  return processResponse(response)
}

async function update(id, data) {
  const request = {
    name: data.name ? data.name : undefined,
    courseHours: data.courseHours ? data.courseHours : undefined,
    examRatio: data.examRatio ? data.examRatio : undefined,
    springTerm: data.springTerm ? data.springTerm : undefined,
  }

  const response = await client.post(
    apiUrls.admin.course.update,
    request, {
      params: {id: id}
    })

  return processResponse(response)
}

async function deleteCourse(id) {
  const response = await client.post(apiUrls.admin.course.delete, undefined, {
    params: {id: id}
  })

  return processResponse(response)
}

/**
 *
 * @param act {number}
 * @returns {Promise<*>}
 */
async function getCandidate(act) {
  const response = await client.get(apiUrls.student.course.candidate, {
    params: {
      act: act
    }
  })

  return processResponse(response)
}

async function getSelected(term) {
  const response = await client.get(apiUrls.student.course.selected, {
    params: {
      term: term
    }
  })
  return processResponse(response)
}

async function getOfficial(term) {
  const response = await client.get(apiUrls.student.course.official, {
    params: {
      term: term
    }
  })

  return processResponse(response)
}

async function checkSelected(id) {
  const response = await client.get(apiUrls.student.course.checkSelected, {
    params: {id: id}
  })

  return processResponse(response)
}

async function select(schedule, activity) {
  const data = {
    schedule: schedule,
    activity: activity
  }
  const response = await client.post(apiUrls.student.course.select, qs.stringify(data))

  return processResponse(response)
}

async function unselect(id) {
  const response = await client.post(apiUrls.student.course.unselect, undefined, {
    params:{id: id}
  })

  return processResponse(response)
}

async function getTeacherCourseSchedule(term) {
  const response = await client.get(apiUrls.teacher.course.list, {
    params: {
      term: term
    }
  })

  return processResponse(response)
}

async function exportStudentExcel(id, scheduleName) {
  const response = await client.get(apiUrls.courseSchedule.studentList, {
    params: {id: id},
    responseType: "blob"
  })

  let data = response.data

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

  const a = document.createElement('a')
  a.style.display = 'none'
  a.href = window.URL.createObjectURL(new Blob([data]))
  a.setAttribute('download', `export_students_${scheduleName}_${id}.xlsx`)
  document.body.appendChild(a)
  a.click()
  window.URL.revokeObjectURL(a.href)
  document.body.removeChild(a)

  return undefined
}

async function getCourseForTeacher() {
  const response = await client.get(apiUrls.teacher.course.courseForTeacher)

  return processResponse(response)
}
