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

const activityTargetApi = {
  list, add, getInfo, update, deleteActivityTarget
}

export default activityTargetApi

/**
 *
 * @param act {number}
 * @returns {Promise<*>}
 */
async function list(act) {
  const response = await client.get(apiUrls.admin.activity.targets.list, {
    params: {
      act: act
    }
  })

  return processResponse(response)
}
/**
 * @typedef {{
 *         "id": number,
 *         "activity": number,
 *         "grade": number,
 *         "major": number,
 *         "course": number[],
 *         "createTime": string,
 *         "editTime": string
 *     }} ActivityTarget
 */

/**
 *
 * @param id {number}
 * @returns {Promise<ActivityTarget>}
 */
async function getInfo(id) {
  const response = await client.get(apiUrls.admin.activity.targets.info, {
    params: {
      id: id
    }
  })

  return processResponse(response)
}

/**
 * @param act {number}
 * @param data {{
 *   "grade": number,
 *   "major": number,
 *   "course": number[]
 * }}
 * @returns {Promise<*>}
 */
async function add(act, data) {
  const response = await client.post(apiUrls.admin.activity.targets.add, data, {
    params: {
      act: act
    }
  })

  return processResponse(response)
}

/**
 *
 * @param id {number}
 * @param data {{
 *   "grade": number,
 *   "major": number,
 *   "course": number[]
 * }}
 * @returns {Promise<*>}
 */
async function update(id, data) {
  const response = await client.post(apiUrls.admin.activity.targets.update,
    data, {
      params: {id: id}
    })

  return processResponse(response)
}

/**
 *
 * @param id {number}
 * @returns {Promise<*>}
 */
async function deleteActivityTarget(id) {
  const response = await client.post(apiUrls.admin.activity.targets.delete, undefined, {
    params: {id: id}
  })

  return processResponse(response)
}

/**
 *
 * @param src {number}
 * @param dst {number}
 * @returns {Promise<*>}
 */
async function duplicateActivityTarget(src, dst) {
  const response = await client.post(apiUrls.admin.activity.targets.duplicate, undefined, {
    params: {
      src: src,
      dst: dst
    }
  })

  return processResponse(response)
}

