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

const activityApi = {
  list, add, getInfo, update, deleteActivity, getCurrent
}

export default activityApi

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

  return processResponse(response)
}

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

  return processResponse(response)
}

async function add(data) {
  const request = {
    name: data.name,
    term: data.term,
    start: data.start,
    end: data.end
  }

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

  return processResponse(response)
}

async function update(id, data) {
  const request = {
    name: data.name === '' ? undefined : data.name,
    term: data.term === '' ? undefined : data.term,
    start: data.start === '' ? undefined : data.start,
    end: data.end === '' ? undefined : data.end
  }

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

  return processResponse(response)
}


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

  return processResponse(response)
}


async function getCurrent() {
  const response = await client.get(apiUrls.activity.current)
  return processResponse(response)
}
