import { ExperimentStatus } from '@/enums';
import EmptyLog from '@/pages/AutoML/components/ExperimentLog/empty';
import LogList from '@/pages/Experiment/components/LogList';
import { HyperParameterInstanceData } from '@/pages/HyperParameter/types';
import { NodeStatus } from '@/types';
import { Tabs } from 'antd';
import styles from './index.less';

const NodePrefix = 'auto-hpo';

type ExperimentLogProps = {
  instanceInfo: HyperParameterInstanceData;
  nodes: Record<string, NodeStatus>;
};

function ExperimentLog({ instanceInfo, nodes }: ExperimentLogProps) {
  let hpoNodeStatus: NodeStatus | undefined;
  let frameworkCloneNodeStatus: NodeStatus | undefined;
  let trainCloneNodeStatus: NodeStatus | undefined;

  Object.keys(nodes)
    .sort((key1, key2) => {
      const node1 = nodes[key1];
      const node2 = nodes[key2];
      return new Date(node1.startedAt).getTime() - new Date(node2.startedAt).getTime();
    })
    .forEach((key) => {
      const node = nodes[key];
      if (node.displayName.startsWith(NodePrefix)) {
        hpoNodeStatus = node;
      } else if (node.displayName.startsWith('git-clone') && !frameworkCloneNodeStatus) {
        frameworkCloneNodeStatus = node;
      } else if (
        node.displayName.startsWith('git-clone') &&
        frameworkCloneNodeStatus &&
        node.displayName !== frameworkCloneNodeStatus?.displayName
      ) {
        trainCloneNodeStatus = node;
      }
    });

  const tabItems = [
    // {
    //   key: 'git-clone-framework',
    //   label: '框架代码日志',
    //   // icon: <KFIcon type="icon-rizhi1" />,
    //   children: (
    //     <div className={styles['experiment-log__tabs__log']}>
    //       {frameworkCloneNodeStatus && (
    //         <LogList
    //           instanceName={instanceInfo.argo_ins_name}
    //           instanceNamespace={instanceInfo.argo_ins_ns}
    //           pipelineNodeId={frameworkCloneNodeStatus.displayName}
    //           workflowId={frameworkCloneNodeStatus.id}
    //           instanceNodeStartTime={frameworkCloneNodeStatus.startedAt}
    //           instanceNodeStatus={frameworkCloneNodeStatus.phase as ExperimentStatus}
    //         ></LogList>
    //       )}
    //     </div>
    //   ),
    // },
    {
      key: 'git-clone-train',
      label: '系统日志',
      // icon: <KFIcon type="icon-rizhi1" />,
      children: (
        <div className={styles['experiment-log__tabs__log']}>
          {trainCloneNodeStatus ? (
            <LogList
              instanceName={instanceInfo.argo_ins_name}
              instanceNamespace={instanceInfo.argo_ins_ns}
              pipelineNodeId={trainCloneNodeStatus.displayName}
              workflowId={trainCloneNodeStatus.id}
              instanceNodeStartTime={trainCloneNodeStatus.startedAt}
              instanceNodeStatus={trainCloneNodeStatus.phase as ExperimentStatus}
            ></LogList>
          ) : (
            <EmptyLog />
          )}
        </div>
      ),
    },
    {
      key: 'auto-hpo',
      label: '超参寻优日志',
      // icon: <KFIcon type="icon-rizhi1" />,
      children: (
        <div className={styles['experiment-log__tabs__log']}>
          {hpoNodeStatus ? (
            <LogList
              instanceName={instanceInfo.argo_ins_name}
              instanceNamespace={instanceInfo.argo_ins_ns}
              pipelineNodeId={hpoNodeStatus.displayName}
              workflowId={hpoNodeStatus.id}
              instanceNodeStartTime={hpoNodeStatus.startedAt}
              instanceNodeStatus={hpoNodeStatus.phase as ExperimentStatus}
            ></LogList>
          ) : (
            <EmptyLog />
          )}
        </div>
      ),
    },
  ];

  return (
    <div className={styles['experiment-log']}>
      <Tabs className={styles['experiment-log__tabs']} items={tabItems} />
    </div>
  );
}

export default ExperimentLog;
