import type { FormInstance } from 'element-plus'

import { useUserStore } from '@vben/stores'

import { questionService } from '#/api/service/question-bank/question'
import { useBasicData } from '#/store/basic-data'

// 年份
export const year_options = [] as any[]
for (let i = 2025; i >= 2010; i--) {
  year_options.push({
    label: `${i}`,
    value: `${i}`,
  })
}

export function useQuestionAdd() {
  const { userInfo } = storeToRefs(useUserStore())
  // 接口
  const { channelTypeMap } = storeToRefs(useBasicData())

  const formRef = ref<FormInstance | undefined>()
  const formData = ref({} as any)
  // 当前编辑的题目
  const editQuestion = ref({} as any)

  // 基本信息
  const baseDataRef = ref()
  // 题干
  const stemDataRef = ref()
  // 答案
  const answerDataRef = ref()

  // 是否为主观题之类的
  const isSubjectiveType = computed(() => {
    const id = formData.value.channelTypeId
    return isSubjectiveQuestion(id)
  })
  // 是否为单选题之类的
  const isSingleOptionType = computed(() => {
    const id = formData.value.channelTypeId
    return isSingleOptionQuestion(id)
  })
  // 是否为选择题之类的
  const isOptionType = computed(() => {
    const id = formData.value.channelTypeId
    return isOptionQuestion(id)
  })
  // 是否为填空题之类的
  const isFillType = computed(() => {
    const id = formData.value.channelTypeId
    return isFillQuestion(id)
  })
  // 是否为判断题之类的
  const isJudgeType = computed(() => {
    const id = formData.value.channelTypeId
    return isJudgeQuestion(id)
  })

  // 是否只读
  const formDisabled = computed(() => {
    return !!questionService.isReadonly(editQuestion.value, userInfo.value.id)
  })

  const $reset = () => {
    formData.value = {}
    editQuestion.value = {}
  }

  const initData = async () => {
    // 重置
    $reset()

    const { id } = useRoute().query
    if (!id) return
    editQuestion.value = await questionService.get(id)
  }

  /**
   * 判断题目是否为主管题
   */
  const isSubjectiveQuestion = (id: any, subjectId: any = undefined) => {
    return (
      !isOptionQuestion(id, subjectId) &&
      !isFillQuestion(id, subjectId) &&
      !isJudgeQuestion(id, subjectId)
    )
  }

  /**
   * 判断题目是否为单选题
   */
  const isSingleOptionQuestion = (id: any, _: any = undefined) => {
    if (!id) return false
    const name = channelTypeMap.value[id]?.name
    return name === '单选题' || name === '选择题'
  }

  /**
   * 判断题目是否为选择题
   */
  const isOptionQuestion = (id: any, _: any = undefined) => {
    if (!id) return false
    const name = channelTypeMap.value[id]?.name
    return isSingleOptionQuestion(id, _) || name === '多选题'
  }

  /**
   * 判断题目是否为填空题
   */
  const isFillQuestion = (id: any, _: any = undefined) => {
    if (!id) return false
    const name = channelTypeMap.value[id]?.name
    return name === '填空题' || name === '短文还原'
  }

  /**
   * 判断题目是否为哦安短题
   */
  const isJudgeQuestion = (id: any, _: any = undefined) => {
    if (!id) return false
    const name = channelTypeMap.value[id]?.name
    return name === '判断题'
  }

  return {
    isJudgeQuestion,
    isSubjectiveQuestion,
    isOptionQuestion,
    isFillQuestion,
    formData,
    editQuestion,
    $reset,
    initData,
    isSubjectiveType,
    formDisabled,
    formRef,
    baseDataRef,
    stemDataRef,
    answerDataRef,
    isOptionType,
    isFillType,
    isJudgeType,
    isSingleOptionType,
  }
}
