const BRAND_BACKGROUND = "#020617";
const BRAND_PANEL = "#0f172a";
const BRAND_PANEL_BORDER = "#1e293b";
const BRAND_FOREGROUND = "#f8fafc";
const BRAND_MUTED = "#94a3b8";
const BRAND_GREEN = "#16a34a";

function BrandTriangle({ size }: { size: number }) {
  const halfWidth = Math.round(size / 2);

  return (
    <div
      style={{
        width: 0,
        height: 0,
        borderLeft: `${halfWidth}px solid transparent`,
        borderRight: `${halfWidth}px solid transparent`,
        borderBottom: `${size}px solid ${BRAND_FOREGROUND}`,
        transform: "translateY(-6%)",
      }}
    />
  );
}

function MetadataAsset({
  imageSrc,
  width,
  height,
}: {
  imageSrc?: string;
  width: string;
  height: string;
}) {
  if (!imageSrc) {
    return <BrandTriangle size={Math.round(Number.parseFloat(width) * 0.75)} />;
  }

  return (
    // `ImageResponse` renders regular HTML/CSS, not `next/image`.
    // eslint-disable-next-line @next/next/no-img-element
    <img
      src={imageSrc}
      alt=""
      style={{
        width,
        height,
        objectFit: "contain",
      }}
    />
  );
}

export function MetadataIconArt({
  imageSrc,
  triangleSize,
}: {
  imageSrc?: string;
  triangleSize: number;
}) {
  return (
    <div
      style={{
        width: "100%",
        height: "100%",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        background: BRAND_GREEN,
      }}
    >
      {imageSrc ? (
        <MetadataAsset imageSrc={imageSrc} width="72%" height="72%" />
      ) : (
        <BrandTriangle size={triangleSize} />
      )}
    </div>
  );
}

export function MetadataPreviewArt({
  title,
  description,
  imageSrc,
}: {
  title: string;
  description: string;
  imageSrc?: string;
}) {
  return (
    <div
      style={{
        width: "100%",
        height: "100%",
        display: "flex",
        alignItems: "center",
        justifyContent: "center",
        background: BRAND_BACKGROUND,
        color: BRAND_FOREGROUND,
        padding: "56px",
      }}
    >
      <div
        style={{
          width: "100%",
          height: "100%",
          display: "flex",
          alignItems: "center",
          gap: "48px",
          borderRadius: "32px",
          padding: "56px",
          background: BRAND_PANEL,
          border: `1px solid ${BRAND_PANEL_BORDER}`,
        }}
      >
        <div
          style={{
            width: "240px",
            height: "240px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            borderRadius: "32px",
            background: BRAND_GREEN,
            border: `1px solid ${BRAND_GREEN}`,
            flexShrink: 0,
            overflow: "hidden",
          }}
        >
          {imageSrc ? (
            <MetadataAsset imageSrc={imageSrc} width="70%" height="70%" />
          ) : (
            <BrandTriangle size={132} />
          )}
        </div>

        <div
          style={{
            display: "flex",
            flexDirection: "column",
            gap: "18px",
            flex: 1,
          }}
        >
          <div
            style={{
              display: "flex",
              fontSize: "26px",
              letterSpacing: "0.28em",
              textTransform: "uppercase",
              color: BRAND_MUTED,
            }}
          >
            Trading Room
          </div>

          <div
            style={{
              display: "flex",
              fontSize: "68px",
              lineHeight: 1.1,
              fontWeight: 700,
              whiteSpace: "pre-wrap",
            }}
          >
            {title}
          </div>

          <div
            style={{
              display: "flex",
              fontSize: "28px",
              lineHeight: 1.45,
              color: BRAND_MUTED,
              whiteSpace: "pre-wrap",
            }}
          >
            {description}
          </div>
        </div>
      </div>
    </div>
  );
}
