/**
 * First modal of the check-in flow.
 * Checks the Occurrence backend and either goes to CheckInModal or starts setup.
 * @author Benedikt Arnarsson
 */

// Import React
import React, { useEffect } from 'react';

// Import DCE reactkit
import {
  LoadingSpinner,
} from '../../../dce-reactkit';

// Import types
import CheckInUIProps from '../../types/CheckInUIProps';
import MMDDYY from '../../shared/types/MMDDYY';

// Import helpers
import getTimeInfoInET from '../../shared/helpers/getTimeInfoInET';
import isOccurrenceStarted from './isOccurrenceStarted';

/*------------------------------------------------------------------------*/
/* -------------------------------- Types ------------------------------- */
/*------------------------------------------------------------------------*/

// Props definition
type Props = (
  & CheckInUIProps
  & {
    // Callback to continue the setup process
    onSubmit: (mmddyy: MMDDYY, occIsStarted?: boolean) => void,
  }
);

/*------------------------------------------------------------------------*/
/* ------------------------------ Component ----------------------------- */
/*------------------------------------------------------------------------*/

const LoadOccurrence: React.FC<Props> = (props) => {
  /*------------------------------------------------------------------------*/
  /* -------------------------------- Setup ------------------------------- */
  /*------------------------------------------------------------------------*/

  /* -------------- Props ------------- */

  // Destructure all props
  const {
    courseId,
    ihid,
    visitServerEndpoint,
    onSubmit,
  } = props;

  /*------------------------------------------------------------------------*/
  /* ------------------------- Lifecycle Functions ------------------------ */
  /*------------------------------------------------------------------------*/

  /**
   * Mount
   * @author Benedikt Arnarsson
   */
  useEffect(
    () => {
      (async () => {
        const {
          year,
          month,
          day,
        } = getTimeInfoInET();
        const mmddyy = {
          year,
          month,
          day,
        };

        const occBasicInfo = {
          courseId,
          ihid,
          ...mmddyy,
        };

        const occIsStarted = await isOccurrenceStarted(visitServerEndpoint, occBasicInfo);

        onSubmit(mmddyy, occIsStarted);
      })();
    },
    [],
  );

  /*------------------------------------------------------------------------*/
  /* ------------------------------- Render ------------------------------- */
  /*------------------------------------------------------------------------*/

  /*----------------------------------------*/
  /* --------------- Main UI -------------- */
  /*----------------------------------------*/

  return (
    <LoadingSpinner />
  );
};

/*------------------------------------------------------------------------*/
/* ------------------------------- Wrap Up ------------------------------ */
/*------------------------------------------------------------------------*/

// Export component
export default LoadOccurrence;
