import {useProvidePaperBatchCondition} from "@/views/dz/papers/hooks/usePaperBatchCondition.js";
import {getPaperExamTypes, getPaperMajors, getPaperSubjects, getPaperUniversities} from "@/api/dz/papers.js";
import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";

const key = Symbol('PaperExactCondition')
export const useProvidePaperExactCondition = function (type) {
    const {buildType, batchId, batchList, formatBatchName, getBatchDisplayName, onBatchReady} = useProvidePaperBatchCondition(type)

    const examType = ref('')
    const examTypes = ref([])

    const universityId = ref('')
    const universities = ref([])

    const _allMajors = ref([])

    const majorGroup = ref('')
    const majorGroups = computed(() => {
        const results = []
        _allMajors.value.forEach(m => {
            if (results.includes(m.majorGroup)) return
            results.push(m.majorGroup)
        })
        return results
    })

    const majorPlanId = ref('')
    const majors = computed(() => {
        if (!majorGroup.value) return
        return _allMajors.value.filter(m => m.majorGroup == majorGroup.value)
    })

    const subjectId = ref('')
    const subjectList = ref([])

    const conditionArgs = computed(() => ({
        buildType: toValue(buildType),
        batchId: toValue(batchId),
        examType: toValue(examType),
        universityId: toValue(universityId),
        majorGroup: toValue(majorGroup),
        majorPlanId: toValue(majorPlanId),
        subjectId: toValue(subjectId)
    }))
    const conditionData = computed(() => ({
        examTypes: toValue(examTypes),
        batchList: toValue(batchList),
        universities: toValue(universities),
        majorGroups: toValue(majorGroups),
        majors: toValue(majors),
        subjectList: toValue(subjectList)
    }))
    const exactEvent = createEventHook()
    const triggerExactEvent = async function (args) {
         await exactEvent.trigger(args)
    }

    watch(batchId, async (batchId) => {
        examType.value = ''
        examTypes.value = []

        if (!batchId) return
        const res = await getPaperExamTypes({buildType, batchId})
        examTypes.value = res.data
    })

    watch([batchId, examType], async ([batchId, examType]) => {
        universityId.value = ''
        universities.value = []

        if (!batchId || !examType) return
        const res = await getPaperUniversities({buildType, batchId, examType})
        universities.value = res.data

        const resSubject = await getPaperSubjects({buildType, batchId, examType})
        subjectList.value = resSubject.data
    })

    watch([batchId, examType, universityId], async ([batchId, examType, universityId]) => {
        majorGroup.value = ''

        if (!batchId || !examType || !universityId) return
        const res = await getPaperMajors({buildType, batchId, examType, universityId})
        _allMajors.value = res.data

        // 没有数据时，说明已经满足了条件
        if (!majorGroups.value.length) await triggerExactEvent({buildType, batchId, examType, universityId})
        else if (majorGroups.value.length == 1) majorGroup.value = majorGroups.value[0]
    })

    watch([batchId, examType, universityId, majorGroup], async ([batchId, examType, universityId, majorGroup]) => {
        majorPlanId.value = ''

        if (!batchId || !examType || !universityId || !majorGroup) return
        if (!majors.value.length) await triggerExactEvent({buildType, batchId, examType, universityId, majorGroup})
        else if (majors.value.length == 1) majorPlanId.value = majors.value[0].id
    })

    watch(majorPlanId, async (majorPlanId) => {
        if (!majorPlanId) return

        // 最细的情况，选中了专业
        const args = {
            buildType,
            batchId: toValue(batchId),
            examType: toValue(examType),
            universityId: toValue(universityId),
            majorGroup: toValue(majorGroup),
            majorPlanId
        }
        await triggerExactEvent(args)
    })

    const payload = {buildType, batchId, batchList, formatBatchName, getBatchDisplayName, onBatchReady,
        examType, examTypes,
        universityId, universities,
        majorGroup, majorGroups,
        majorPlanId, majors,
        conditionArgs, conditionData, subjectList,
        onConditionReady: exactEvent.on}
    provideLocal(key, payload)
    return payload
}

export const useInjectPaperExactCondition = function () {
    return injectLocal(key)
}