import ConfigInfo, { type BasicInfoData } from '@/components/ConfigInfo';
import { ExperimentStatus, hyperParameterOptimizedMode } from '@/enums';
import { useComputingResource } from '@/hooks/useComputingResource';
import ExperimentRunBasic from '@/pages/AutoML/components/ExperimentRunBasic';
import {
  schedulerAlgorithms,
  searchAlgorithms,
} from '@/pages/HyperParameter/components/CreateForm/utils';
import { HyperParameterData } from '@/pages/HyperParameter/types';
import { type NodeStatus } from '@/types';
import {
  formatCodeConfig,
  formatDataset,
  formatDate,
  formatEnum,
  formatMirror,
  formatModel,
} from '@/utils/format';
import classNames from 'classnames';
import { useMemo } from 'react';
import ParameterInfo from '../ParameterInfo';
import styles from './index.less';

// 格式化优化方向
const formatOptimizeMode = (value: string) => {
  return value === hyperParameterOptimizedMode.Max ? '越大越好' : '越小越好';
};

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

function HyperParameterBasic({
  info,
  className,
  workflowStatus,
  instanceStatus,
  isInstance = false,
}: HyperParameterBasicProps) {
  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 [];
    }
    return [
      {
        label: '代码配置',
        value: info.code_config,
        format: formatCodeConfig,
      },
      {
        label: '主函数代码文件',
        value: info.main_py,
      },
      {
        label: '镜像',
        value: info.image,
        format: formatMirror,
      },
      {
        label: '数据集',
        value: info.dataset,
        format: formatDataset,
      },
      {
        label: '模型',
        value: info.model,
        format: formatModel,
      },
      {
        label: '总试验次数',
        value: info.num_samples,
      },
      {
        label: '搜索算法',
        value: info.search_alg,
        format: formatEnum(searchAlgorithms),
      },
      {
        label: '调度算法',
        value: info.scheduler,
        format: formatEnum(schedulerAlgorithms),
      },
      {
        label: '单次试验最大时间',
        value: info.max_t,
      },
      {
        label: '最小试验数',
        value: info.min_samples_required,
      },
      {
        label: '指标优化方向',
        value: info.mode,
        format: formatOptimizeMode,
      },
      {
        label: '指标',
        value: info.metric,
      },
      {
        label: '资源规格',
        value: info.computing_resource_id,
        format: getResourceDescription,
      },
    ];
  }, [info, getResourceDescription]);

  return (
    <div className={classNames(styles['hyper-parameter-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' }}
      >
        {info && <ParameterInfo info={info} />}
      </ConfigInfo>
    </div>
  );
}

export default HyperParameterBasic;
