import {injectLocal, provideLocal} from "@vueuse/core";
import {toast} from "@/uni_modules/uv-ui-tools/libs/function";
import _ from "lodash";
import {useInjectPaperService} from "@/components/mx-paper/usePaperInjection";
import {useInjectQuestionService} from "@/components/mx-question/useQuestionInjection";

const key = Symbol('PAPER_NAVIGATION_SERVICE')

export const useProvidePaperNavigationService = (paperService, questionService, overrideService = {}) => {

    const {goToQuestion, allowAnswer, allowScore, commitPaper, scorePaper} = useInjectPaperService()
    const {unansweredQuestions, unscoredQuestions, doChunk} = useInjectQuestionService()

    const questionsNavigationHandler = (navigator, list, label) => {
        if (!list.length) return toast(`没有${label}的题`)
        if (list.length == 1) return goToQuestion(_.first(list))
        navigator.open(list, `${label}的题`)
    }

    const uncompletedCheckForQuit = async (navigator) => {
        await doChunk()
        return new Promise((resolve, reject) => {
            if (allowAnswer.value) {
                if (unansweredQuestions.value.length) {
                    navigator.open(unansweredQuestions.value, '您还有未做的题', {
                        left: '强制退出',
                        right: '继续做题',
                        onLeft: () => {
                            navigator.close()
                            resolve() // 允许用户继续执行后续逻辑
                        },
                        onRight: () => {
                            navigator.close()
                            goToQuestion(_.first(unansweredQuestions.value))
                            reject('操作终止，用户选择继续做题')
                        }
                    })
                } else {
                    navigator.open([], '提示', {
                        description: '您已经做完所有题，是否完成提交',
                        left: '强制退出',
                        right: '完成提交',
                        onLeft: () => {
                            navigator.close()
                            resolve()
                        },
                        onRight: () => {
                            navigator.close()
                            commitPaper()
                            reject('操作终止，用户选择完成提交')
                        }
                    })
                }
            } else if (allowScore.value) {
                if (unscoredQuestions.value.length) {
                    navigator.open(unscoredQuestions.value, '您还有未阅的题', {
                        left: '强制退出',
                        right: '继续阅卷',
                        onLeft: () => {
                            navigator.close()
                            resolve() // 允许用户继续执行后续逻辑
                        },
                        onRight: () => {
                            navigator.close()
                            goToQuestion(_.first(unscoredQuestions.value))
                            reject('操作终止，用户选择继续阅卷')
                        }
                    })
                } else {
                    navigator.open([], '提示', {
                        description: '您已经阅完所有题，是否完成阅卷',
                        left: '强制退出',
                        right: '完成阅卷',
                        onLeft: () => {
                            navigator.close()
                            resolve()
                        },
                        onRight: () => {
                            navigator.close()
                            scorePaper()
                            reject('操作终止，用户选择完成阅卷')
                        }
                    })
                }
            } else {
                resolve()
            }
        })
    }

    const uncompletedCheckForSubmit = async (navigator) => {
        await doChunk()
        return new Promise((resolve, reject) => {
            if (allowAnswer.value) {
                if (unansweredQuestions.value.length) {
                    navigator.open(unansweredQuestions.value, '您还有未做的题', {
                        left: '仍然提交',
                        right: '继续做题',
                        onLeft: () => {
                            navigator.close()
                            resolve() // 题没有做完是可以强制交卷的！
                        },
                        onRight: () => {
                            navigator.close()
                            goToQuestion(_.first(unansweredQuestions.value))
                            reject('操作终止，用户选择继续做题') // 注意这里也是reject
                        }
                    })
                    return // 只要有题未做，弹层都会阻止原提交行为
                }
            } else if (allowScore.value) {
                if (unscoredQuestions.value.length) {
                    navigator.open(unscoredQuestions.value, '未阅的题')
                    reject() // 阅卷时必须给所有题打完分才行
                    return
                }
            }
            resolve()
        })
    }

    const options = {
        questionsNavigationHandler,
        uncompletedCheckForQuit,
        uncompletedCheckForSubmit,
        ...overrideService
    }
    provideLocal(key, options)
    return options
}

export const useInjectPaperNavigationService = () => {
    return injectLocal(key)
}