import {createEventHook, injectLocal, provideLocal} from "@vueuse/core";
import {
    getPaperBatches,
    getPaperExamTypes,
    getPaperKnowledges,
    getPaperMajors,
    getPaperProvinces,
    getPaperSubjects,
    getPaperUniversities
} from "@/api/dz/papers.js";

const key = Symbol('PaperExactCondition')

export const useProvidePaperExactCondition = function () {
    const batchId = ref('')
    const batchList = ref([])

    const location = ref('')
    const provinces = ref([])
    const examType = ref('')
    const examTypes = ref([])
    const universityId = ref('')
    const universities = ref([])
    const majorPlanId = ref('')
    const majors = ref([])

    const subjects = ref([])
    const subjectId = ref('')
    const knowledges = ref([])
    const knowledgeNode = ref(null) // 单选节点
    const knowledgeCheckNodes = ref([]) // 多选的节点
    const knowledgeIds = computed(() => knowledgeCheckNodes.value.map(k => k.id))
    const knowledgeId = computed(() => knowledgeNode.value?.id || '') // 单选
    const knowledgeRemoveEvent = createEventHook()
    const removeKnowledge = async (k) => {
        const nodes = knowledgeCheckNodes.value
        const idx = nodes.indexOf(k)
        if (idx > -1) {
            nodes.splice(idx, 1)
            await knowledgeRemoveEvent.trigger(k)
        }
    }

    const paperArgs = computed(() => ({
        batchId: toValue(batchId),
        batchName: batchList.value.find(b => b.batchId == batchId.value)?.name,
        subjectId: toValue(subjectId),
        subjectName: subjects.value.find(s => s.subjectId == subjectId.value)?.subjectName,
        examTypes: [toValue(examType)],
        location: toValue(location),
        universityIds: [toValue(universityId)],
        planIds: [toValue(majorPlanId)]
    }))
    const payload = {
        location, provinces, examType, examTypes, universityId, universities,
        batchId, batchList, majorPlanId, majors, subjects, subjectId,
        knowledges, knowledgeNode, knowledgeId, knowledgeCheckNodes, knowledgeIds,
        removeKnowledge, onKnowledgeRemove: knowledgeRemoveEvent.on,
        paperArgs
    }
    provideLocal(key, payload)

    // hooks
    onMounted(async () => {
        const res = await getPaperProvinces()
        provinces.value = res.data
    })
    onMounted(async () => {
        const res = await getPaperBatches()
        batchList.value = res.data
    })
    watch(location, async (location) => {
        // clean
        examType.value = ''
        examTypes.value = []

        if (!location) return
        const resT = await getPaperExamTypes({location})
        examTypes.value = resT.data
    })
    watch([location, batchId], async ([location, batchId]) => {
        // clean
        universityId.value = ''
        universities.value = []

        if (!location || !batchId) return
        const resU = await getPaperUniversities({location, batchId})
        universities.value = resU.data
    })
    watch([location, examType, batchId, universityId], async ([location, examType, batchId, universityId]) => {
        // clean
        majorPlanId.value = ''
        majors.value = []

        if (!location || !examType || !batchId || !universityId) return
        const res = await getPaperMajors({location, examType, batchId, universityId})
        majors.value = res.data
        if (res.data.length) majorPlanId.value = res.data[0].id
    })
    watch(universityId, async (universityId) => {
        // clean
        subjects.value = []
        subjectId.value = ''

        if (!universityId) return
        const res = await getPaperSubjects({universityId})
        subjects.value = res.data
        if (res.data.length) subjectId.value = res.data[0].subjectId
    })
    watch([majorPlanId, subjectId], async ([majorPlanId, subjectId]) => {
        // clean
        knowledges.value = []
        knowledgeNode.value = null // 单选的情况
        knowledgeCheckNodes.value = [] // 多选的情况

        if (!subjectId || !majorPlanId) return
        // 获取知识点数据
        const res = await getPaperKnowledges({subjectId, majorPlanId})
        knowledges.value = res.data
    })

    return payload
}

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