import { useMain as useReasoningMain } from '@/views/modelReasoning/index/hooks/reasoning.ts'
import { ref } from 'vue'

export const useMain = () => {

  const {
    gpuUsageRequest,
    gpuInfoRequest,
    memoryInfoRequest,
    cpuUsageRequest,
    datasetListRequest,
    testDatasetListRequest,
    modelListRequest,
    reasoningOptionsRequest,
    modelWeightOptionsRequest,
    createTaskRequest,
    stopTaskRequest,
    taskStatusRequest,
    taskResultRequest,
    taskListRequest,
    logsRequest
  } = useReasoningMain()

  const cpuUsage = ref('')
  const gpuUsage = ref('')
  const memoryUsage = ref('')
  const gpuInfo = ref('')

  // 获取cpu使用情况
  const getCpuUsage = async () => {
    const res = await cpuUsageRequest.request()
    if (res?.code == 200) {
      cpuUsage.value = res.data
    }
    return res
  }

  // 获取gpu使用情况
  const getGpuUsageInfo = async () => {
    const res = await gpuUsageRequest.request()
    if (res?.code == 200) {
      gpuUsage.value = res.data.gpu_usage[0]
    }
    return res
  }

  // 获取内存使用情况
  const getMemoryInfo = async () => {
    const res = await memoryInfoRequest.request()
    if (res?.code == 200) {
      memoryUsage.value = res.data
    }
    return res
  }

  // 获取gpu情况
  const getGpuInfo = async () => {
    const res = await gpuInfoRequest.request()
    if (res?.code == 200) {
      gpuInfo.value = res.data.gpus[0]
    }
    return res
  }


  // 获取系统数据
  const getSystemInfo = async () => {
    const res = await Promise.all([
      getCpuUsage(),
      getGpuUsageInfo(),
      getMemoryInfo(),
      getGpuInfo(),
    ])
    return res
  }

  const datasetList = ref([])

  // 获取数据集
  const getDataset = async () => {
    const res = await datasetListRequest.request()
    console.log('getDataset', res)
    if (res?.code == 200) {
      datasetList.value = res.data?.datasets
    }
    return res
  }

  const testDatasetList = ref([])

  // 获取测试数据集
  const getTestDataset = async () => {
    const res = await testDatasetListRequest.request()
    if (res?.code == 200) {
      testDatasetList.value = res.data?.datasets
    }
    return res
  }

  const models = ref([])

  // 获取模型列表
  const getModelList = async () => {
    const res = await modelListRequest.request()
    if (res?.code == 200) {
      models.value = res?.models
    }
    console.log('getModelList',  models.value)
    return res
  }

  // 模型权重
  const modelWeightOptions = ref([])

  const getModelWeightOptions = async (moduleName: string) => {
    const res = await modelWeightOptionsRequest.request(moduleName)
    if (res?.code == 200) {
      modelWeightOptions.value = res.data
    }
    return res
  }



  const resoningTypes = ref([])

  // 获取推理模式
  const getReasoningOptions = async () => {
    const res = await reasoningOptionsRequest.request()
    if (res?.code == 200) {
      resoningTypes.value = res.data
    }
    return res
  }

  const relationTaskId = ref('')

  // 创建任务
  const createTask = async (params: Record<string, any>) => {
    const res = await createTaskRequest.request(params)
    if (res?.code == 200) {
      relationTaskId.value = res.data.task_id
    }
    return res
  }

  // 停止任务
  const onStopTask = async (taskId: string) => {
    const res = await stopTaskRequest.request(taskId)
    return res
  }
  // 清空任务id

  const cleanTaskId = () => {
    relationTaskId.value = ''
  }

  const taskStatus = ref({})
  let taskController: null | AbortController = null

  // 获取任务状态
  const getTaskStatus = async (taskId: string) => {
    taskController = new AbortController()
    const res = await taskStatusRequest.request(taskId, taskController)
    taskController = null
    if (res?.code == 200) {
      taskStatus.value = res.data
    }
    return res
  }

  const onTaskStatusInterrupt = () => {
    console.log('taskController', taskController)
    if (taskController) {
      taskController.abort()
      taskController = null
    }
  }

  // 重置任务状态
  const resetTaskStatus = () => {
    taskStatus.value = {}
  }

  // 任务结果列表
  const taskResultList = ref<any[]>([])

  // 获取任务结果
  const getTaskResult = async (taskId: string) => {
    const res = await taskResultRequest.request(taskId)
    if (res?.code == 200) {
      taskResultList.value = [res.data]
    }
    return res
  }

  // 获取任务列表
  const taskList = ref([])
  const getTaskList = async () => {
    const res = await taskListRequest.request()
    if (res?.code == 200) {
      taskList.value = res.data.tasks
    }
    return res
  }

  // 获取日志
  const logs = ref<string[]>([])
  let logController: null | AbortController = null

  // 中断日志请求
  const onLogInterrupt = async () => {
    if (logController) {
      logController.abort()
      logController = null;
    }
  }

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

  const getLogs = async (id: string) => {
    logController = new AbortController()
    const res = await logsRequest.request(id, logController)
    if (res?.status == 200) {
      clearLogs()
    }
    return res
  }


  const state = {
    cpuUsage: cpuUsage,
    gpuUsage: gpuUsage,
    memoryUsage: memoryUsage,
    gpuInfo: gpuInfo,
    models: models,
    resoningTypes: resoningTypes,
    datasetList: datasetList,
    testDatasetList: testDatasetList,
    modelWeightOptions: modelWeightOptions,
    relationTaskId: relationTaskId,
    taskResultList: taskResultList,
    taskStatus: taskStatus,
    taskList,
    log: {
      logs: logs,
      logController: logController,
    }
  }

  return {
    state,
    api: {
      getSystemInfo,
      getModelList,
      getDataset,
      getTestDataset,
      getReasoningOptions,
      getModelWeightOptions,
      resetTaskStatus,
      createTask,
      onStopTask,
      cleanTaskId,
      getTaskResult,
      getTaskStatus,
      onTaskStatusInterrupt,
      getTaskList,
      log: {
        getLogs,
        clearLogs,
        onLogInterrupt,
      }
    },
    requestMap: {
      gpuUsageRequest,
      gpuInfoRequest,
      memoryInfoRequest,
      cpuUsageRequest,
      datasetListRequest,
      testDatasetListRequest,
      modelListRequest,
      reasoningOptionsRequest,
      modelWeightOptionsRequest,
      createTaskRequest,
      stopTaskRequest,
      taskStatusRequest,
      taskResultRequest,
      taskListRequest,
      logsRequest
    }
  }
}
