import ConfigInfo, { type BasicInfoData } from '@/components/ConfigInfo';
import { AutoMLTaskType, autoMLTaskTypeOptions, ExperimentStatus } from '@/enums';
import { useComputingResource } from '@/hooks/useComputingResource';
import {
  classifierAlgorithms,
  FrameworkType,
  frameworkTypeOptions,
  queryStrategies,
  regressorAlgorithms,
} from '@/pages/ActiveLearn/components/CreateForm/utils';
import { ActiveLearnData } from '@/pages/ActiveLearn/types';
import ExperimentRunBasic from '@/pages/AutoML/components/ExperimentRunBasic';
import { type NodeStatus } from '@/types';
import {
  formatBoolean,
  formatCodeConfig,
  formatDataset,
  formatDate,
  formatEnum,
  formatMirror,
  formatModel,
} from '@/utils/format';
import classNames from 'classnames';
import { useMemo } from 'react';
import styles from './index.less';

type BasicInfoProps = {
  info?: ActiveLearnData;
  className?: string;
  isInstance?: boolean;
  workflowStatus?: NodeStatus;
  instanceStatus?: ExperimentStatus;
};

function BasicInfo({
  info,
  className,
  workflowStatus,
  instanceStatus,
  isInstance = false,
}: BasicInfoProps) {
  const getResourceDescription = useComputingResource()[1];
  const basicDatas: BasicInfoData[] = useMemo(() => {
    if (!info) {
      return [];
    }

    return [
      {
        label: '实验名称',
        value: info.name,
      },
      {
        label: '实验描述',
        value: info.description,
      },
      {
        label: '创建人',
        value: info.create_by,
      },
      {
        label: '创建时间',
        value: info.create_time,
        format: formatDate,
      },
      {
        label: '更新时间',
        value: info.update_time,
        format: formatDate,
      },
    ];
  }, [info]);

  const configDatas: BasicInfoData[] = useMemo(() => {
    if (!info) {
      return [];
    }

    const modelInfo = [
      {
        label: '预训练模型',
        value: info.model,
        format: formatModel,
      },
      {
        label: '模型文件路径',
        value: info.model_py,
      },
      {
        label: '模型类名称',
        value: info.model_class_name,
      },
      {
        label: 'epochs',
        value: info.epochs,
      },
      {
        label: 'batch_size',
        value: info.batch_size,
      },
    ];

    const lossInfo = [
      {
        label: '学习率',
        value: info.lr,
      },
      {
        label: 'loss文件路径',
        value: info.loss_py,
      },
      {
        label: 'loss类名',
        value: info.loss_class_name,
      },
    ];

    const algorithmInfo = [
      {
        label: info.task_type === AutoMLTaskType.Regression ? '回归算法' : '分类算法',
        value:
          info.task_type === AutoMLTaskType.Regression ? info.regressor_alg : info.classifier_alg,
        format: formatEnum(
          info.task_type === AutoMLTaskType.Regression ? regressorAlgorithms : classifierAlgorithms,
        ),
      },
    ];

    const diffInfo =
      info.framework_type === FrameworkType.Pytorch
        ? [...modelInfo, ...lossInfo]
        : info.framework_type === FrameworkType.Keras
        ? modelInfo
        : algorithmInfo;

    return [
      {
        label: '任务类型',
        value: info.task_type,
        format: formatEnum(autoMLTaskTypeOptions),
      },
      {
        label: '代码配置',
        value: info.code_config,
        format: formatCodeConfig,
      },
      {
        label: '数据集',
        value: info.dataset,
        format: formatDataset,
      },
      {
        label: '数据集处理文件路径',
        value: info.dataset_py,
      },
      {
        label: '数据集处理类名',
        value: info.dataset_class_name,
      },
      {
        label: '镜像',
        value: info.image,
        format: formatMirror,
      },
      {
        label: '资源规格',
        value: info.computing_resource_id,
        format: getResourceDescription,
      },
      {
        label: '框架类型',
        value: info.framework_type,
        format: formatEnum(frameworkTypeOptions),
      },
      ...diffInfo,
      {
        label: '是否打乱',
        value: info.shuffle,
        format: formatBoolean,
      },
      {
        label: '数据量',
        value: info.data_size,
      },
      {
        label: '训练集数据量',
        value: info.train_size,
      },
      {
        label: '初始训练数据量',
        value: info.initial_num,
      },
      {
        label: '查询次数',
        value: info.queries_num,
      },
      {
        label: '每次查询数据量',
        value: info.instances_num,
      },
      {
        label: '查询策略',
        value: info.query_strategy,
        format: formatEnum(queryStrategies),
      },
      {
        label: '检查点轮数',
        value: info.checkpoint_num,
      },
    ];
  }, [info, getResourceDescription]);

  return (
    <div className={classNames(styles['active-learn-basic'], className)}>
      {isInstance && workflowStatus && (
        <ExperimentRunBasic workflowStatus={workflowStatus} instanceStatus={instanceStatus} />
      )}
      {!isInstance && (
        <ConfigInfo
          title="基本信息"
          datas={basicDatas}
          labelWidth={70}
          style={{ marginBottom: '20px' }}
        />
      )}
      <ConfigInfo
        title="配置信息"
        datas={configDatas}
        labelWidth={120}
        style={{ marginBottom: '20px' }}
      ></ConfigInfo>
    </div>
  );
}

export default BasicInfo;
