import Link from "next/link"

import Image from "@/components/Image"
import Button from "@/components/TempDesignSystem/Button"
import BiroScript from "@/components/TempDesignSystem/Text/BiroScript"
import Body from "@/components/TempDesignSystem/Text/Body"
import Title from "@/components/TempDesignSystem/Text/Title"
import { getTheme } from "@/utils/cardTheme"

import { cardVariants } from "./variants"

import styles from "./card.module.css"

import type { CardProps } from "./card"

export default function Card({
  primaryButton,
  secondaryButton,
  scriptedTopTitle,
  heading,
  bodyText,
  className,
  theme,
  backgroundImage,
  imageHeight,
  imageWidth,
  imageGradient,
  onPrimaryButtonClick,
  onSecondaryButtonClick,
}: CardProps) {
  const buttonTheme = getTheme(theme)

  imageHeight = imageHeight || 320

  imageWidth =
    imageWidth ||
    (backgroundImage && "dimensions" in backgroundImage
      ? backgroundImage.dimensions.aspectRatio * imageHeight
      : 420)

  return (
    <article
      className={cardVariants({
        theme,
        className,
      })}
    >
      {backgroundImage && (
        <div className={imageGradient ? styles.imageWrapper : ""}>
          <Image
            src={backgroundImage.url}
            className={styles.image}
            alt={backgroundImage.meta.alt || backgroundImage.title}
            width={imageWidth}
            height={imageHeight}
          />
        </div>
      )}
      <div className={styles.content}>
        {scriptedTopTitle ? (
          <section className={styles.scriptContainer}>
            <BiroScript
              className={styles.scriptedTitle}
              type="two"
              tilted="small"
            >
              {scriptedTopTitle}
            </BiroScript>
          </section>
        ) : null}
        <Title
          as="h4"
          className={styles.heading}
          level="h3"
          textAlign="center"
          textTransform="regular"
        >
          {heading}
        </Title>
        {bodyText ? (
          <Body className={styles.bodyText} textAlign="center">
            {bodyText}
          </Body>
        ) : null}
        <div className={styles.buttonContainer}>
          {primaryButton ? (
            <Button asChild theme={buttonTheme} size="small">
              <Link
                href={primaryButton.href}
                target={primaryButton.openInNewTab ? "_blank" : undefined}
                onClick={onPrimaryButtonClick}
              >
                {primaryButton.title}
              </Link>
            </Button>
          ) : null}
          {secondaryButton ? (
            <Button
              asChild
              theme={buttonTheme}
              size="small"
              intent="secondary"
              disabled
            >
              <Link
                href={secondaryButton.href}
                target={secondaryButton.openInNewTab ? "_blank" : undefined}
                onClick={onSecondaryButtonClick}
              >
                {secondaryButton.title}
              </Link>
            </Button>
          ) : null}
        </div>
      </div>
    </article>
  )
}
