import React, { useState } from "react";
import { Image } from "./Image";
import useWindowDimensions from "../hooks/useWindowDimension";
import styles from "./TeamMemberCard.module.css";

export interface TeamMemberCardProps {
  name: string;
  role: string;
  image?: string; // path to image of person, relative to public directory
  children: React.ReactNode;
}

export function TeamMemberCard({
  name,
  image,
  role,
  children,
}: TeamMemberCardProps) {
  const { width } = useWindowDimensions();
  const [isOpen, setIsOpen] = useState(false);
  const handleClick = () => {
    if (width <= 768) {
      setIsOpen(!isOpen);
    }
  };
  return (
    <>
      <article className={styles.card} onClick={handleClick}>
        <div className={styles.picture}>
          <Image
            className={styles.image}
            src={image ?? "/images/team/team-member-placeholder.png"}
            alt={`Picture of ${name}`}
          />
        </div>
        <h1 className={styles.name}>{name}</h1>
        <h2 className={styles.role}>{role}</h2>
        <div className={styles.description}>{children}</div>
      </article>
      {isOpen && (
        <ExecPopup
          name={name}
          role={role}
          image={image}
          handleClick={handleClick}
        >
          {children}
        </ExecPopup>
      )}
    </>
  );
}

interface Propup extends TeamMemberCardProps {
  handleClick: () => void;
}

function ExecPopup({ name, image, role, children, handleClick }: Propup) {
  return (
    <>
      <div className={styles.popup_background} onClick={handleClick} />
      <div className={styles.popup_container}>
        <button className={styles.close_btn} onClick={handleClick}>
          <Image src="images/team/popup-close.svg" />
        </button>
        <div className={styles.popup_content}>
          <div className={styles.picture}>
            <Image
              className={styles.popup_image}
              src={image ?? "/images/team/team-member-placeholder.png"}
              alt={`Picture of ${name}`}
            />
          </div>
          <h1 className={styles.popup_name}>{name}</h1>
          <h2 className={styles.popup_role}>{role}</h2>
          <div className={styles.popup_description}>{children}</div>
        </div>
      </div>
    </>
  );
}
