import { useMain as useTaskHooks } from '@/views/modelTraining/detail/hooks/task.ts'
import { ref } from 'vue'
import { message } from 'ant-design-vue'

export const useMain = () => {
  const { taskParamsRequest, interruptRequest, statusRequest, logsRequest, modelExportRequest, getExportModelsRequest } = useTaskHooks()

  // 任务参数
  const taskParams = ref<any>({})
  const getTaskParams = async (taskId: string) => {
    const res = await taskParamsRequest.request(taskId)
    if (res?.code == 200) {
      taskParams.value = res.data
    }
    return res
  }

  // 中断
  const onInterrupt = async (taskId: string) => {
    const res = await interruptRequest.request(taskId)
    if (res?.code == 200) {

    }
    return res
  }

  // 任务状态
  const taskStatus = ref<any>({})
  const getStatus = async (taskId: string) => {
    const res = await statusRequest.request(taskId)
    if (res?.code == 200) {
      taskStatus.value = res.data
    }
    return res
  }

  // 任务日志
  const logs = ref<string[]>([])
  let logsController: null | AbortController = null

  // 清空日志
  const clearLogs = () => {
    logs.value = []
  }

  // 获取日志
  const getLogs = async (taskId: string) => {
    const res = await logsRequest.request(taskId)
    // if (res?.status == 200) {
    //   const decoder = new TextDecoder("utf-8")
    //   const reader = res?.body?.getReader()
    //   function read() {
    //   // @ts-ignore
    //     reader.read().then(function processText({ done, value }) {
    //       console.log('logsStream', done, value)
    //       if (done) {
    //         // @ts-ignore
    //         // logs.value = buffer
    //         return
    //       }
    //       const chunk = decoder.decode(value, { stream: false });
    //       const chunks = chunk.split('\n')
    //       console.log('chunks', chunk, chunks)
    //       logs.value.push(...chunks)
    //       read();
    //     });
    //   }
    //   return read();
    // }

    return res
  }

  // 中断日志
  const interruptLogs = () => {
    if (logsController) {
      logsController?.abort()
      logsController = null
    }
  }

  // 获取能够导出的模型格式列表
  const validExportModels = ref<any[]>([])

  const getExportModels = async (taskId: string) => {
    const res = await getExportModelsRequest.request(taskId)
    if (res?.code == 200) {
      validExportModels.value = res.data
    }
    return res
  }

  // 导出模型
  const onModelExport = async (taskId: string, type: string) => {
    const res = await modelExportRequest.request({
      taskId,
      exportType: type
    })
    return res
    // if (res?.code == 200) {
    //   const { exported_model_path } = res.data
    //   message.info({
    //     content: `已经导出到${exported_model_path}`,
    //     duration: 10,
    //   });
    //   // todo
    // }
    // return res
  }

  const state = {
    taskParams,
    taskStatus,
    logs,
    logsController,
    validExportModels
  }

  return {
    state: state,
    api: {
      getTaskParams,
      getStatus,
      getLogs,
      interruptLogs,
      onInterrupt,
      getExportModels,
      onModelExport,
      clearLogs
    },
    requestMap: {
      taskParamsRequest,
      interruptRequest,
      statusRequest,
      logsRequest,
      modelExportRequest,
      getExportModelsRequest
    }
  }
}
