// Three pig mascot variants, all composed of geometric primitives.
// Props: { size, pose, accent, skin }
// pose: 'wave' | 'surf' | 'chill'
// Each returns a self-contained SVG. No external assets.

const PigFlat = ({ size = 220, pose = "wave", wave = 0 }) => {
  // Flat vector cartoon — clean shapes, thick outline, shades + lei
  const skin = "#FFC2B4";
  const skinShade = "#F59B88";
  const out = "#2E2A2A";
  const stroke = 3.2;
  const waveAngle = Math.sin(wave) * 14; // degrees
  return (
    <svg viewBox="0 0 260 260" width={size} height={size} style={{ display: "block", overflow: "visible" }}>
      {/* board behind */}
      {pose === "surf" && (
        <g transform="translate(40 170) rotate(-10)">
          <rect x="0" y="0" width="180" height="28" rx="14" fill="#F7D27A" stroke={out} strokeWidth={stroke} />
          <rect x="82" y="4" width="16" height="20" rx="4" fill="#E07A4F" />
          <circle cx="170" cy="14" r="5" fill={out} />
        </g>
      )}
      {/* body */}
      <ellipse cx="130" cy="160" rx="78" ry="62" fill={skin} stroke={out} strokeWidth={stroke} />
      <ellipse cx="130" cy="175" rx="58" ry="36" fill={skinShade} opacity="0.35" />
      {/* legs */}
      <rect x="80" y="200" width="22" height="32" rx="10" fill={skin} stroke={out} strokeWidth={stroke} />
      <rect x="158" y="200" width="22" height="32" rx="10" fill={skin} stroke={out} strokeWidth={stroke} />
      {/* tail curl */}
      <path d="M205 150 q 20 -4 16 14 q -4 16 -18 6" fill="none" stroke={out} strokeWidth={stroke} strokeLinecap="round" />
      {/* head */}
      <g style={{ transformOrigin: "130px 100px", transform: `rotate(${Math.sin(wave * 1.3) * 2}deg)` }}>
        <ellipse cx="130" cy="100" rx="64" ry="56" fill={skin} stroke={out} strokeWidth={stroke} />
        {/* ears */}
        <path d="M78 66 q 4 -30 34 -18 q 6 18 -6 30 z" fill={skin} stroke={out} strokeWidth={stroke} />
        <path d="M182 66 q -4 -30 -34 -18 q -6 18 6 30 z" fill={skin} stroke={out} strokeWidth={stroke} />
        <path d="M86 62 q 8 -18 22 -12" fill="none" stroke={skinShade} strokeWidth="2.5" strokeLinecap="round" />
        {/* snout */}
        <ellipse cx="130" cy="118" rx="30" ry="22" fill={skinShade} stroke={out} strokeWidth={stroke} />
        <ellipse cx="122" cy="120" rx="3.4" ry="5" fill={out} />
        <ellipse cx="138" cy="120" rx="3.4" ry="5" fill={out} />
        {/* sunglasses */}
        <g>
          <rect x="88" y="84" width="36" height="18" rx="6" fill={out} />
          <rect x="136" y="84" width="36" height="18" rx="6" fill={out} />
          <rect x="124" y="91" width="12" height="4" fill={out} />
          <rect x="92" y="88" width="14" height="6" rx="2" fill="#6FD2D7" opacity="0.7" />
          <rect x="140" y="88" width="14" height="6" rx="2" fill="#6FD2D7" opacity="0.7" />
        </g>
        {/* smile */}
        <path d="M118 132 q 12 10 24 0" fill="none" stroke={out} strokeWidth={stroke} strokeLinecap="round" />
        {/* lei */}
        <g>
          {[...Array(9)].map((_, i) => {
            const t = i / 8;
            const x = 78 + t * 104;
            const y = 146 + Math.sin(t * Math.PI) * -18 + 8;
            const colors = ["#FF6B4A", "#FFD36B", "#FF9EC0", "#6FD2D7", "#7BC96F"];
            return <circle key={i} cx={x} cy={y} r="7" fill={colors[i % colors.length]} stroke={out} strokeWidth="2" />;
          })}
        </g>
      </g>
      {/* waving arm */}
      {pose === "wave" && (
        <g style={{ transformOrigin: "185px 140px", transform: `rotate(${-10 + waveAngle}deg)` }}>
          <rect x="176" y="112" width="26" height="52" rx="12" fill={skin} stroke={out} strokeWidth={stroke} />
          <circle cx="200" cy="112" r="14" fill={skin} stroke={out} strokeWidth={stroke} />
        </g>
      )}
      {/* chill — shaka hand */}
      {pose === "chill" && (
        <g>
          <rect x="176" y="150" width="22" height="42" rx="10" fill={skin} stroke={out} strokeWidth={stroke} />
        </g>
      )}
    </svg>
  );
};

const PigClay = ({ size = 220, pose = "wave", wave = 0 }) => {
  // Chunky 3D-clay — softer gradients, no hard outline, rounded everything
  const waveAngle = Math.sin(wave) * 12;
  return (
    <svg viewBox="0 0 260 260" width={size} height={size} style={{ display: "block", overflow: "visible" }}>
      <defs>
        <radialGradient id="clayBody" cx="40%" cy="30%">
          <stop offset="0%" stopColor="#FFD9CC" />
          <stop offset="60%" stopColor="#F5A089" />
          <stop offset="100%" stopColor="#D77A64" />
        </radialGradient>
        <radialGradient id="claySnout" cx="40%" cy="30%">
          <stop offset="0%" stopColor="#FFB9A5" />
          <stop offset="100%" stopColor="#D77A64" />
        </radialGradient>
        <radialGradient id="clayBoard" cx="30%" cy="30%">
          <stop offset="0%" stopColor="#FFE8A8" />
          <stop offset="100%" stopColor="#E0A64A" />
        </radialGradient>
      </defs>
      {pose === "surf" && (
        <g transform="translate(40 175) rotate(-8)">
          <rect x="0" y="0" width="180" height="30" rx="15" fill="url(#clayBoard)" />
          <path d="M20 6 Q 90 2 160 6" stroke="#FFF" strokeWidth="2" opacity="0.5" fill="none" />
        </g>
      )}
      {/* body */}
      <ellipse cx="130" cy="162" rx="80" ry="64" fill="url(#clayBody)" />
      {/* legs */}
      <ellipse cx="92" cy="212" rx="13" ry="18" fill="#D77A64" />
      <ellipse cx="168" cy="212" rx="13" ry="18" fill="#D77A64" />
      {/* tail */}
      <path d="M206 150 q 24 0 18 18 q -6 16 -20 6" fill="none" stroke="#D77A64" strokeWidth="10" strokeLinecap="round" />
      {/* head */}
      <g style={{ transformOrigin: "130px 100px", transform: `rotate(${Math.sin(wave * 1.3) * 2}deg)` }}>
        <ellipse cx="130" cy="100" rx="66" ry="58" fill="url(#clayBody)" />
        {/* ears */}
        <ellipse cx="88" cy="60" rx="18" ry="24" fill="#E08A74" transform="rotate(-25 88 60)" />
        <ellipse cx="172" cy="60" rx="18" ry="24" fill="#E08A74" transform="rotate(25 172 60)" />
        {/* snout */}
        <ellipse cx="130" cy="122" rx="34" ry="24" fill="url(#claySnout)" />
        <ellipse cx="121" cy="122" rx="4" ry="6" fill="#5A2E22" />
        <ellipse cx="139" cy="122" rx="4" ry="6" fill="#5A2E22" />
        {/* highlight */}
        <ellipse cx="112" cy="80" rx="18" ry="12" fill="#FFF" opacity="0.25" />
        {/* shades */}
        <g>
          <rect x="86" y="82" width="40" height="22" rx="10" fill="#1A1A1A" />
          <rect x="134" y="82" width="40" height="22" rx="10" fill="#1A1A1A" />
          <rect x="122" y="90" width="14" height="5" fill="#1A1A1A" />
          <ellipse cx="100" cy="88" rx="6" ry="3" fill="#FFDC8A" opacity="0.6" />
          <ellipse cx="148" cy="88" rx="6" ry="3" fill="#FFDC8A" opacity="0.6" />
        </g>
        {/* smile */}
        <path d="M118 138 q 12 10 24 0" fill="none" stroke="#5A2E22" strokeWidth="3.5" strokeLinecap="round" />
        {/* lei */}
        <g>
          {[...Array(10)].map((_, i) => {
            const t = i / 9;
            const x = 76 + t * 108;
            const y = 146 + Math.sin(t * Math.PI) * -16 + 8;
            const colors = ["#FF6B4A", "#FFC26B", "#FF9EC0", "#6FD2D7"];
            return <circle key={i} cx={x} cy={y} r="8" fill={colors[i % colors.length]} />;
          })}
        </g>
      </g>
      {pose === "wave" && (
        <g style={{ transformOrigin: "190px 140px", transform: `rotate(${-8 + waveAngle}deg)` }}>
          <ellipse cx="190" cy="140" rx="16" ry="28" fill="url(#clayBody)" />
          <circle cx="200" cy="112" r="15" fill="url(#clayBody)" />
        </g>
      )}
    </svg>
  );
};

const PigSticker = ({ size = 220, pose = "wave", wave = 0 }) => {
  // Sticker/emoji style — thick white stroke around whole piece
  const waveAngle = Math.sin(wave) * 14;
  return (
    <svg viewBox="0 0 260 260" width={size} height={size} style={{ display: "block", overflow: "visible", filter: "drop-shadow(0 4px 0 rgba(0,0,0,0.15))" }}>
      <defs>
        <filter id="stickerOutline" x="-20%" y="-20%" width="140%" height="140%">
          <feMorphology in="SourceGraphic" operator="dilate" radius="4" result="out" />
          <feFlood floodColor="#FFFFFF" />
          <feComposite in2="out" operator="in" />
          <feComposite in="SourceGraphic" operator="over" />
        </filter>
      </defs>
      <g filter="url(#stickerOutline)">
        {pose === "surf" && (
          <g transform="translate(40 172) rotate(-8)">
            <rect x="0" y="0" width="180" height="28" rx="14" fill="#5FB7E0" />
            <rect x="0" y="0" width="180" height="10" rx="5" fill="#A9E0F0" />
            <circle cx="92" cy="14" r="5" fill="#FF6B4A" />
          </g>
        )}
        <ellipse cx="130" cy="162" rx="78" ry="62" fill="#FFB3A0" />
        <rect x="80" y="202" width="22" height="32" rx="10" fill="#FFB3A0" />
        <rect x="158" y="202" width="22" height="32" rx="10" fill="#FFB3A0" />
        <path d="M205 150 q 20 -4 16 14 q -4 16 -18 6" fill="none" stroke="#E88873" strokeWidth="7" strokeLinecap="round" />
        <g style={{ transformOrigin: "130px 100px", transform: `rotate(${Math.sin(wave * 1.3) * 2}deg)` }}>
          <ellipse cx="130" cy="100" rx="64" ry="56" fill="#FFB3A0" />
          <path d="M78 66 q 4 -30 34 -18 q 6 18 -6 30 z" fill="#FFB3A0" />
          <path d="M182 66 q -4 -30 -34 -18 q -6 18 6 30 z" fill="#FFB3A0" />
          <ellipse cx="130" cy="120" rx="32" ry="24" fill="#F58A72" />
          <ellipse cx="121" cy="120" rx="4" ry="6" fill="#2E1A14" />
          <ellipse cx="139" cy="120" rx="4" ry="6" fill="#2E1A14" />
          {/* Big star shades */}
          <g>
            <rect x="86" y="82" width="40" height="22" rx="4" fill="#2E1A14" />
            <rect x="134" y="82" width="40" height="22" rx="4" fill="#2E1A14" />
            <rect x="122" y="90" width="14" height="4" fill="#2E1A14" />
            <path d="M96 88 l2 -4 l2 4 l4 0 l-3 3 l1 4 l-4 -2 l-4 2 l1 -4 l-3 -3 z" fill="#FFD36B" />
            <path d="M148 88 l2 -4 l2 4 l4 0 l-3 3 l1 4 l-4 -2 l-4 2 l1 -4 l-3 -3 z" fill="#FFD36B" />
          </g>
          {/* tongue-out grin */}
          <path d="M118 134 q 12 14 24 0" fill="none" stroke="#2E1A14" strokeWidth="4" strokeLinecap="round" />
          <ellipse cx="138" cy="142" rx="5" ry="4" fill="#FF7A8A" />
          {/* hibiscus on ear */}
          <g transform="translate(180 52)">
            {[...Array(5)].map((_, i) => (
              <ellipse key={i} cx="0" cy="-10" rx="8" ry="12" fill="#FF3D6E" transform={`rotate(${i * 72})`} />
            ))}
            <circle r="4" fill="#FFD36B" />
          </g>
        </g>
        {pose === "wave" && (
          <g style={{ transformOrigin: "185px 140px", transform: `rotate(${-10 + waveAngle}deg)` }}>
            <rect x="176" y="112" width="26" height="52" rx="12" fill="#FFB3A0" />
            <circle cx="200" cy="112" r="14" fill="#FFB3A0" />
          </g>
        )}
      </g>
    </svg>
  );
};

const Pig = ({ variant = "flat", ...props }) => {
  const time = React.useRef(0);
  const [, force] = React.useState(0);
  React.useEffect(() => {
    let raf;
    const loop = (t) => {
      time.current = t / 400;
      force((n) => (n + 1) % 1000000);
      raf = requestAnimationFrame(loop);
    };
    raf = requestAnimationFrame(loop);
    return () => cancelAnimationFrame(raf);
  }, []);
  const wave = time.current;
  if (variant === "clay") return <PigClay wave={wave} {...props} />;
  if (variant === "sticker") return <PigSticker wave={wave} {...props} />;
  if (variant === "pixel" && window.PigPixel) return <window.PigPixel wave={wave} {...props} />;
  if (variant === "line" && window.PigLine) return <window.PigLine wave={wave} {...props} />;
  if (variant === "retro" && window.PigRetro) return <window.PigRetro wave={wave} {...props} />;
  return <PigFlat wave={wave} {...props} />;
};

Object.assign(window, { Pig, PigFlat, PigClay, PigSticker });
