import { useUserStore } from '@vben/stores'

import { knowledgeService } from '#/api/service/subject/knowledge'
import { subjectService } from '#/api/service/subject/subject'
import { textbookService } from '#/api/service/subject/textbook'
import obj from '#/utils/obj'

import { cleanVersions } from './book-clean'
import {
  getStageByGrade,
  stage_chuzhong,
  stage_gaozhong,
  stage_xiaoxue,
} from './subject'

export const useBasicData = defineStore('basic-data', () => {
  const { userInfo } = storeToRefs(useUserStore())
  let userBookMap = undefined as any

  const textbooksMap = {
    [stage_xiaoxue]: {},
    [stage_chuzhong]: {},
    [stage_gaozhong]: {},
  } as any
  const chaptersMap = {} as any
  const knowledgeMap = {
    [stage_xiaoxue]: {},
    [stage_chuzhong]: {},
    [stage_gaozhong]: {},
  } as any
  const textbooksSingleMap = {} as any

  // 题型
  const channelTypeMap = ref({} as any)
  const channelTypesMap = ref({} as any)

  async function getTextbooks(stageId: any, subjectId: any) {
    const stageMap = textbooksMap[stageId]
    let textbooks = stageMap[subjectId]
    if (!textbooks) {
      textbooks = await textbookService.list(stageId, subjectId)
      cleanVersions(textbooks)
      for (const v of textbooks) {
        for (const b of v.textBookVoList) {
          textbooksSingleMap[b.id] = b
        }
      }
      stageMap[subjectId] = textbooks
    }
    return textbooks
  }

  function getTextbook(bookId: any) {
    return textbooksSingleMap[bookId] as any
  }

  async function getChapters(bookId: any) {
    let chapters = chaptersMap[bookId]
    if (!chapters) {
      chapters = await textbookService.listChapters(bookId)
      chaptersMap[bookId] = chapters
    }
    return chapters
  }

  async function getKnowledgeList(stageId: any, subjectId: any) {
    const knowledgeListMap = knowledgeMap[stageId]
    let knowledgeList = knowledgeListMap[subjectId]
    if (!knowledgeList) {
      knowledgeList = await knowledgeService.list(stageId, subjectId)
      knowledgeListMap[subjectId] = knowledgeList
    }
    return knowledgeList
  }

  async function getUserChannelTypes(bookId: any) {
    if (!userBookMap) {
      userBookMap = obj.list2map(userInfo.value.personConfig?.books, 'id')
    }
    const book = userBookMap[bookId]
    if (!book) return []
    return await getChannelTypes(getStageByGrade(book.gradeId), book.subjectId)
  }

  async function getChannelTypes(stageId: any, subjectId: any) {
    const key = `${stageId}-${subjectId}`
    let types = channelTypesMap.value[key]
    if (types) return types

    types = await subjectService.listChannelTypes(stageId, subjectId)
    for (const type of types) {
      channelTypeMap.value[type.id] = type
    }
    channelTypesMap.value[key] = types
    return types
  }

  async function getUserChannelTypeOptions(bookId: any) {
    const res = await getUserChannelTypes(bookId)
    return res.map((item: any) => ({
      label: item.name,
      value: item.id,
    }))
  }

  async function getChannelTypeOptions(stageId: any, subjectId: any) {
    const res = await getChannelTypes(stageId, subjectId)
    return res.map((item: any) => ({
      label: item.name,
      value: item.id,
    }))
  }

  async function initBasicData() {
    //
  }

  function $reset() {}
  return {
    getUserChannelTypes,
    getUserChannelTypeOptions,
    getChannelTypes,
    channelTypeMap,
    getTextbooks,
    getTextbook,
    $reset,
    getChapters,
    getKnowledgeList,
    initBasicData,
    getChannelTypeOptions,
  }
})
