import * as React from 'react';
import {
  Button,
  ButtonVariant,
  Icon,
  Modal,
  ModalBody,
  ModalFooter,
  ModalHeader,
  ModalVariant,
} from '@patternfly/react-core';
import { InfoCircleIcon } from '@patternfly/react-icons/dist/js/icons/info-circle-icon';

import { ConfigMapK8sResource } from '../../types';
import HostRequirements, {
  HostRequirementsListProps,
} from '../../../common/components/hosts/HostRequirements';
import { useTranslation } from '../../../common/hooks/use-translation-wrapper';

type HostData = {
  cpu_cores: number;
  ram_mib: number;
  disk_size_gb: number;
};

type HWRequirements = {
  version: string;
  master: HostData;
  worker: HostData;
  sno: HostData;
};

type MinimalHWRequirementsProps = {
  aiConfigMap: ConfigMapK8sResource;
  isSNOCluster?: boolean;
};

type MinimalHWRequirementsModalProps = MinimalHWRequirementsProps & {
  isOpen: boolean;
  onClose: () => void;
};

export const getHWRequirements = (aiConfigMap: ConfigMapK8sResource): HostRequirementsListProps => {
  let hwRequirements: HWRequirements | undefined;
  try {
    const hwData = JSON.parse(
      aiConfigMap.data?.HW_VALIDATOR_REQUIREMENTS || '[]',
    ) as HWRequirements[];

    hwRequirements = hwData.find((req: { version?: string }) => req.version === 'default');
  } catch {
    // console.error('Failed to parse hw requirements config map');
    hwRequirements = undefined;
  }

  return {
    master: {
      cpuCores: hwRequirements?.master?.cpu_cores,
      ramMib: hwRequirements?.master?.ram_mib,
      diskSizeGb: hwRequirements?.master?.disk_size_gb,
    },
    worker: {
      cpuCores: hwRequirements?.worker?.cpu_cores,
      ramMib: hwRequirements?.worker?.ram_mib,
      diskSizeGb: hwRequirements?.worker?.disk_size_gb,
    },
    sno: {
      cpuCores: hwRequirements?.sno?.cpu_cores,
      ramMib: hwRequirements?.sno?.ram_mib,
      diskSizeGb: hwRequirements?.sno?.disk_size_gb,
    },
  };
};

const MinimalHWRequirements: React.FC<MinimalHWRequirementsProps> = ({
  aiConfigMap,
  isSNOCluster,
}) => {
  const [isHostRequirementsOpen, setHostRequirementsOpen] = React.useState(false);
  const { t } = useTranslation();
  return (
    <>
      <Button
        variant={ButtonVariant.link}
        onClick={() => setHostRequirementsOpen(true)}
        isInline
        className="host-requirements-link"
      >
        <Icon status="warning" size="sm">
          <InfoCircleIcon />
        </Icon>
        &nbsp;{t('ai:Minimum hardware requirements')}
      </Button>
      <MinimalHWRequirementsModal
        isOpen={isHostRequirementsOpen}
        onClose={() => setHostRequirementsOpen(false)}
        aiConfigMap={aiConfigMap}
        isSNOCluster={isSNOCluster}
      />
    </>
  );
};

export const MinimalHWRequirementsModal = ({
  isOpen,
  aiConfigMap,
  isSNOCluster,
  onClose,
}: MinimalHWRequirementsModalProps) => {
  const { t } = useTranslation();
  return (
    <Modal isOpen={isOpen} onClose={onClose} variant={ModalVariant.medium}>
      <ModalHeader title={t('ai:Minimum hardware requirements')} />
      <ModalBody>
        <HostRequirements {...getHWRequirements(aiConfigMap)} isSNOCluster={isSNOCluster} />
      </ModalBody>
      <ModalFooter>
        <Button key="close" variant={ButtonVariant.primary} onClick={onClose}>
          {t('ai:Close')}
        </Button>
      </ModalFooter>
    </Modal>
  );
};

export default MinimalHWRequirements;
