import BasicTableInfo, { BasicInfoData } from '@/components/BasicTableInfo';
import SubAreaTitle from '@/components/SubAreaTitle';
import { DatasetData, ModelData, ResourceType, resourceConfig } from '@/pages/Dataset/config';
import ModelMetrics from '@/pages/Model/components/ModelMetrics';
import { formatCodeConfig, formatDatasets, formatSource, formatTrainTask } from '@/utils/format';
import classNames from 'classnames';
import styles from './index.less';

type ResourceIntroProps = {
  resourceType: ResourceType;
  info: DatasetData | ModelData;
  resourceId: number;
  identifier: string;
  owner: string;
  version?: string;
};

const getDatasetDatas = (data: DatasetData): BasicInfoData[] => [
  {
    label: '数据集名称',
    value: data.name,
  },
  {
    label: '数据集版本',
    value: data.version,
  },
  {
    label: '创建人',
    value: data.create_by,
  },
  {
    label: '更新时间',
    value: data.update_time,
  },
  {
    label: '数据来源',
    value: data.dataset_source,
    format: formatSource,
  },
  {
    label: '训练任务',
    value: data.train_task,
    format: formatTrainTask,
  },
  {
    label: '处理代码',
    value: data.processing_code,
    format: formatCodeConfig,
  },
  {
    label: '数据集分类',
    value: data.data_type,
  },
  {
    label: '研究方向',
    value: data.data_tag,
  },
];

const getModelDatas = (data: ModelData): BasicInfoData[] => [
  {
    label: '模型名称',
    value: data.name,
    ellipsis: true,
  },
  {
    label: '模型版本',
    value: data.version,
    ellipsis: true,
  },
  {
    label: '创建人',
    value: data.create_by,
    ellipsis: true,
  },
  {
    label: '更新时间',
    value: data.update_time,
    ellipsis: true,
  },
  {
    label: '训练镜像',
    value: data.image,
    ellipsis: true,
  },
  {
    label: '训练代码',
    value: data.project_depency,
    format: formatCodeConfig,
    ellipsis: true,
  },
  {
    label: '训练数据集',
    value: data.train_datasets,
    format: formatDatasets,
    ellipsis: true,
  },
  {
    label: '测试数据集',
    value: data.test_datasets,
    format: formatDatasets,
    ellipsis: true,
  },
  {
    label: '模型来源',
    value: data.model_source,
    format: formatSource,
    ellipsis: true,
  },
  {
    label: '训练任务',
    value: data.train_task,
    format: formatTrainTask,
    ellipsis: true,
  },
  {
    label: '模型框架',
    value: data.model_type,
    ellipsis: true,
  },
  {
    label: '模型能力',
    value: data.model_tag,
    ellipsis: true,
  },
];

function ResourceIntro({
  resourceType,
  info,
  resourceId,
  identifier,
  owner,
  version,
}: ResourceIntroProps) {
  const config = resourceConfig[resourceType];
  const basicDatas: BasicInfoData[] =
    resourceType === ResourceType.Dataset
      ? getDatasetDatas(info as DatasetData)
      : getModelDatas(info as ModelData);

  return (
    <div
      className={classNames(styles['resource-intro'], {
        [styles['resource-intro--dataset']]: resourceType === ResourceType.Dataset,
      })}
    >
      <div className={styles['resource-intro__top']}>
        <SubAreaTitle
          title="基本信息"
          image={require('@/assets/img/mirror-basic.png')}
          style={{ marginBottom: '15px' }}
        ></SubAreaTitle>
        <div className={styles['resource-intro__top__basic']}>
          <BasicTableInfo datas={basicDatas} labelWidth={135}></BasicTableInfo>
        </div>
        <div className={styles['resource-intro__top__title']}>{`${config.name}描述`}</div>
        <div className={styles['resource-intro__top__desc']}>{info.description ?? '暂无描述'}</div>
        <div className={styles['resource-intro__top__title']}>版本描述</div>
        <div className={styles['resource-intro__top__desc']}>{info.version_desc ?? '暂无描述'}</div>
        <SubAreaTitle
          title="实例用法"
          image={require('@/assets/img/usage-icon.png')}
          style={{ margin: '25px 0 15px' }}
        ></SubAreaTitle>
        <div
          className={styles['resource-intro__top__usage']}
          dangerouslySetInnerHTML={{ __html: info.usage ?? '暂无实例用法' }}
        ></div>
      </div>
      {resourceType === ResourceType.Model && version && (
        <ModelMetrics
          resourceId={resourceId}
          identifier={identifier}
          owner={owner}
          version={version}
        ></ModelMetrics>
      )}
    </div>
  );
}

export default ResourceIntro;
