import { useMain as usePerformanceMain } from '@/views/modelReasoning/index/hooks/performance.ts'
import { useMain as usePerformaceChatMain } from '@/views/modelReasoning/index/hooks/barChat.ts'
import { ref } from 'vue'

export const useMain = () => {
  const { getModelByDatasetRequest, getPerformanceRequest } = usePerformanceMain()

  const {
    createSeriesOptions,
    options: performanceOptions
  } = usePerformaceChatMain()

  const models = ref([])

  const getModelsByDataset = async (datasetId: string) => {
    const res = await getModelByDatasetRequest.request(datasetId)
    if (res?.code == 200) {
      models.value = res.data
    }
    return res
  }

  const performanceData = ref([])

  const getPerformance = async (data: Record<string, any>) => {
    const res = await getPerformanceRequest.request(data)
    if (res?.code == 200) {
      performanceData.value = res.data
    }
    return res
  }

  const onUpdateChatOptions = (data: Record<string, any>[]) => {
    const showXAxisData: string[] = []
    const seriesList: Record<string, any> = []
    const titleMap: Record<string, any> = {
      eva_batch_top1: '批平均准确率',
      eva_class_top1 :'类平均准确率',
      point_acc: '点平均准确率'
    }
    for(const type in titleMap) {
      const series = createSeriesOptions({
        title: titleMap[type],
        key: type
      })
      seriesList.push(series)
    }
    data.forEach((item: Record<string, any>, index) => {
      const { eva_batch_metrics, eva_class_metrics, point_acc } = item.metrics
      const { eva_batch_top1 } = eva_batch_metrics
      const { eva_class_top1 } = eva_class_metrics
      showXAxisData.push(`第${index + 1}次`)
      seriesList[0].data.push(eva_batch_top1)
      seriesList[1].data.push(eva_class_top1)
      seriesList[2].data.push(point_acc)
    })
    performanceOptions.value = {
      ...performanceOptions.value,
      series: seriesList,
      xAxis: [
        {
          ...performanceOptions.value.xAxis[0],
          data: showXAxisData
        }
      ]
    }
    console.log('performanceOptions', performanceOptions.value)
    // performanceOptions
  }

  const createChatOptions = (groupData: Record<string, any>) => {
    const seriesList = []
    const xAxisData = []
    const titleMap: Record<string, any> = {
      eva_batch_top1: '批平均准确率',
      eva_class_top1 :'类平均准确率',
      point_acc: '点平均准确率'
    }
    for(const type in titleMap) {
      const series = createSeriesOptions({
        title: titleMap[type],
        key: type
      })
      seriesList.push(series)
    }
    const dataName = `${groupData.modelInfo.label}_${groupData.weightInfo.label}`
    groupData.data.records.forEach((item: Record<string, any>, index) => {
      const { eva_batch_metrics, eva_class_metrics, point_acc } = item.metrics
      const { eva_batch_top1 } = eva_batch_metrics
      const { eva_class_top1 } = eva_class_metrics
      xAxisData.push(`第${index + 1}次`)
      seriesList[0].data.push(eva_batch_top1)
      seriesList[1].data.push(eva_class_top1)
      seriesList[2].data.push(point_acc)
    })

    return {
      groupName: dataName,
      xAxis: xAxisData,
      series: seriesList
    }
    // data.forEach((item: Record<string, any>, index: number) => {
    //   const { eva_batch_metrics, eva_class_metrics, point_acc } = item.metrics
    //   const { eva_batch_top1 } = eva_batch_metrics
    //   const { eva_class_top1 } = eva_class_metrics
    //   showXAxisData.push(`第${index + 1}次`)
    //   seriesList[0].data.push(eva_batch_top1)
    //   seriesList[1].data.push(eva_class_top1)
    //   seriesList[2].data.push(point_acc)
    // })
    // performanceOptions.value = {
    //   ...performanceOptions.value,
    //   series: seriesList,
    //   xAxis: [
    //     {
    //       ...performanceOptions.value.xAxis[0],
    //       data: showXAxisData
    //     }
    //   ]
    // }
    // console.log('performanceOptions', performanceOptions.value)
  }

  const state = {
    models,
    performanceData,
    performanceOptions
  }

  return {
    state: state,
    api: {
      getModelsByDataset,
      getPerformance,
      onUpdateChatOptions,
      createChatOptions
    },
    requestMap: {
      getModelByDatasetRequest,
      getPerformanceRequest
    }
  }

}
