import { ref, watch, onBeforeMount, Ref, computed, nextTick, ComputedRef } from 'vue';
import { CollectionModel } from '@/dataModel/collection';
import { RouterBus } from '@/hooks';
import { Notify } from 'vant';

/**错题与收藏 */
export const useSubjectCheck = (currentSubject: ComputedRef<any>, nextSubject: () => Promise<void>) => {
    const trueNum = ref(0); //正确数量
    const falseNum = ref(0); //错误数量
    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 = () => {
        currentSubject.value.optsBack = currentSubject.value.opts.map((val: String) => {
            let status;
            if (currentSubject.value.answer.includes(val)) {
                status = 1;
            } else {
                status = 0;
            }
            if (currentSubject.value.userAnswer.includes(val)) {
                status += 2;
            }
            return { opt: val, status };
        });
        if (JSON.stringify(currentSubject.value.answer) == JSON.stringify(currentSubject.value.userAnswer)) {
            //答案正确
            currentSubject.value.isTrue = true;
            trueNum.value++;
            if (isJumpNext.value) {
                nextTick(() => {
                    nextSubject();
                });
            }
        } else {
            //答案错误
            wrongModel.adds([
                {
                    carType: query.vehicle as CollectionAndWrongType.CarType,
                    km: query.name as CollectionAndWrongType.Km,
                    questionId: currentSubject.value.id,
                },
            ]);
            currentSubject.value.isTrue = false;
            falseNum.value++;
        }
    };

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