import { DetailsList, DetailsListLayoutMode, Stack, Text, TextField } from "@fluentui/react";
import { Keys, t } from "Localization";
import React, { useEffect } from "react";
import { useCopyJobContext } from "../../../Context/CopyJobContext";
import { getDefaultJobName } from "../../../CopyJobUtils";
import FieldRow from "../Components/FieldRow";
import { getPreviewCopyJobDetailsListColumns } from "./Utils/PreviewCopyJobUtils";

const PreviewCopyJob: React.FC = () => {
  const { copyJobState, setCopyJobState } = useCopyJobContext();

  const selectedDatabaseAndContainers = [
    {
      sourceDatabaseName: copyJobState.source?.databaseId,
      sourceContainerName: copyJobState.source?.containerId,
      targetDatabaseName: copyJobState.target?.databaseId,
      targetContainerName: copyJobState.target?.containerId,
    },
  ];

  useEffect(() => {
    onJobNameChange(undefined, getDefaultJobName(selectedDatabaseAndContainers));
  }, []);

  const jobName = copyJobState.jobName;

  const onJobNameChange = (_ev?: React.FormEvent, newValue?: string) => {
    setCopyJobState((prevState) => ({
      ...prevState,
      jobName: newValue || "",
    }));
  };
  return (
    <Stack tokens={{ childrenGap: 20 }} className="previewCopyJobContainer" data-test="Panel:PreviewCopyJob">
      <FieldRow label={t(Keys.containerCopy.preview.jobNameLabel)}>
        <TextField data-test="job-name-textfield" value={jobName} onChange={onJobNameChange} />
      </FieldRow>
      <Stack>
        <Text className="bold themeText">{t(Keys.containerCopy.preview.subscriptionLabel)}</Text>
        <Text data-test="destination-subscription-name" className="themeText">
          {copyJobState.target?.subscription?.displayName}
        </Text>
      </Stack>
      <Stack>
        <Text className="bold themeText">{t(Keys.containerCopy.preview.accountLabel)}</Text>
        <Text data-test="destination-account-name" className="themeText">
          {copyJobState.target?.account?.name}
        </Text>
      </Stack>
      <Stack>
        <DetailsList
          items={selectedDatabaseAndContainers}
          layoutMode={DetailsListLayoutMode.justified}
          checkboxVisibility={2}
          columns={getPreviewCopyJobDetailsListColumns()}
        />
      </Stack>
    </Stack>
  );
};

export default PreviewCopyJob;
