// MiniEventCard.tsx
import React, { ReactNode, useState } from "react";

import { EventSetting } from "./EventSetting";
import { Link } from "./Link";

import styles from "./MiniEventCard.module.css";

interface MiniEventCardProps {
  name: string;
  description: ReactNode;
  short: string;
  online: boolean;
  location: string;
  startDate: Date;
  endDate?: Date;
  poster?: string;
  year: number;
  term: string;
  slug: string;
}

export const MiniEventCard: React.FC<MiniEventCardProps> = ({
  name,
  short,
  description,
  location,
  startDate,
  endDate,
  online,
  poster,
  year,
  term,
  slug,
}) => {
  const [isOpen, setIsOpen] = useState(false);

  const cardBackground = `${styles.card} ${poster ? styles.hasPoster : ""}`;
  const style = poster
    ? { ["--poster-image" as any]: `url(/${poster})` }
    : undefined;
  return (
    <details
      className={cardBackground}
      style={style}
      onToggle={(e) => {
        const details = e.currentTarget as HTMLDetailsElement;
        setIsOpen(details.open);
      }}
    >
      <summary>
        <div className={styles.content}>
          <h2 className={styles.name}>
            <Link href={`/events/${year}/${term}/${slug}`}>{name}</Link>
            <div className={styles.nameSpacer}></div>
          </h2>
          <div className={styles.info}>
            <EventSetting
              startDate={startDate}
              endDate={endDate}
              location={location}
              online={online}
            />
          </div>
          <p className={styles.shortDescription}>{short}</p>
          <div className={styles.details}>
            {isOpen ? "Less" : "More"} {dropDownIcon}
          </div>
        </div>
      </summary>
      <div>{description}</div>
    </details>
  );
};

const dropDownIcon = (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    width="14"
    height="9"
    viewBox="0 0 14 9"
    className={styles.dropDownIcon}
  >
    <path d="M6.24407 8.12713C6.64284 8.58759 7.35716 8.58759 7.75593 8.12713L13.3613 1.65465C13.9221 1.00701 13.4621 0 12.6053 0H1.39467C0.537918 0 0.0778675 1.00701 0.638743 1.65465L6.24407 8.12713Z" />
  </svg>
);
