import { ref, watch, onBeforeMount, Ref, computed, nextTick, ComputedRef } from "vue";
import { CollectionModel } from "@/model/collection";
import { RouterBus } from "@/hooks";
import { Notify } from "vant";
import { isArrSubset } from "@/utils/utils";
import store from "@/store";

/**错题与收藏 */
export const useSubjectCheck = (currentSubject: ComputedRef<any>, nextSubject: () => Promise<void>, _type?: ExerciseType.ListType) => {
	const trueNum = ref(0); //正确数量
	const falseNum = ref(0); //错误数量
	const skillsShow = ref(false); //显示技巧讲解
	const officialShow = ref(false); //显示官方解释
	const isJumpNext = ref(false); //答对跳转下一题
	const wrongModel = new CollectionModel("wrong");
	const collectionModel = new CollectionModel("collection");
	const {
		route: { query },
	} = new RouterBus();

	/** 批量新增收藏 */
	const addsCullection = async (ids: number[]) => {
		const questionList = ids.map((id) => {
			return {
				carType: query.vehicle as CollectionAndWrongType.CarType,
				km: query.name as CollectionAndWrongType.Km,
				questionId: id,
			};
		});
		collectionModel.adds(questionList);
	};

	/** 收藏当前题目 */
	const addCurrentQuestion = async () => {
		const res = await collectionModel.adds([
			{
				carType: query.vehicle as CollectionAndWrongType.CarType,
				km: query.name as CollectionAndWrongType.Km,
				questionId: currentSubject.value.id,
			},
		]);
		if (res.data == 1) {
			currentSubject.value.isCollection = true;
			Notify({ type: "success", message: "收藏成功" });
		} else {
			currentSubject.value.isCollection = true;
			Notify({ type: "primary", message: res.msg });
		}
	};
	/**
	 * 选择答案后进行校验
	 */
	const userAnswerChange = () => {
		console.log(currentSubject.value);
		let isAllTrue = true
		currentSubject.value.optsBack = currentSubject.value.opts.map((val: String) => {
			let status;

			let answers = currentSubject.value.answer.split("-")
			if (Array.isArray(currentSubject.value.userAnswer)) {
				// if (currentSubject.value.answer.includes(val)) {
				// 	status = 1;
				// } else {
				// 	status = 0;
				// }
				if (answers.includes(val)) {
					status = 3;
				}
				else if (!answers.includes(val) && JSON.stringify(answers.sort()) == JSON.stringify(currentSubject.value.userAnswer.sort())) {
					status = 0;

				}
				else {
					//状态2是错误的
					isAllTrue = false
					status = 2
				}

			} else {
				if (currentSubject.value.answer === val) {
					status = 1;
				} else {
					status = 0;
				}
				if (currentSubject.value.userAnswer === val) {
					//状态2是错误的
					isAllTrue = false
					status += 2;
				}
			}

			return { opt: val, status };
		});
		//正确已经在别处处理了

		let answers = currentSubject.value.answer.split("-")
		// console.log(JSON.stringify(currentSubject.value.answer.split("-")),JSON.stringify(currentSubject.value.userAnswer))
		//统一使用数组进行检查,单项时候currentSubject.value.userAnswer为string,需转化为数组进行比对
		if (JSON.stringify(answers.sort()) == JSON.stringify(typeof currentSubject.value.userAnswer === 'string' ? [currentSubject.value.userAnswer] : currentSubject.value.userAnswer.sort())) {
			//答案正确
			//类型是错误时候不显示对错
			_type == "localWrong" ? currentSubject.value.isTrue = null : currentSubject.value.isTrue = true
			trueNum.value++;
			if (isJumpNext.value) {
				nextTick(() => {
					nextSubject();
				});
			}
		} else {

			falseNum.value++;
			//答案错误
			wrongModel.adds([
				{
					carType: query.vehicle as CollectionAndWrongType.CarType,
					km: query.name as CollectionAndWrongType.Km,
					questionId: currentSubject.value.id,
				},
			]);
			//类型是错误时候不显示对错
			_type == "localWrong" ? currentSubject.value.isTrue = null : currentSubject.value.isTrue = false
			//vip才显示错误解析
			if (store.getters.getIsVip) {

				skillsShow.value = true;
			}


		}
	};

	return {
		trueNum,
		skillsShow,
		officialShow,
		falseNum,
		isJumpNext,
		userAnswerChange,
		addCurrentQuestion,
	};
};
