import { TextField } from "@mui/material";
import { ReactNode, useCallback, useState } from "react";
import Dialog from "../../../UI/Dialog";

export default function VideoMeetingInfoDialog({
  elementId,
  showDialog,
  onSuccessfulClose,
  onBadClose,
  sessionPassword,
}: {
  elementId: string;
  showDialog: boolean;
  onSuccessfulClose: (name: string, email: string) => void;
  onBadClose: () => void;
  sessionPassword: string;
}) {
  const [passwordValidated, setPasswordValidated] = useState<boolean>(
    sessionPassword === "",
  );

  const [name, setName] = useState<string>("");
  const [email, setEmail] = useState<string>("");
  const [passwordError, setPasswordError] = useState<ReactNode>(null);

  const closeCallback = useCallback(() => {
    if (passwordValidated || sessionPassword.length === 0) {
      onSuccessfulClose(name, email);
    } else {
      onBadClose();
    }
  }, [
    passwordValidated,
    name,
    email,
    onBadClose,
    onSuccessfulClose,
    sessionPassword,
  ]);

  return (
    <Dialog
      // closed via unmount (passwordValidated = true)
      open={showDialog}
      title="Join meeting"
      onClose={closeCallback}
      onCancel={closeCallback}
      cancelButton="Cancel"
      closeButton="Submit"
      closeButtonProps={{
        type: "submit",
        form: `video-meeting-${elementId}-form`,
        onClick: () => ({}),
      }}
    >
      <form
        id={`video-meeting-${elementId}-form`}
        onSubmit={(e) => {
          e.preventDefault();
          if (
            sessionPassword.length &&
            (e.target as HTMLFormElement).password.value !== sessionPassword
          ) {
            setPasswordError("Invalid password");
            setPasswordValidated(false);
          } else if ((e.target as HTMLFormElement).reportValidity()) {
            setPasswordValidated(true);
            onSuccessfulClose(name, email);
          }
        }}
      >
        <TextField
          style={{ marginTop: "0.5rem" }}
          label="Name"
          name="name"
          variant="outlined"
          required
          fullWidth
          onInput={(e) => setName((e.target as HTMLInputElement).value)}
        />
        <TextField
          style={{ marginTop: "0.5rem" }}
          label="Email"
          name="email"
          type="email"
          variant="outlined"
          required
          fullWidth
          onInput={(e) => setEmail((e.target as HTMLInputElement).value)}
        />
        <TextField
          style={{
            marginTop: "0.5rem",
            display: sessionPassword.length === 0 ? "none" : "block",
          }}
          label="Meeting Password"
          name="password"
          variant="outlined"
          required={sessionPassword.length !== 0}
          fullWidth
          helperText={passwordError}
          error={passwordError !== null}
          inputProps={{ name: "password" }}
          onInput={() => {
            setPasswordError(null);
          }}
        />
      </form>
    </Dialog>
  );
}
