import { ref, computed, onMounted } from "vue";
import { defineStore } from "pinia";
import {
  getQuestionByTagName,
  validationAnswer,
  getQuestion,
  answered,
} from "@/api/question/question";
import { collect, cancelCollect, getCollectList } from "@/api/collect/collect";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
import router from "@/router";
import { ElLoading } from 'element-plus'
// import { fa } from 'element-plus/es/locale';
export const useTopicStore = defineStore("useTopicStore", () => {
  //当前标签
  const tagName = ref("");
  //当前索引
  const index = ref(0);
  //题目列表
  const Topics = ref([]);
  //当前题目
  const Topic = computed(() => {
    return Topics.value[index.value - 1];
  });
  //收藏列表
  const collectTopics = ref([]);
  //收藏列表id
  const collectTopicIds = computed(() => {
    return collectTopics.value.map((item) => item.id);
  });
  const count = computed(() => Topics.value.length);
  const init = () => {
    index.value = 0;
    if (Topics.value.length == 0) {
      CollectList();
      getData();
      console.log("刷新数据");
    } else {
      console.log("已存在");
    }
  };

  const setQuestion = (question) => {
    CollectList();
    Topics.value = [];
    Topic.value = question;
    Topics.value.push({
      ...question,
      isCollect: collectTopicIds.value.includes(question.id),
    });
  };

  const setQuestions = (questions,item) => {
    CollectList();
    index.value =0
    Topics.value = [];
    Topic.value = item;
    questions.forEach(e => {
      Topics.value.push({
        ...e,
        isCollect: collectTopicIds.value.includes(e.id),
      });
    });
   
  };
  //获取题目
  //设置标签
  const setTagName = (name) => {
    tagName.value = name;
  };
  //根据传入标签情况判断是刷新还是加载
  const loadData = async (name) => {
    if (name === tagName.value) {
      await getData();
    } else {
      setTagName(name);
      await refresh();
    }
  };
  //刷新
  const refresh = async () => {
    clear();
    let res = await getQuestion("refresh");
    if (res.code == 200) {
      if (res.data.length == 0) {
        ElMessage({ message: "题库为空你已经打完所有题目", type: "warning" });
        return;
      }
      res.data = res.data.map((e) => {
        return {
          ...e,
          // isCollect: collectTopicIds.value.includes(e.id),
        };
      });
      Topics.value = res.data;
      index.value = Topics.value.length > 0 ? 1 : 0;
    }
  };
  //加载数据
  const getData = async () => {
    const loadingInstance = ElLoading.service()
    let res = await getQuestion();
    if (res.code == 200) {
      if (res.data.length == 0) {
        ElMessage({ message: "题库为空你已经打完所有题目", type: "warning" });
        res.data = [
          {
            id: 0,
            question: "没有题目了",
            analysis: "没有题目了",
            correctAnswer: "没有题目了",
            correct: false,
            userAnswer: "没有题目了",
            isAnswer: false,
            isCollect: false,
          },
        ];
      }
      res.data = res.data.map((e) => {
        return {
          ...e,
          // isCollect: collectTopicIds.value.includes(e.id),
        };
      });
      Topics.value = [...Topics.value, ...res.data];
      loadingInstance.close()

    }
  };
  //清空数据
  const clear = () => {
    index.value = 0;
    Topics.value = [];
    collectTopics.value = [];
  };
  //提交答案
  const submitAnswer = async (answer) => {
    //先判断是否已经答过
    // if(Topic.value.isAnswer){
    //     ElMessage({ message: "已经做过了", type: "warning" });
    //     return;
    // }
    // console.log(Topic.value);
    // let res = await validationAnswer({ questionId: Topic.value.id, answer });
    // console.log(res);
    // if (res.code == 200) {
    //   setAnswer({
    //     isAnswer: true,
    //     analysis: res.data.analysis,
    //     correctAnswer: res.data.correctAnswer ? res.data.correctAnswer : answer,
    //     correct: res.data.correct,
    //     userAnswer: answer,
    //   });
    // }
    let res = await answered(Topic.value.id);
    ElMessage({ message: res.msg, type: "info" });
    refresh();
  };
  //设置答题情况
  const setAnswer = ({
    isAnswer,
    analysis,
    userAnswer,
    correctAnswer,
    correct,
  }) => {
    Topics.value[index.value - 1] = {
      ...Topic.value,
      isAnswer,
      analysis,
      correctAnswer,
      correct,
      userAnswer,
    };
  };
  //收藏题目
  const Collect = async () => {
    console.log("执行");
    let res;
    if (isCollect()) {
      res = await cancelCollect(Topic.value.id);
      if (res.code == 200) {
        ElMessage({ message: "取消收藏", type: "info" });
        Topics.value[index.value - 1].isCollect = false;
      }
    } else {
      res = await collect(Topic.value.id);
      if (res.code == 200) {
        ElMessage({ message: "收藏成功", type: "info" });
        Topics.value[index.value - 1].isCollect = true;
      }
    }
    await CollectList();
  };
  //上一题
  const prev = () => {
    if (index.value >= 1) {
      index.value--;
      return true;
    }
    return false;
  };
  //下一题
  const next = () => {
    index.value++;
    if (index.value > count.value) {
      index.value = count.value;
      return false;
    }
    const type = router.currentRoute.value.query.type;
    if (type == "view") {
      return true;
    }
    if (index.value == count.value) {
      getData();
    }
    return true;
  };
  //获取收藏题目列表
  const CollectList = async () => {
    let res = await getCollectList();
    if (res.code == 200) {
      collectTopics.value = res.data.map((item) => item.questionVo);
    }
  };
  //判断是否收藏
  const isCollect = () => {
    if (count.value == 0) {
      return false;
    }
    console.log("123", Topic.value, index.value);
    return collectTopicIds.value.includes(Topic.value.id);
  };
  // onMounted(() => {
  //   init();
  // });
  return {
    index,
    Topics,
    count,
    collectTopics,
    collectTopicIds,
    Topic,
    setAnswer,
    loadData,
    setTagName,
    refresh,
    submitAnswer,
    Collect,
    isCollect,
    clear,
    getData,
    prev,
    next,
    CollectList,
    init,
    setQuestion,
    setQuestions
  };
});
