import request from "../request";
import { AxiosPromise } from "axios";

class Test {
  /** 获取题目列表 */
  list(params: Test.listParams): AxiosPromise<Test.listRes> {
    return request("/student/qustion/info/list", {
      params,
    });
  }

  /** 获取非VIP题目列表 */
  normalList(params: Test.listParams): AxiosPromise<Test.listRes> {
    return request("/student/qustion/info/normalList", {
      params,
    });
  }

  /** 根据ids批量获取问题详情  */
  getQuestionInfoByIds(
    params: Test.QuestionInfoByIdsParams
  ): AxiosPromise<Test.QuestionInfoByIdsRes> {
    return request("/student/qustion/info/getQuestionInfoByIds", {
      params: {
        ...params,
        ids: params.ids.toString(),
      },
    });
  }

  /** 获取地方题分类 */
  selectDfQustionInfo(
    params: Partial<Test.getTopicClassParams>
  ): AxiosPromise<Test.selectDfQustionInfoRes[]> {
    return request({
      url: `/student/qustion/info/selectDfQustionInfo`,
      params,
    });
  }

  /** 获取分类专题分类 */
  selectFlQustionInfo(
    params: Partial<Test.getTopicClassParams>
  ): AxiosPromise<Test.selectFlQustionInfoRes[]> {
    return request({
      url: `/student/qustion/info/selectFlQustionInfo`,
      params,
    });
  }

  /** 获取精选题分类 */
  selectJxQustionInfo(
    params: Partial<Test.getTopicClassParams>
  ): AxiosPromise<Test.selectJxQustionInfoRes[]> {
    return request({
      url: `/student/qustion/info/selectJxQustionInfo`,
      params,
    });
  }

  /** 获取顺序练习分类 */
  selectSxQustionInfo(
    params: Partial<Test.getTopicClassParams>
  ): AxiosPromise<Test.selectSxQustionInfoRes[]> {
    return request({
      url: `/student/qustion/info/selectSxQustionInfo`,
      params,
    });
  }
}

export const test = new Test();

/**
 * 获取题目列表
 * @returns
 */
export async function getTopicList(params: any, isVip: Boolean = true) {
  const getUserAnswer = (type: string) => {
    switch (type) {
      case "判断题":
        return null;
      case "单选题":
        return null;
      case "多选题":
        return [];
    }
  };

  const getType = (answer: Array<string>) => {
    if (answer.length === 1) {
      if (["√", "×"].includes(answer[0])) {
        return "判断题";
      } else {
        return "单选题";
      }
    } else {
      return "多选题";
    }
  };

  const res = await request({
    url: isVip
      ? "/student/qustion/info/list"
      : "/student/qustion/info/normalList",
    params,
  });

  const data = {
    total: res.data.total,
    list: res.data.rows.map((item: any) => {
      return {
        ...item,
        explain: item.issue,
        opts: item.opts.split("-"),
        image: item.image,
        type: getType(item.answer.split("-")),
        answer: item.answer,
        userAnswer: getUserAnswer(getType(item.answer.split("-"))),
        isTrue: null,
        isCollection: false,
      };
    }),
  };

  return data;
}

/**
 * 获取题目分类
 */
export async function getTopicClass(path: string, params: any) {
  return request({
    url: `/student/qustion/info/${path}`,
    params,
  });
}

/**
 * 根据ids批量获取问题详情
 */
export const getQuestionInfoByIds = async (
  params: Test.QuestionInfoByIdsParams
) => {
  const res = await request({
    url: "/student/qustion/info/getQuestionInfoByIds",
    params: {
      ...params,
      ids: params.ids.toString(),
    },
  });
  return <Test.QuestionInfoByIdsRes>res.data;
};
