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

const teacherManager = {
  list, add, getInfo, update, deleteTeacher
}

export default teacherManager

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

  return processResponse(response)
}

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

  return processResponse(response)
}

async function add(teacherName, username, initialPassword) {
  const request = {
    teacherName: teacherName === '' ? undefined : teacherName,
    username: username === '' ? undefined : username,
    initialPassword: initialPassword === '' ? undefined : initialPassword
  }

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

  return processResponse(response)
}

async function update(id, teacherName, username, initialPassword) {
  const request = {
    teacherName: teacherName === '' ? undefined : teacherName,
    username: username === '' ? undefined : username,
    initialPassword: initialPassword === '' ? undefined : initialPassword
  }

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

  return processResponse(response)
}


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

  return processResponse(response)
}
