import datasetImg from '@/assets/img/modal-select-dataset.png';
import mirrorImg from '@/assets/img/modal-select-mirror.png';
import modelImg from '@/assets/img/modal-select-model.png';
import { CommonTabKeys, MirrorVersionStatus } from '@/enums';
import {
  getDatasetList,
  getDatasetVersionIdList,
  getDatasetVersionsById,
  getModelList,
  getModelVersionIdList,
  getModelVersionsById,
} from '@/services/dataset/index.js';
import { getMirrorListReq, getMirrorVersionListReq } from '@/services/mirror';
import type { TabsProps } from 'antd';

export enum ResourceSelectorType {
  Model = 'Model', // 模型
  Dataset = 'Dataset', // 数据集
  Mirror = 'Mirror', //镜像
}

export type MirrorVersion = {
  id: number; // 镜像版本 id
  status: MirrorVersionStatus; // 镜像版本状态
  tag_name: string; // 镜像版本 name
  url: string; // 镜像版本路径
};

export type SelectorTypeInfo = {
  getList: (params: any) => Promise<any>; // 获取资源列表
  getVersions: (params: any) => Promise<any>; // 获取资源版本列表
  getFiles: (params: any) => Promise<any>; // 获取资源文件列表
  handleVersionResponse: (res: any) => any[]; // 处理版本列表接口数据
  modalIcon: string; // modal icon
  buttonIcon: string; // button icon
  name: string; // 名称
  litReqParamKey: 'available_range' | 'image_type'; // 表示是公开还是私有的参数名称，获取资源列表接口使用
  fileReqParamKey: 'models_id' | 'dataset_id'; // 文件请求参数名称，获取文件列表接口使用
  tabItems: TabsProps['items']; // tab 列表
  buttontTitle: string; // 按钮 title
};

// 获取镜像文件列表，为了兼容数据集和模型
const getMirrorFilesReq = ({ id, version }: { id: number; version: string }): Promise<any> => {
  const index = version.indexOf('-');
  const url = version.slice(index + 1);
  return Promise.resolve({
    data: {
      path: url,
      content: [
        {
          id: `${id}-${version}`,
          file_name: `${url}`,
        },
      ],
    },
  });
};

export const selectorTypeConfig: Record<ResourceSelectorType, SelectorTypeInfo> = {
  [ResourceSelectorType.Model]: {
    getList: getModelList,
    getVersions: getModelVersionsById,
    getFiles: getModelVersionIdList,
    handleVersionResponse: (res) => res.data || [],
    name: '模型',
    modalIcon: modelImg,
    buttonIcon: 'icon-xuanzemoxing',
    litReqParamKey: 'available_range',
    fileReqParamKey: 'models_id',
    tabItems: [
      {
        key: CommonTabKeys.Private,
        label: '我的模型',
      },
      {
        key: CommonTabKeys.Public,
        label: '公开模型',
      },
    ],
    buttontTitle: '选择模型',
  },
  [ResourceSelectorType.Dataset]: {
    getList: getDatasetList,
    getVersions: getDatasetVersionsById,
    getFiles: getDatasetVersionIdList,
    handleVersionResponse: (res) => res.data || [],
    name: '数据集',
    modalIcon: datasetImg,
    buttonIcon: 'icon-xuanzeshujuji',
    litReqParamKey: 'available_range',
    fileReqParamKey: 'dataset_id',
    tabItems: [
      {
        key: CommonTabKeys.Private,
        label: '我的数据集',
      },
      {
        key: CommonTabKeys.Public,
        label: '公开数据集',
      },
    ],
    buttontTitle: '选择数据集',
  },
  [ResourceSelectorType.Mirror]: {
    getList: getMirrorListReq,
    getVersions: (id: number) => getMirrorVersionListReq({ image_id: id, page: 0, size: 200 }),
    getFiles: getMirrorFilesReq,
    handleVersionResponse: (res) =>
      res.data?.content?.filter((v: MirrorVersion) => v.status === MirrorVersionStatus.Available) ||
      [],
    name: '镜像',
    modalIcon: mirrorImg,
    buttonIcon: 'icon-xuanzejingxiang',
    litReqParamKey: 'image_type',
    fileReqParamKey: 'dataset_id',
    tabItems: [
      {
        key: CommonTabKeys.Private,
        label: '我的镜像',
      },
      {
        key: CommonTabKeys.Public,
        label: '公开镜像',
      },
    ],
    buttontTitle: '选择镜像',
  },
};
