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

const courseScheduleApi = {
  list, add, getInfo, update, clone, deleteCourseSchedule, getScheduleForCourse
}

export default courseScheduleApi

async function list(courseId, term, page, pageSize) {
  const response = await client.get(apiUrls.courseSchedule.list, {
    params: {
      course: courseId,
      page: page,
      pageSize: pageSize,
      term: term
    }
  })

  return processResponse(response)
}

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

  return processResponse(response)
}

async function add(courseId, data) {
  const response = await client.post(apiUrls.admin.courseSchedule.add, data, {
    params: {
       course: courseId,
    }
  })

  return processResponse(response)
}

async function update(id, data) {
  const response = await client.post(
    apiUrls.admin.courseSchedule.update,
    data, {
      params: {id: id}
    })

  return processResponse(response)
}

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

  return processResponse(response)
}

async function clone(id, source, target) {
  const response = await client.post(apiUrls.admin.courseSchedule.clone, qs.stringify({
    course: id,
    from: source,
    to: target
  }))

  return processResponse(response)
}

async function getScheduleForCourse(course, term) {
  const response = await client.get(apiUrls.courseSchedule.fromCourse, {
    params: {
      course: course,
      term: term,
    }
  })

  return processResponse(response)
}
