import axios, { AxiosResponse } from 'axios';
import {
  MarkQualityMonitorListFilter,
  MarkQualityMonitorListPageParam,
  MarkQualityMonitorListPageRes,
  QMScoreItem,
  MarkArbitrationListFilter,
  MarkArbitrationListPageRes,
  MarkArbitrationListPageParam,
  MarkTaskListPageRes,
  MarkTaskListPageParam,
  MarkTaskListFilter,
  MarkTrialListPageRes,
  MarkTrialListPageParam,
  MarkMarkerListPageParam,
  MarkMarkerListPageRes,
  MarkGroupListPageParam,
  MarkGroupListPageRes,
  MarkGroupUpdateParams,
  MarkGroupResetParams,
  MarkGroupItem,
  MarkGroupUpdateStatusParams,
  MarkStatInfo,
  MarkStatListPageParam,
  MarkStatListPageRes,
  MarkRejectResetTaskParam,
  GroupArbitrateStatFilter,
  GroupArbitrateStatListRes,
  GroupArbitrateStatListParam,
} from './types/mark';

// 质量监控
// 获取质量监控列表
export function getQualityMonitorList(
  params: MarkQualityMonitorListPageParam
): Promise<MarkQualityMonitorListPageRes> {
  return axios.post('/api/admin/exam/quality/list/marker', {}, { params });
}

// 导出质量监控列表
export function exportQualityMonitorList(
  params: MarkQualityMonitorListFilter
): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/exam/quality/export',
    {},
    { params, responseType: 'blob' }
  );
}

// 分组统计仲裁列表
export function getGroupArbitrateStatList(
  params: GroupArbitrateStatListParam
): Promise<GroupArbitrateStatListRes> {
  const url = params.group
    ? '/api/admin/exam/quality/list/group'
    : '/api/admin/exam/quality/list/subject';
  return axios.post(url, {}, { params });
}
// 分组统计仲裁导出
export function exportGroupArbitrateStatList(
  params: GroupArbitrateStatFilter
): Promise<AxiosResponse<Blob>> {
  const url = params.group
    ? '/api/admin/exam/quality/list/group/export'
    : '/api/admin/exam/quality/list/subject/export';
  return axios.post(url, {}, { params, responseType: 'blob' });
}

// 重新计算
export function qualityMonitorCalculate(
  params: MarkQualityMonitorListFilter
): Promise<boolean> {
  return axios.post('/api/admin/exam/quality/update', {}, { params });
}
// 给分曲线
export function qualityMonitorScoreList(
  params: MarkQualityMonitorListFilter
): Promise<QMScoreItem[]> {
  return axios.post('/api/admin/exam/quality/chart', {}, { params });
}

// 仲裁管理
// 获取仲裁列表
export function getArbitrationList(
  params: MarkArbitrationListPageParam
): Promise<MarkArbitrationListPageRes> {
  return axios.post('/api/admin/exam/arbitrate/list', {}, { params });
}

// 仲裁导出
export function exportArbitrationList(
  params: MarkArbitrationListFilter
): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/exam/arbitrate/export',
    {},
    { params, responseType: 'blob' }
  );
}

// 任务管理
// 获取任务列表
export function getMarkTaskList(
  params: MarkTaskListPageParam
): Promise<MarkTaskListPageRes> {
  return axios.post('/api/admin/exam/library/list', {}, { params });
}

// 获取待复核任务数
export function getMarkInspectedTaskCount(
  params: MarkTaskListFilter
): Promise<number> {
  return axios.post('/api/admin/exam/library/inspected/count', {}, { params });
}

// 评卷员任务指定
export function markTaskAssign({
  libraryId,
  marker,
}: {
  libraryId: number;
  marker: string;
}): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/library/assigned',
    {},
    { params: { libraryId, marker } }
  );
}

// 任务导出
export function exportMarkTaskList(
  params: MarkTaskListFilter
): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/exam/library/export',
    {},
    { params, responseType: 'blob' }
  );
}

// 任务打回or重置
export function markTaskRejectOrReset(
  params: MarkRejectResetTaskParam
): Promise<boolean> {
  return axios.post('/api/admin/exam/library/reject', {}, { params });
}

// 试评管理
// 获取试评列表
export function getMarkTrialList(
  params: MarkTrialListPageParam
): Promise<MarkTrialListPageRes> {
  return axios.post('/api/mark/task/list', {}, { params });
}

// 评卷员管理
// 获取评卷员列表
export function getMarkMarkerList(
  params: MarkMarkerListPageParam
): Promise<MarkMarkerListPageRes> {
  return axios.post('/api/admin/exam/marker/list', {}, { params });
}

// 启用、禁用评卷员
export function markMarkerEnable({
  ids,
  enable,
}: {
  ids: number[];
  enable: boolean;
}): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/marker/toggle',
    {},
    { params: { ids, enable } }
  );
}
// 解绑评卷员
export function markMarkerUnbind(markerId: number): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/marker/delete',
    {},
    { params: { markerId } }
  );
}

// 回收任务
export function markMarkerTaskRecycle(ids: number[]): Promise<boolean> {
  return axios.post('/api/admin/exam/marker/release', {}, { params: { ids } });
}

// 设置评卷数量
export function markMarkerSetTaskCount({
  ids,
  taskCount,
}: {
  ids: number[];
  taskCount: number;
}): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/marker/set-task-count',
    {},
    { params: { ids, taskCount } }
  );
}

// 重置密码
export function markMarkerResetPassword(
  ids: number[],
  password = '123456'
): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/marker/reset-password',
    {},
    { params: { ids, password } }
  );
}
// 重置已评数量
export function markMarkerResetFinishCount(
  id: number,
  deleteCode: string
): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/marker/reset',
    {},
    { params: { id, deleteCode } }
  );
}
// 重置是否弹窗确认
export function checkResetNeedConfirm(): Promise<boolean> {
  return axios.post('/api/admin/exam/marker/delete-check', {});
}

// 评卷员导入模板下载
export function markerImportTemplate(): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/exam/marker/template',
    {},
    { responseType: 'blob' }
  );
}

// 分组管理
// 获取分组列表
export function getMarkGroupList(
  params: MarkGroupListPageParam
): Promise<MarkGroupListPageRes> {
  return axios.post('/api/admin/exam/group/list', {}, { params });
}

// 获取分组信息
export function getMarkGroupInfo({
  subjectCode,
  groupNumber,
}: {
  subjectCode: string;
  groupNumber: number;
}): Promise<MarkGroupItem> {
  return axios.post(
    '/api/mark/group/info',
    {},
    { params: { subjectCode, groupNumber } }
  );
}

function transformGroupUpdateData(data: MarkGroupUpdateParams) {
  const ndata = { ...data };
  ndata.intervalScoreList = data.questions.map((item) => item.intervalScore);
  ndata.arbitrateThresholdList = data.questions.map(
    (item) => item.arbitrateThreshold
  );
  ndata.questionIds = data.questions.map((item) => item.id);
  ndata.number = data.groupNumber;
  return ndata;
}

// 修改分组
export function markGroupUpdate(
  params: MarkGroupUpdateParams,
  editType: 'update' | 'add'
): Promise<boolean> {
  if (editType === 'update') {
    return axios.post(
      '/api/admin/exam/group/update',
      transformGroupUpdateData(params)
    );
  }
  return axios.post(
    '/api/admin/exam/group/insert',
    transformGroupUpdateData(params)
  );
}

// 修改分组状态
export function markGroupUpdateStatus(
  params: MarkGroupUpdateStatusParams
): Promise<boolean> {
  return axios.post('/api/admin/exam/group/changeStatus', {}, { params });
}

// 删除分组
export function markGroupDelete(
  params: MarkGroupResetParams
): Promise<boolean> {
  return axios.post('/api/admin/exam/group/delete', {}, { params });
}
// 重置分组
export function markGroupReset(params: MarkGroupResetParams): Promise<boolean> {
  return axios.post('/api/admin/exam/group/reset', {}, { params });
}
// 回收分组任务
export function markGroupTaskRecycle(
  params: MarkGroupResetParams
): Promise<boolean> {
  return axios.post('/api/admin/exam/group/release', {}, { params });
}

// 设置试评数量
export function markGroupSetTrialCount({
  subjectCode,
  trialCount,
}: {
  subjectCode: string;
  trialCount: number;
}): Promise<boolean> {
  return axios.post(
    '/api/admin/exam/group/updateTrialCount',
    {},
    { params: { subjectCode, trialCount } }
  );
}

// 数据校对
export function markGroupDataCheck(subjectCode: string) {
  return axios.post(
    '/api/admin/exam/group/checkCount',
    {},
    { params: { subjectCode } }
  );
}

// 关闭分组
export function markGroupClose(subjectCode: string, groupNumbers: number[]) {
  return axios.post(
    '/api/admin/exam/group/close',
    {},
    { params: { subjectCode, groupNumbers } }
  );
}

// 评卷进度统计
// 获取评卷进度统计信息
export function getMarkStatInfo(examId: number): Promise<MarkStatInfo> {
  return axios.post('/api/mark/stat/info', {}, { params: { examId } });
}

// 获取评卷进度统计列表
export function getMarkStatList(
  params: MarkStatListPageParam
): Promise<MarkStatListPageRes> {
  return axios.post('/api/mark/stat/list', {}, { params });
}

// 关闭科目
export function markSubjectClose(subjectIds: number[]): Promise<boolean> {
  return axios.post('/api/mark/subject/close', {}, { params: { subjectIds } });
}

// 导出评卷进度
export function exportMarkProgress(): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/mark/info/export/progress',
    {},
    { responseType: 'blob' }
  );
}
// 导出工作量统计
export function exportMarkWorkload(): Promise<AxiosResponse<Blob>> {
  return axios.post(
    '/api/admin/mark/info/export/marker',
    {},
    { responseType: 'blob' }
  );
}
