/* global React */

// CSDA Brand Mark — recreates the puzzle-piece "C" from the logo as inline SVG.
// Pieces: blue (top-left), cyan (top), green (right), yellow (bottom-right),
//         orange (bottom), red (bottom-left), pink (left)
// Rendered around a center void shaped like "C", with the inner "CSDA" letters.

function CSDAMark({ size = 80, showLetters = true, monochrome = null }) {
  const colors = monochrome
    ? { blue: monochrome, cyan: monochrome, green: monochrome, yellow: monochrome,
        orange: monochrome, red: monochrome, pink: monochrome, navy: monochrome }
    : {
        blue:   "#1F8FD2",
        cyan:   "#1FB7C9",
        green:  "#34B85F",
        yellow: "#F4C518",
        orange: "#F08C2E",
        red:    "#E0382C",
        pink:   "#E94C8A",
        navy:   "#14315B",
      };

  // Build a ring of 7 puzzle-piece sectors, leaving a gap on the right (the "C" opening)
  const cx = 100, cy = 100, R = 92, r = 38;
  // Sector definitions: [startAngleDeg, endAngleDeg, color]
  // C-opening on the right: gap from -22deg to +22deg
  // 7 pieces split across the remaining 316° => ~45° each
  const gap = 22;
  const sectors = [
    { color: colors.cyan,   from: -gap - 45 * 1, to: -gap - 45 * 0 },        // top-right
    { color: colors.blue,   from: -gap - 45 * 2, to: -gap - 45 * 1 },        // top
    { color: colors.purple || "#6E5CB6", from: -gap - 45 * 3, to: -gap - 45 * 2 }, // top-left (we'll re-color)
    { color: colors.pink,   from: -gap - 45 * 4, to: -gap - 45 * 3 },        // left
    { color: colors.red,    from: -gap - 45 * 5, to: -gap - 45 * 4 },        // bottom-left
    { color: colors.orange, from: -gap - 45 * 6, to: -gap - 45 * 5 },        // bottom
    { color: colors.yellow, from: -gap - 45 * 7, to: -gap - 45 * 6 },        // bottom-right
  ];
  // Override with the brand's actual order (blue→cyan→green→yellow→orange→red→pink top to bottom)
  // From original logo: top-left blue, top cyan, top-right green, right (none — gap),
  //   bottom-right yellow, bottom orange, bottom-left red, left pink
  const ringColors = [colors.cyan, colors.blue, colors.pink, colors.red, colors.orange, colors.yellow, colors.green];

  function polar(angleDeg, radius) {
    const a = (angleDeg * Math.PI) / 180;
    return [cx + Math.cos(a) * radius, cy + Math.sin(a) * radius];
  }

  function sectorPath(fromDeg, toDeg) {
    const [x1, y1] = polar(fromDeg, R);
    const [x2, y2] = polar(toDeg, R);
    const [x3, y3] = polar(toDeg, r);
    const [x4, y4] = polar(fromDeg, r);
    const large = Math.abs(toDeg - fromDeg) > 180 ? 1 : 0;
    return `M ${x1} ${y1} A ${R} ${R} 0 ${large} 1 ${x2} ${y2} L ${x3} ${y3} A ${r} ${r} 0 ${large} 0 ${x4} ${y4} Z`;
  }

  // Generate 7 sectors leaving a gap on the right (-gap to +gap)
  const startAngle = gap;          // first sector starts at +22°
  const endAngle = 360 - gap;      // last ends at 360 - 22 = 338°
  const span = endAngle - startAngle; // 316°
  const each = span / 7;

  return (
    <svg viewBox="0 0 200 200" width={size} height={size} aria-label="CSDA logo mark">
      {ringColors.map((color, i) => {
        const from = startAngle + each * i;
        const to = startAngle + each * (i + 1);
        // Add small visual gap between pieces
        return (
          <path key={i} d={sectorPath(from + 1.5, to - 1.5)} fill={color} />
        );
      })}
      {showLetters && (
        <text
          x="100" y="115"
          textAnchor="middle"
          fontFamily='"Cormorant Garamond", serif'
          fontSize="34"
          fontWeight="600"
          fontStyle="italic"
          fill={colors.navy}
          letterSpacing="0.02em"
        >CSDA</text>
      )}
    </svg>
  );
}

// Tiny puzzle-piece icon (single piece — used as section accent)
function CSDAPuzzlePiece({ size = 24, color = "#E0382C" }) {
  return (
    <svg viewBox="0 0 40 40" width={size} height={size}>
      <path
        d="M8 4 H18 a2 2 0 0 1 2 2 v2 a2 2 0 0 0 4 0 V6 a2 2 0 0 1 2 -2 H32 a4 4 0 0 1 4 4 V18 a2 2 0 0 1 -2 2 h-2 a2 2 0 0 0 0 4 h2 a2 2 0 0 1 2 2 V32 a4 4 0 0 1 -4 4 H8 a4 4 0 0 1 -4 -4 V8 a4 4 0 0 1 4 -4 Z"
        fill={color}
      />
    </svg>
  );
}

// Animal silhouettes for the 個性分析 section (虎/ひつじ/こじか/ペガサスなど)
// Hand-tuned silhouettes — recognizable head-on portraits at 56px display size
function CSDAAnimal({ kind = "tiger", size = 64, color = "#14315B" }) {
  const paths = {
    // Tiger — round face, prominent ears, fang stripes
    tiger: (
      <g fill={color}>
        {/* ears */}
        <path d="M14 22 L18 12 L24 18 Z" />
        <path d="M50 22 L46 12 L40 18 Z" />
        {/* face */}
        <path d="M32 18 c-12 0 -19 8 -19 18 c0 11 9 18 19 18 c10 0 19 -7 19 -18 c0 -10 -7 -18 -19 -18 z" />
        {/* stripes (negative space via white) */}
        <path d="M16 28 l4 4 M16 36 l4 4 M48 28 l-4 4 M48 36 l-4 4" stroke="#fff" strokeWidth="1.6" strokeLinecap="round" fill="none" />
        {/* eyes */}
        <ellipse cx="25" cy="32" rx="2" ry="2.4" fill="#fff" />
        <ellipse cx="39" cy="32" rx="2" ry="2.4" fill="#fff" />
        {/* snout */}
        <path d="M28 42 c0 3 2 4 4 4 c2 0 4 -1 4 -4 z" fill="#fff" />
        <ellipse cx="32" cy="40" rx="2" ry="1.4" />
      </g>
    ),
    // Sheep — fluffy cloud-like wool with face front
    sheep: (
      <g fill={color}>
        {/* wool puffs around */}
        <circle cx="16" cy="26" r="6" />
        <circle cx="22" cy="18" r="6" />
        <circle cx="32" cy="14" r="6" />
        <circle cx="42" cy="18" r="6" />
        <circle cx="48" cy="26" r="6" />
        <circle cx="50" cy="36" r="6" />
        <circle cx="14" cy="36" r="6" />
        <circle cx="22" cy="44" r="6" />
        <circle cx="42" cy="44" r="6" />
        {/* face oval */}
        <ellipse cx="32" cy="34" rx="11" ry="13" fill="#fff" stroke={color} strokeWidth="1.5" />
        {/* ears flopping */}
        <ellipse cx="20" cy="32" rx="3.5" ry="5" transform="rotate(-25 20 32)" />
        <ellipse cx="44" cy="32" rx="3.5" ry="5" transform="rotate(25 44 32)" />
        {/* eyes */}
        <circle cx="28" cy="32" r="1.5" />
        <circle cx="36" cy="32" r="1.5" />
        {/* nose */}
        <ellipse cx="32" cy="40" rx="2" ry="1.4" />
      </g>
    ),
    // Deer (こじか) — big head, large round eyes, small antler nubs, white belly
    deer: (
      <g fill={color}>
        {/* antler nubs */}
        <path d="M22 12 l-2 -6 l4 4 z M42 12 l2 -6 l-4 4 z" />
        {/* ears */}
        <ellipse cx="18" cy="20" rx="4" ry="6" transform="rotate(-25 18 20)" />
        <ellipse cx="46" cy="20" rx="4" ry="6" transform="rotate(25 46 20)" />
        {/* body */}
        <path d="M32 22 c-11 0 -17 8 -17 16 c0 9 8 16 17 16 c9 0 17 -7 17 -16 c0 -8 -6 -16 -17 -16 z" />
        {/* white spots */}
        <circle cx="22" cy="40" r="1.5" fill="#fff" />
        <circle cx="42" cy="40" r="1.5" fill="#fff" />
        <circle cx="28" cy="46" r="1.2" fill="#fff" />
        <circle cx="36" cy="46" r="1.2" fill="#fff" />
        {/* eyes */}
        <ellipse cx="25" cy="34" rx="2.4" ry="3" fill="#fff" />
        <ellipse cx="39" cy="34" rx="2.4" ry="3" fill="#fff" />
        <circle cx="25" cy="34" r="1.3" />
        <circle cx="39" cy="34" r="1.3" />
        {/* nose */}
        <ellipse cx="32" cy="42" rx="2" ry="1.4" fill="#fff" />
      </g>
    ),
    // Pegasus — winged horse profile facing right
    pegasus: (
      <g fill={color}>
        {/* wing back */}
        <path d="M18 28 c-4 -8 0 -14 8 -16 c-2 6 -2 12 2 18 z" />
        {/* head + body */}
        <path d="M22 28 c0 -8 6 -14 14 -14 c8 0 14 6 14 14 c0 4 -2 8 -6 10 l0 8 l-6 0 l0 -6 l-8 0 l0 6 l-6 0 l0 -10 c-2 -2 -2 -4 -2 -8 z" />
        {/* mane */}
        <path d="M22 20 c-4 -2 -6 0 -8 4 c4 -2 6 0 8 0 z" />
        {/* horn */}
        <path d="M44 14 l4 -8 l-1 9 z" />
        {/* eye */}
        <circle cx="42" cy="22" r="1.6" fill="#fff" />
        {/* nostril */}
        <circle cx="48" cy="26" r="1" fill="#fff" />
      </g>
    ),
    // Lion — big mane, face center
    lion: (
      <g fill={color}>
        {/* mane scallops */}
        <circle cx="32" cy="14" r="6" />
        <circle cx="20" cy="18" r="6" />
        <circle cx="44" cy="18" r="6" />
        <circle cx="14" cy="28" r="6" />
        <circle cx="50" cy="28" r="6" />
        <circle cx="14" cy="40" r="6" />
        <circle cx="50" cy="40" r="6" />
        <circle cx="20" cy="50" r="6" />
        <circle cx="44" cy="50" r="6" />
        <circle cx="32" cy="52" r="6" />
        {/* face */}
        <circle cx="32" cy="32" r="14" fill="#fff" stroke={color} strokeWidth="1.5" />
        {/* ears */}
        <circle cx="22" cy="22" r="3" />
        <circle cx="42" cy="22" r="3" />
        {/* eyes */}
        <circle cx="27" cy="31" r="1.6" />
        <circle cx="37" cy="31" r="1.6" />
        {/* nose */}
        <path d="M30 36 l4 0 l-2 3 z" />
        {/* whisker dots */}
        <circle cx="26" cy="38" r="0.7" />
        <circle cx="38" cy="38" r="0.7" />
      </g>
    ),
    // Cheetah — slim face, tear marks, ears up
    cheetah: (
      <g fill={color}>
        {/* ears */}
        <path d="M16 14 c-2 4 0 8 4 8 c2 -4 0 -8 -4 -8 z" />
        <path d="M48 14 c2 4 0 8 -4 8 c-2 -4 0 -8 4 -8 z" />
        {/* head — long oval */}
        <ellipse cx="32" cy="36" rx="14" ry="16" />
        {/* spots */}
        <circle cx="22" cy="44" r="1.3" fill="#fff" />
        <circle cx="26" cy="48" r="1.1" fill="#fff" />
        <circle cx="42" cy="44" r="1.3" fill="#fff" />
        <circle cx="38" cy="48" r="1.1" fill="#fff" />
        <circle cx="32" cy="50" r="1.1" fill="#fff" />
        {/* tear marks */}
        <path d="M27 36 l-1 8 M37 36 l1 8" stroke="#fff" strokeWidth="1.4" strokeLinecap="round" />
        {/* eyes */}
        <ellipse cx="26" cy="32" rx="1.6" ry="2" fill="#fff" />
        <ellipse cx="38" cy="32" rx="1.6" ry="2" fill="#fff" />
        {/* nose */}
        <path d="M30 40 l4 0 l-2 2 z" fill="#fff" />
      </g>
    ),
    // Monkey — round head, big ears, face mask
    monkey: (
      <g fill={color}>
        {/* ears */}
        <circle cx="14" cy="32" r="6" />
        <circle cx="50" cy="32" r="6" />
        <circle cx="14" cy="32" r="3" fill="#fff" opacity="0.5" />
        <circle cx="50" cy="32" r="3" fill="#fff" opacity="0.5" />
        {/* head */}
        <circle cx="32" cy="32" r="15" />
        {/* face mask */}
        <path d="M32 44 c-8 0 -10 -6 -10 -10 c0 -4 4 -6 10 -6 c6 0 10 2 10 6 c0 4 -2 10 -10 10 z" fill="#fff" />
        {/* eyes */}
        <circle cx="27" cy="30" r="1.8" />
        <circle cx="37" cy="30" r="1.8" />
        {/* nose dots */}
        <circle cx="30" cy="36" r="0.8" />
        <circle cx="34" cy="36" r="0.8" />
        {/* mouth */}
        <path d="M28 40 q4 3 8 0" stroke={color} strokeWidth="1.4" fill="none" strokeLinecap="round" />
      </g>
    ),
    // Koala — fluffy ears, big nose
    koala: (
      <g fill={color}>
        {/* ears */}
        <circle cx="16" cy="22" r="8" />
        <circle cx="48" cy="22" r="8" />
        <circle cx="16" cy="22" r="4" fill="#fff" opacity="0.4" />
        <circle cx="48" cy="22" r="4" fill="#fff" opacity="0.4" />
        {/* head */}
        <ellipse cx="32" cy="36" rx="16" ry="15" />
        {/* eyes */}
        <circle cx="25" cy="34" r="1.8" fill="#fff" />
        <circle cx="39" cy="34" r="1.8" fill="#fff" />
        {/* big nose — signature feature */}
        <ellipse cx="32" cy="42" rx="5" ry="4" fill="#fff" />
        <ellipse cx="32" cy="42" rx="3.5" ry="2.8" />
      </g>
    ),
  };
  return (
    <svg viewBox="0 0 64 64" width={size} height={size} style={{ display: "block" }}>
      {paths[kind] || paths.tiger}
    </svg>
  );
}

// Arrow icon
function CSDAArrow({ size = 14, color = "currentColor" }) {
  return (
    <svg viewBox="0 0 16 16" width={size} height={size} fill="none">
      <path d="M3 8 H12 M8 4 L12 8 L8 12" stroke={color} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

// Plus / minus for accordions
function CSDAPlus({ open = false, size = 16, color = "currentColor" }) {
  return (
    <svg viewBox="0 0 16 16" width={size} height={size} fill="none" style={{ transition: "transform 240ms" }}>
      <line x1="2" y1="8" x2="14" y2="8" stroke={color} strokeWidth="1.5" strokeLinecap="round" />
      <line x1="8" y1="2" x2="8" y2="14" stroke={color} strokeWidth="1.5" strokeLinecap="round"
            style={{ transformOrigin: "center", transform: open ? "rotate(90deg)" : "rotate(0deg)", transition: "transform 240ms" }} />
    </svg>
  );
}

Object.assign(window, { CSDAMark, CSDAPuzzlePiece, CSDAAnimal, CSDAArrow, CSDAPlus });
