import React from "react";

import { uniqueId } from "lodash";
import ReactTooltip from "react-tooltip";

import { formatFloatAsPercentage } from "utilities/helpers";

import Icon from "components/Icon";
import { COLORS } from "styles/var/colors";

const baseClass = "probability-of-exploit";

interface IProbabilityOfExploit {
  probabilityOfExploit: number;
  cisaKnownExploit?: boolean | null;
  tooltipPosition?: "top" | "bottom" | "left" | "right";
}

const ProbabilityOfExploit = ({
  probabilityOfExploit,
  cisaKnownExploit,
  tooltipPosition = "top",
}: IProbabilityOfExploit) => {
  const renderExploitedIcon = () => {
    const tooltipId = uniqueId();
    return (
      <>
        <span
          className={`${baseClass} tooltip tooltip__tooltip-icon`}
          data-tip
          data-for={tooltipId}
        >
          <Icon name="error" size="small" color="status-error" />
        </span>

        <ReactTooltip
          place={tooltipPosition}
          effect="solid"
          backgroundColor={COLORS["tooltip-bg"]}
          id={tooltipId}
          data-html
        >
          <span className={`tooltip__tooltip-text`}>
            <>
              The vulnerability has been actively exploited in the wild. This
              data is reported by the Cybersecurity and Infrastructure Security
              Agency (CISA).
            </>
          </span>
        </ReactTooltip>
      </>
    );
  };

  return (
    <span className={baseClass}>
      {formatFloatAsPercentage(probabilityOfExploit)}
      {cisaKnownExploit && renderExploitedIcon()}
    </span>
  );
};

export default ProbabilityOfExploit;
