import { ref, onMounted } from "vue"
import { useSlidesStore } from "@/store"
import message from "@/utils/message"
import axios from "axios"

import { getHttpJson } from "@/libs/jsonTool"
import { initWorker, addCourseWareTask, getRunJobIds } from "@/worker/useCoursewaresWorker"

export default () => {
  const slidesStore = useSlidesStore()
  const exporting = ref(false)
  onMounted(() => {
    initWorker()
  })
  // 导入PPTX文件
  const importPPTXFile = (files: FileList) => {
    getHttpJson("./mjkJson/8.json").then(res => {
      if (res.code === 200) {
        jsonToPpt(res.data)
      } else {
        exporting.value = false
      }
    })
    return
    const file = files[0]
    if (!file) return
    exporting.value = true
    const suffix = `.${file.name.split(".")?.reverse()[0]}`
    addCourseWareTask(
      {
        type: "uploadCourseware",
        extra: {
          outputName: file.name.trim(),
          name: file.name.replace(suffix, "").trim(),
          type: file.type,
          suffix,
          path: file.webkitRelativePath,
          paths: file.webkitRelativePath.split("/"),
          files: file
        }
      },
      (e: any) => {
        console.log(e)
        if (e.progress === 100) {
          if (e.status === "done") {
            getHttpJson(e.extra.url).then(res => {
              if (res.code === 200) {
                jsonToPpt(res.data)
                exporting.value = false
              } else {
                exporting.value = false
              }
            })
          } else if (e.status !== "doing") {
            exporting.value = false
          }
        }
      }
    ).catch(err => {
      exporting.value = false
      console.log(err)
    })
  }
  //处理数据专黄
  function jsonToPpt(jsonData: Record<string, any>) {
    console.log(jsonData, 2333)
    const { width, theme, slides } = jsonData
    slidesStore.updateSlideIndex(0)
    slidesStore.setViewportSize(width || 1920)
    slidesStore.setViewportRatio(theme.viewportRatio)
    slidesStore.setTheme(theme)
    slidesStore.setSlides(slides)
  }
  return {
    importPPTXFile,
    exporting
  }
}
