import { gpuUsageApi, gpuInfoApi, memoryInfoApi, cpuUsageApi } from '@/apis/system/index.ts'
import { reasonDatasetListApi, datasetListApi } from '@/apis/dataset/index.ts'
import { listApi as modelListApi } from '@/apis/model/index.ts'

import {
  optionsApi as reasoningTypeOptionsApi,
  weightOptionsApi,
  createTaskApi,
  taskStatusApi,
  taskResultApi,
  listApi as taskListApi,
  logsApi,
  stopTaskApi
} from '@/apis/train/inference.ts'
import { ref } from 'vue'

export const useMain = () => {
  // 获取gpu使用情况loading
  const gpuUsageLoading = ref(false)

  // 获取gpu使用情况
  const getGpuUsage = async () => {
    if (gpuUsageLoading.value) return false
    gpuUsageLoading.value = true
    const res = await gpuUsageApi().catch(err => {
      return err
    })
    gpuUsageLoading.value = false
    return res
  }

  // 获取gpu信息loading
  const gpuInfoLoading = ref(false)

  // 获取gpu信息
  const getGpuInfo = async () => {
    if (gpuInfoLoading.value) return false
    gpuInfoLoading.value = true
    const res = await gpuInfoApi().catch(err => {
      return err
    })
    gpuInfoLoading.value = false
    return res
  }

  // 内存loading
  const memoryInfoLoading = ref(false)

  // 获取内存信息
  const getMemoryInfo = async () => {
    if (memoryInfoLoading.value) return false
    memoryInfoLoading.value = true
    const res = await memoryInfoApi().catch(err => {
      return err
    })
    memoryInfoLoading.value = false
    return res
  }

  // cpu使用loading
  const cpuUsageLoading = ref(false)

  // 获取内存信息
  const getCpuUsage= async () => {
    if (cpuUsageLoading.value) return false
    cpuUsageLoading.value = true
    const res = await cpuUsageApi().catch(err => {
      return err
    })
    cpuUsageLoading.value = false
    return res
  }

  // 获取数据集loading
  const datasetListLoading = ref(false)

  // 获取数据集
  const getDatasetList = async () => {
    if (datasetListLoading.value) return false
    datasetListLoading.value = true
    const res = await reasonDatasetListApi().catch(err => {
      return err
    })
    datasetListLoading.value = false
    return res
  }

  // 获取测试数据集
  const testDatasetListLoading = ref(false)
  const getTestDataList = async () => {
    if (testDatasetListLoading.value) return false
    testDatasetListLoading.value = true
    const res = await datasetListApi({
      type: 'test'
    }).catch(err => {
      return err
    })
    testDatasetListLoading.value = false
    return res
  }

  // 模型列表
  const modelListLoading = ref(false)
  const getModelList = async () => {
    if (modelListLoading.value) return false
    modelListLoading.value = true
    const res = await modelListApi().catch(err => {
      return err
    })
    modelListLoading.value = false
    return res
  }

  // 推理模式
  const reasoningTypeOptionsLoading = ref(false)
  const getReasoningTypeOptions = async () => {
    if (reasoningTypeOptionsLoading.value) return false
    reasoningTypeOptionsLoading.value = true
    const res = await reasoningTypeOptionsApi().catch(err => {
      return err
    })
    reasoningTypeOptionsLoading.value = false
    return res
  }

  // 模型权重
  const modelWeightOptionsLoading = ref(false)
  const getModelWeightOptions = async (modelId: string) => {
    if (modelWeightOptionsLoading.value) return false
    modelWeightOptionsLoading.value = true
    const res = await weightOptionsApi({
      model_id: modelId
    }).catch(err => {
      return err
    })
    modelWeightOptionsLoading.value = false
    return res
  }

  // 创建推理任务
  const createTaskLoading = ref(false)
  const createTask = async (data: Record<string, any>) => {
    if (createTaskLoading.value) return false
    createTaskLoading.value = true
    const res = await createTaskApi(data).catch(err => {
      return err
    })
    createTaskLoading.value = false
    return res
  }

  // 停止推理任务
  const stopTaskLoading = ref(false)
  const stopTask = async (taskId: string) => {
    if (stopTaskLoading.value) return false
    stopTaskLoading.value = true
    const res = await stopTaskApi(taskId).catch(err => {
      return err
    })
    stopTaskLoading.value = false
    return res
  }

  // 推理任务列表
  const taskListLoading = ref(false)
  const getTaskList = async () => {
    if (taskListLoading.value) return false
    taskListLoading.value = true
    const res = await taskListApi().catch(err => {
      return err
    })
    taskListLoading.value = false
    return res
  }

  // 获取任务状态
  const taskStatusLoading = ref(false)
  const getTaskStatus = async (id: string, controller?: AbortController) => {
    if (taskStatusLoading.value) return false
    taskStatusLoading.value = true
    const res = await taskStatusApi(id, controller).catch(err => {
      return err
   })
    taskStatusLoading.value = false
    return res
  }

  // 获取任务结果
  const taskResultLoading = ref(false)
  const getTaskResult = async (id: string, controller?: AbortController) => {
    if (taskResultLoading.value) return false
    taskResultLoading.value = true
    const res = await taskResultApi(id, controller).catch(err => {
      return err
    })
    taskResultLoading.value = false
    return res
  }

  // 获取任务日志
  const getLogsLoading = ref(false)
  const getLogs = async (id: string, controller?: AbortController) => {
    if (getLogsLoading.value) return false
    getLogsLoading.value = true
    const res = await logsApi(id, controller).catch(err => {
      return err
    })
    getLogsLoading.value = false
    return res
  }


  return {
    gpuUsageRequest: {
      request: getGpuUsage,
      loading: gpuUsageLoading
    },
    gpuInfoRequest: {
      request: getGpuInfo,
      loading: gpuInfoLoading
    },
    memoryInfoRequest: {
      request: getMemoryInfo,
      loading: memoryInfoLoading
    },
    cpuUsageRequest: {
      request: getCpuUsage,
      loading: cpuUsageLoading
    },
    datasetListRequest: {
      request: getDatasetList,
      loading: datasetListLoading
    },
    testDatasetListRequest: {
      request: getTestDataList,
      loading: testDatasetListLoading
    },
    modelListRequest: {
      request: getModelList,
      loading: modelListLoading
    },
    reasoningOptionsRequest: {
      request: getReasoningTypeOptions,
      loading: reasoningTypeOptionsLoading
    },
    modelWeightOptionsRequest: {
      request: getModelWeightOptions,
      loading: modelWeightOptionsLoading
    },
    createTaskRequest: {
      request: createTask,
      loading: createTaskLoading
    },
    stopTaskRequest: {
      request: stopTask,
      loading: stopTaskLoading
    },
    taskListRequest: {
      request: getTaskList,
      loading: taskListLoading
    },
    taskStatusRequest: {
      request: getTaskStatus,
      loading: taskStatusLoading
    },
    taskResultRequest: {
      request: getTaskResult,
      loading: taskResultLoading
    },
    logsRequest: {
      request: getLogs,
      loading: getLogsLoading
    }
  }
}
