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

export interface TeamMemberCardProps {
  name: string;
  role: string;
  children: React.ReactNode;
}

export function TeamMemberCard({ name, role, children }: TeamMemberCardProps) {
  const { width } = useWindowDimension();
  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={getMemberImagePath(name)}
            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} handleClick={handleClick}>
          {children}
        </ExecPopup>
      )}
    </>
  );
}

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

function ExecPopup({ name, role, children, handleClick }: Propup) {
  return (
    <>
      <div className={styles.popupBackground} onClick={handleClick} />
      <div className={styles.popupContainer}>
        <button className={styles.closeBtn} onClick={handleClick}>
          <Image src="images/team/popup-close.svg" />
        </button>
        <div className={styles.popupContent}>
          <div className={styles.picture}>
            <Image
              className={styles.popupImage}
              src={getMemberImagePath(name)}
              alt={`Picture of ${name}`}
            />
          </div>
          <h1 className={styles.popupName}>{name}</h1>
          <h2 className={styles.popupRole}>{role}</h2>
          <div className={styles.popupDescription}>{children}</div>
        </div>
      </div>
    </>
  );
}
