/* global React */
const { useState, useRef, useEffect } = React;

/* =========================================================
   STICKERS — original SVG primitives (no branded mascots)
========================================================= */
window.Star = function Star({ size = 80, fill = '#ff5a3c', stroke = '#0c0c0c' }) {
  const points = [];
  const cx = 50, cy = 50;
  for (let i = 0; i < 16; i++) {
    const a = (i / 16) * Math.PI * 2 - Math.PI / 2;
    const r = i % 2 === 0 ? 48 : 22;
    points.push(`${cx + Math.cos(a) * r},${cy + Math.sin(a) * r}`);
  }
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      <polygon points={points.join(' ')} fill={fill} stroke={stroke} strokeWidth="3" strokeLinejoin="round" />
    </svg>
  );
};

window.Smiley = function Smiley({ size = 64, fill = '#ffd34a', stroke = '#0c0c0c' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      <circle cx="50" cy="50" r="44" fill={fill} stroke={stroke} strokeWidth="4" />
      <circle cx="36" cy="42" r="4" fill={stroke} />
      <circle cx="64" cy="42" r="4" fill={stroke} />
      <path d="M 32 60 Q 50 78 68 60" stroke={stroke} strokeWidth="4" fill="none" strokeLinecap="round" />
    </svg>
  );
};

window.Flower = function Flower({ size = 70, fill = '#5ec6ff', stroke = '#0c0c0c' }) {
  const petals = [];
  for (let i = 0; i < 5; i++) {
    const a = (i / 5) * Math.PI * 2 - Math.PI / 2;
    petals.push(<circle key={i} cx={50 + Math.cos(a) * 20} cy={50 + Math.sin(a) * 20} r="18" fill={fill} stroke={stroke} strokeWidth="3" />);
  }
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      {petals}
      <circle cx="50" cy="50" r="11" fill="#ffd34a" stroke={stroke} strokeWidth="3" />
    </svg>
  );
};

window.Butterfly = function Butterfly({ size = 78, fill = '#ff8fb0', stroke = '#0c0c0c' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      <path d="M50 50 Q 20 18 12 38 Q 6 60 30 64 Q 44 64 50 50 Z" fill={fill} stroke={stroke} strokeWidth="3" strokeLinejoin="round" />
      <path d="M50 50 Q 80 18 88 38 Q 94 60 70 64 Q 56 64 50 50 Z" fill={fill} stroke={stroke} strokeWidth="3" strokeLinejoin="round" />
      <path d="M50 50 Q 24 64 22 84 Q 38 76 50 60 Z" fill={fill} stroke={stroke} strokeWidth="3" strokeLinejoin="round" />
      <path d="M50 50 Q 76 64 78 84 Q 62 76 50 60 Z" fill={fill} stroke={stroke} strokeWidth="3" strokeLinejoin="round" />
      <line x1="50" y1="30" x2="50" y2="74" stroke={stroke} strokeWidth="4" strokeLinecap="round" />
      <circle cx="50" cy="28" r="3" fill={stroke} />
    </svg>
  );
};

window.Heart = function Heart({ size = 60, fill = '#ff5a3c', stroke = '#0c0c0c' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      <path d="M50 86 C -8 48 16 6 50 36 C 84 6 108 48 50 86 Z" fill={fill} stroke={stroke} strokeWidth="4" strokeLinejoin="round" />
    </svg>
  );
};

window.Squiggle = function Squiggle({ width = 120, color = '#ff5a3c' }) {
  return (
    <svg width={width} height="22" viewBox="0 0 120 22">
      <path d="M 2 11 Q 12 1 22 11 T 42 11 T 62 11 T 82 11 T 102 11 T 118 11" stroke={color} strokeWidth="3" fill="none" strokeLinecap="round" />
    </svg>
  );
};

window.Arrow = function Arrow({ width = 110, color = '#0c0c0c' }) {
  return (
    <svg width={width} height="40" viewBox="0 0 110 40">
      <path d="M 6 32 Q 30 4 60 18 T 102 14" stroke={color} strokeWidth="3" fill="none" strokeLinecap="round" />
      <path d="M 102 14 L 92 6 M 102 14 L 92 22" stroke={color} strokeWidth="3" fill="none" strokeLinecap="round" />
    </svg>
  );
};

window.Burst = function Burst({ size = 110, fill = '#ff5a3c', stroke = '#0c0c0c', spikes = 14 }) {
  const points = [];
  const cx = 50, cy = 50;
  for (let i = 0; i < spikes * 2; i++) {
    const a = (i / (spikes * 2)) * Math.PI * 2 - Math.PI / 2;
    const r = i % 2 === 0 ? 46 : 28;
    points.push(`${cx + Math.cos(a) * r},${cy + Math.sin(a) * r}`);
  }
  return (
    <svg width={size} height={size} viewBox="0 0 100 100">
      <polygon points={points.join(' ')} fill={fill} stroke={stroke} strokeWidth="2.5" strokeLinejoin="round" />
    </svg>
  );
};

window.PaperClip = function PaperClip({ size = 70, color = '#0c0c0c' }) {
  return (
    <svg width={size} height={size * 1.6} viewBox="0 0 50 80">
      <path d="M 28 8 Q 42 8 42 22 L 42 60 Q 42 72 30 72 Q 18 72 18 60 L 18 30 Q 18 22 26 22 Q 34 22 34 30 L 34 56" stroke={color} strokeWidth="4" fill="none" strokeLinecap="round" />
    </svg>
  );
};

/* Photo silhouette for the ID card — not anyone in particular */
window.PortraitGlyph = function PortraitGlyph() {
  return (
    <svg viewBox="0 0 200 250" preserveAspectRatio="xMidYMid slice">
      <defs>
        <linearGradient id="bg" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#5ec6ff" />
          <stop offset="1" stopColor="#b07cff" />
        </linearGradient>
        <pattern id="dots" width="6" height="6" patternUnits="userSpaceOnUse">
          <circle cx="3" cy="3" r="0.8" fill="rgba(0,0,0,.18)" />
        </pattern>
      </defs>
      <rect width="200" height="250" fill="url(#bg)" />
      <rect width="200" height="250" fill="url(#dots)" />
      {/* Abstract figure: shoulders + head silhouette */}
      <ellipse cx="100" cy="220" rx="92" ry="48" fill="#0c0c0c" opacity="0.85" />
      <circle cx="100" cy="120" r="52" fill="#0c0c0c" opacity="0.9" />
      <circle cx="100" cy="120" r="42" fill="#1c1c1c" />
      {/* Hair tuft */}
      <path d="M 60 100 Q 70 60 100 60 Q 130 60 140 95 Q 130 80 100 82 Q 80 84 60 100 Z" fill="#0c0c0c" />
      {/* Face highlight */}
      <ellipse cx="86" cy="118" rx="3" ry="5" fill="#f2ead8" opacity=".5" />
      <ellipse cx="114" cy="118" rx="3" ry="5" fill="#f2ead8" opacity=".5" />
      {/* Frame caption bar */}
      <rect x="0" y="232" width="200" height="18" fill="rgba(0,0,0,.55)" />
    </svg>
  );
};

/* Project thumbnails (abstract, original) */
window.ProjThumb = function ProjThumb({ kind, palette }) {
  const [a, b, c] = palette;
  if (kind === 'queue') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        {[0,1,2,3,4].map(i => (
          <rect key={i} x={20} y={28 + i*30} width={160 - i*8} height={20} fill={b} stroke="#0c0c0c" strokeWidth="2.5" />
        ))}
        <circle cx="172" cy="38" r="10" fill={c} stroke="#0c0c0c" strokeWidth="2.5" />
      </svg>
    );
  }
  if (kind === 'resume') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        <rect x="38" y="22" width="124" height="156" fill="#fbf6e9" stroke="#0c0c0c" strokeWidth="3" />
        <rect x="50" y="36" width="68" height="10" fill="#0c0c0c" />
        <rect x="50" y="52" width="100" height="3" fill="#0c0c0c" opacity=".4" />
        <rect x="50" y="60" width="92" height="3" fill="#0c0c0c" opacity=".4" />
        <rect x="50" y="76" width="50" height="3" fill="#0c0c0c" opacity=".4" />
        <rect x="50" y="84" width="80" height="3" fill="#0c0c0c" opacity=".4" />
        <circle cx="156" cy="160" r="22" fill={b} stroke="#0c0c0c" strokeWidth="3" />
        <text x="156" y="166" textAnchor="middle" fontFamily="Archivo Black" fontSize="14" fill="#0c0c0c">A+</text>
      </svg>
    );
  }
  if (kind === 'mac') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        <rect x="20" y="40" width="160" height="120" rx="8" fill="#fbf6e9" stroke="#0c0c0c" strokeWidth="3" />
        <rect x="20" y="40" width="160" height="20" rx="8" fill="#0c0c0c" />
        <circle cx="32" cy="50" r="3" fill="#ff5a3c" />
        <circle cx="44" cy="50" r="3" fill="#ffd34a" />
        <circle cx="56" cy="50" r="3" fill="#d8ff3a" />
        <rect x="34" y="74" width="100" height="6" fill={b} />
        <rect x="34" y="88" width="80" height="3" fill="#0c0c0c" opacity=".4" />
        <rect x="34" y="96" width="120" height="3" fill="#0c0c0c" opacity=".4" />
        <rect x="34" y="118" width="60" height="20" fill={c} stroke="#0c0c0c" strokeWidth="2" />
      </svg>
    );
  }
  if (kind === 'browser') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        <rect x="14" y="32" width="172" height="136" fill="#fbf6e9" stroke="#0c0c0c" strokeWidth="3" />
        <rect x="14" y="32" width="172" height="22" fill={b} stroke="#0c0c0c" strokeWidth="3" />
        <rect x="40" y="38" width="80" height="10" fill="#fbf6e9" stroke="#0c0c0c" strokeWidth="2" />
        <rect x="30" y="64" width="44" height="44" fill={c} stroke="#0c0c0c" strokeWidth="3" />
        <rect x="82" y="64" width="44" height="44" fill="#0c0c0c" />
        <rect x="134" y="64" width="44" height="44" fill={b} stroke="#0c0c0c" strokeWidth="3" />
      </svg>
    );
  }
  if (kind === 'ml') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        {[0,1,2].map(col => [0,1,2,3].map(row => (
          <circle key={`${col}-${row}`} cx={50 + col*50} cy={40 + row*40} r="14" fill={col === 1 ? c : b} stroke="#0c0c0c" strokeWidth="2.5" />
        )))}
        {[0,1,2].map(col => [0,1,2].map(row => (
          <line key={`l-${col}-${row}`} x1={50 + col*50} y1={40 + row*40 + 14} x2={50 + ((col+1) % 3)*50} y2={40 + (row+1)*40 - 14} stroke="#0c0c0c" strokeWidth="1.5" opacity=".6" />
        )))}
      </svg>
    );
  }
  if (kind === 'menubar') {
    return (
      <svg viewBox="0 0 200 200">
        <rect width="200" height="200" fill={a} />
        <rect x="0" y="20" width="200" height="22" fill="#0c0c0c" />
        <text x="14" y="35" fontFamily="JetBrains Mono" fontSize="11" fill="#fbf6e9">  File  Edit  View</text>
        <rect x="160" y="46" width="34" height="44" fill={b} stroke="#0c0c0c" strokeWidth="2.5" />
        <text x="177" y="68" textAnchor="middle" fontFamily="Archivo Black" fontSize="9" fill="#0c0c0c">PIX</text>
        <rect x="40" y="100" width="120" height="80" fill="#fbf6e9" stroke="#0c0c0c" strokeWidth="3" />
        <rect x="50" y="112" width="100" height="3" fill="#0c0c0c" opacity=".5" />
        <rect x="50" y="120" width="80" height="3" fill="#0c0c0c" opacity=".5" />
        <rect x="50" y="128" width="60" height="3" fill="#0c0c0c" opacity=".5" />
      </svg>
    );
  }
  // default: abstract code
  return (
    <svg viewBox="0 0 200 200">
      <rect width="200" height="200" fill={a} />
      <text x="100" y="115" textAnchor="middle" fontFamily="JetBrains Mono" fontWeight="700" fontSize="48" fill="#0c0c0c">{`{}`}</text>
    </svg>
  );
};

/* =========================================================
   STICKER — draggable wrapper
========================================================= */
window.Sticker = function Sticker({ children, top, left, right, bottom, rotate = 0, dragId }) {
  const [pos, setPos] = useState({ x: 0, y: 0 });
  const [drag, setDrag] = useState(null);
  const onDown = (e) => {
    e.preventDefault();
    const start = { x: e.clientX, y: e.clientY };
    setDrag(start);
  };
  useEffect(() => {
    if (!drag) return;
    const move = (e) => setPos({ x: e.clientX - drag.x, y: e.clientY - drag.y });
    const up = () => setDrag(null);
    window.addEventListener('mousemove', move);
    window.addEventListener('mouseup', up);
    return () => { window.removeEventListener('mousemove', move); window.removeEventListener('mouseup', up); };
  }, [drag]);

  const style = {
    top, left, right, bottom,
    transform: `translate(${pos.x}px, ${pos.y}px) rotate(${rotate}deg)`,
    '--rot': `${rotate}deg`,
  };
  return (
    <div className="sticker" style={style} onMouseDown={onDown}>{children}</div>
  );
};
