import { cancelCollectQuestion, collectQuestion, deleteWrongQuestion as deleteWrongQuestionApi } from "@/api/modules/study";
import { EnumQuestionType } from "@/common/enum";
import { Study } from "@/types";

const useQuestionBook = () => {
  /**
   * 题目是否正确
   * @param qs 题目
   * @returns 是否正确
   */
  const isQuestionCorrect = (qs: Study.FavoriteQuestionVO): boolean => {
    let { answers, answer, typeId } = qs;
    answers = answers?.filter(item => !!item) || [];
    answer = answer || '';
    if ([EnumQuestionType.SINGLE_CHOICE, EnumQuestionType.JUDGMENT].includes(typeId)) {
      return answer.includes(answers[0]);
    } else if ([EnumQuestionType.MULTIPLE_CHOICE].includes(typeId)) {
      return answers.length === answer.length && answers.every(item => answer.includes(item));
    } else {
      // 主观题 A 对 B 错
      return answers.includes('A') && !answers.includes('B');
    }
  };
  /**
   * 题目是否未作答
   * @param qs 题目
   * @returns 是否未作答
   */
  const isQuestionNotAnswer = (qs: Study.FavoriteQuestionVO): boolean => {
    return !qs.answers || qs.answers.filter(item => !!item).length === 0;
  }
  /**
   * 选项是否正确
   * @param qs 题目
   * @param option 选项
   * @returns 是否正确
   */
  const isOptionCorrect = (qs: Study.FavoriteQuestionVO, option: Study.QuestionOption) => {
    const { answers, answer, typeId } = qs;
    if ([EnumQuestionType.SINGLE_CHOICE, EnumQuestionType.JUDGMENT].includes(typeId)) {
      return answer?.includes(option.no);
    } else if ([EnumQuestionType.MULTIPLE_CHOICE].includes(typeId)) {
      return answer?.includes(option.no);
    } else {
      return answers?.includes(option.no) && option.no === 'A';
    }
  }
  /**
   * 选项是否选中
   * @param qs 题目
   * @param option 选项
   * @returns 是否选中
   */
  const isOptionSelected = (qs: Study.FavoriteQuestionVO, option: Study.QuestionOption) => {
    return qs.answers.includes(option.no);
  }
  /**
   * 取消收藏
   * @param id 题目ID
   * @returns 是否成功
   */
  const cancelCollect = (id: number): Promise<boolean> => {
    return new Promise((resolve, reject) => {
      uni.$ie.showModal({
        title: '提示',
        content: '确定取消收藏吗？',
      }).then(async confirm => {
        if (confirm) {
          try {
            await cancelCollectQuestion(id);
            uni.$ie.showToast('取消收藏成功');
            resolve(true);
          } catch (error) {
            uni.$ie.showToast('取消收藏失败');
            reject(error);
          }
        } else {
          reject(new Error('用户取消'));
        }
      });
    });
  }
  /**
   * 收藏题目
   * @param id 题目ID
   * @returns 是否成功
   */
  const collect = (id: number): Promise<boolean> => {
    return new Promise(async (resolve, reject) => {
      try {
        await collectQuestion(id);
        uni.$ie.showToast('收藏成功');
        resolve(true);
      } catch (error) {
        uni.$ie.showToast('收藏失败');
        reject(error);
      }
    });
  }
  const deleteWrongQuestion = (id: number): Promise<boolean> => {
    return new Promise(async (resolve, reject) => {
      uni.$ie.showModal({
        title: '提示',
        content: '确定删除错题吗？',
      }).then(async confirm => {
        if (confirm) {
          try {
            await deleteWrongQuestionApi(id);
            uni.$ie.showToast('删除错题成功');
            resolve(true);
          } catch (error) {
            uni.$ie.showToast('删除错题失败');
            reject(error);
          }
        } else {
          reject(new Error('用户取消'));
        }
      });
    });
  }


  return {
    isQuestionCorrect,
    isQuestionNotAnswer,
    isOptionCorrect,
    isOptionSelected,
    cancelCollect,
    collect,
    deleteWrongQuestion
  }
}

export default useQuestionBook;