// Jai Mahal Audio Tour — Choose-a-chapter overlay
// L-shaped diagram with numbered medallions (1,2,3,4 horizontal · 5 on the cut)
// + a clean list of full chapter titles below.

const { useState, useEffect } = React;

// Coordinates in a 360 × 380 viewBox.
const NODES = [
  { id: 1, x: 60,  y: 120 },
  { id: 2, x: 140, y: 120 },
  { id: 3, x: 220, y: 120 },
  { id: 4, x: 300, y: 120 },  // corner
  { id: 5, x: 300, y: 300 },  // bottom of the cut
];

// L-shape path — starts at the entrance dot, runs through all four top
// medallions, turns the corner, and drops to medallion 5.
const PATH_D = "M 20 120 L 300 120 L 300 320";

function LMap({ open, onClose, onPick }) {
  const [hover, setHover] = useState(null);

  // Lock body scroll when open
  useEffect(() => {
    if (!open) return;
    const prev = document.body.style.overflow;
    document.body.style.overflow = "hidden";
    return () => { document.body.style.overflow = prev; };
  }, [open]);

  if (!open) return null;

  const T = window.TOUR;

  return (
    <div
      style={{
        position: "fixed",
        inset: 0,
        zIndex: 60,
        background: "var(--cream)",
        display: "flex",
        flexDirection: "column",
        animation: "sheetUp .4s cubic-bezier(.2,.7,.2,1)",
      }}
    >
      {/* Header */}
      <div
        style={{
          padding: "14px 18px",
          display: "flex",
          alignItems: "center",
          justifyContent: "space-between",
          borderBottom: "1px solid var(--border)",
          flexShrink: 0,
        }}
      >
        <button
          onClick={onClose}
          aria-label="Back"
          style={{
            background: "none",
            border: "none",
            cursor: "pointer",
            color: "var(--ink-soft)",
            display: "flex",
            alignItems: "center",
            gap: 6,
            padding: 6,
            margin: -6,
            fontFamily: "var(--font-sans)",
            fontSize: 11,
            letterSpacing: 1.5,
            textTransform: "uppercase",
          }}
        >
          <Icon.ArrowLeft size={14} />
          Back
        </button>
        <div
          style={{
            fontFamily: "var(--font-sans)",
            fontSize: 10,
            letterSpacing: 3,
            color: "var(--gold-dark)",
            textTransform: "uppercase",
          }}
        >
          Choose a chapter
        </div>
        <div style={{ width: 56 }} />
      </div>

      {/* Scrollable body so labels are never clipped on small phones */}
      <div
        style={{
          flex: 1,
          overflowY: "auto",
          padding: "14px 14px 22px",
        }}
      >
        {/* Map */}
        <div
          style={{
            position: "relative",
            background: "var(--cream-2)",
            border: "1px solid var(--border)",
            aspectRatio: "360 / 380",
            width: "100%",
            maxWidth: 420,
            margin: "0 auto",
          }}
        >
          {/* Corner diamond ornaments */}
          {[
            { top: 6, left: 6 }, { top: 6, right: 6 },
            { bottom: 6, left: 6 }, { bottom: 6, right: 6 },
          ].map((p, i) => (
            <div
              key={i}
              style={{
                position: "absolute", ...p,
                width: 5, height: 5,
                background: "var(--gold-dark)",
                transform: "rotate(45deg)",
                opacity: 0.55,
              }}
            />
          ))}

          {/* Plan label */}
          <div
            style={{
              position: "absolute",
              top: 14,
              left: 0,
              right: 0,
              textAlign: "center",
              fontFamily: "var(--font-sans)",
              fontSize: 9,
              letterSpacing: 3,
              color: "var(--gold-dark)",
              textTransform: "uppercase",
              opacity: 0.75,
              pointerEvents: "none",
            }}
          >
            ✦ &nbsp; Tour Plan · The L-Shaped Alley &nbsp; ✦
          </div>

          {/* SVG diagram */}
          <svg
            viewBox="0 0 360 380"
            preserveAspectRatio="xMidYMid meet"
            style={{
              position: "absolute",
              inset: 0,
              width: "100%",
              height: "100%",
              display: "block",
            }}
          >
            <defs>
              <pattern id="dotGrid" x="0" y="0" width="14" height="14" patternUnits="userSpaceOnUse">
                <circle cx="1" cy="1" r="0.5" fill="var(--ink-faint-2)" opacity="0.4" />
              </pattern>
            </defs>

            {/* Faint dot grid background */}
            <rect x="10" y="46" width="340" height="324" fill="url(#dotGrid)" opacity="0.5" />

            {/* "Start" entrance dot */}
            <g>
              <circle cx="20" cy="120" r="4" fill="var(--gold-dark)" />
              <text
                x="20" y="104"
                textAnchor="middle"
                fontFamily="var(--font-sans)"
                fontSize="7.5"
                letterSpacing="2"
                fill="var(--gold-dark)"
                style={{ textTransform: "uppercase" }}
              >
                Start
              </text>
            </g>

            {/* L-path */}
            <path
              d={PATH_D}
              fill="none"
              stroke="var(--gold-dark)"
              strokeWidth="1.1"
              strokeDasharray="3 5"
              strokeLinecap="round"
              opacity="0.85"
            />
            <path
              d={PATH_D}
              fill="none"
              stroke="var(--gold-dark)"
              strokeWidth="0.6"
              opacity="0.22"
            />

            {/* End arrow */}
            <g transform="translate(300,326)">
              <path d="M -4,-4 L 0,4 L 4,-4 Z" fill="var(--gold-dark)" opacity="0.85" />
              <text
                x="0" y="22"
                textAnchor="middle"
                fontFamily="var(--font-serif)"
                fontStyle="italic"
                fontSize="11"
                fill="var(--ink-soft)"
              >
                to closing
              </text>
            </g>

            {/* Compass top-right */}
            <g transform="translate(338, 60)" opacity="0.4">
              <circle r="9" fill="none" stroke="var(--gold-dark)" strokeWidth="0.5" />
              <line x1="0" y1="-9" x2="0" y2="9" stroke="var(--gold-dark)" strokeWidth="0.5" />
              <line x1="-9" y1="0" x2="9" y2="0" stroke="var(--gold-dark)" strokeWidth="0.5" />
              <text x="0" y="-12" textAnchor="middle" fontFamily="var(--font-sans)" fontSize="7" fill="var(--gold-dark)">N</text>
            </g>

            {/* Medallions */}
            {NODES.map((n) => {
              const isHover = hover === n.id;
              return (
                <g
                  key={n.id}
                  style={{ cursor: "pointer" }}
                  onMouseEnter={() => setHover(n.id)}
                  onMouseLeave={() => setHover(null)}
                  onClick={() => onPick(n.id)}
                >
                  {/* Pulse ring */}
                  <circle cx={n.x} cy={n.y} r="16" fill="var(--gold-dark)" opacity="0.18">
                    <animate
                      attributeName="r"
                      values="16;26;16"
                      dur={`${2.4 + n.id * 0.15}s`}
                      repeatCount="indefinite"
                      begin={`${n.id * 0.25}s`}
                    />
                    <animate
                      attributeName="opacity"
                      values="0.3;0;0.3"
                      dur={`${2.4 + n.id * 0.15}s`}
                      repeatCount="indefinite"
                      begin={`${n.id * 0.25}s`}
                    />
                  </circle>
                  {/* Outer ring */}
                  <circle
                    cx={n.x} cy={n.y}
                    r={isHover ? 20 : 17}
                    fill="var(--cream)"
                    stroke="var(--gold-dark)"
                    strokeWidth={isHover ? 1.4 : 1}
                    style={{ transition: "r .2s, stroke-width .2s" }}
                  />
                  {/* Inner */}
                  <circle
                    cx={n.x} cy={n.y}
                    r={isHover ? 14 : 12}
                    fill={isHover ? "var(--gold-dark)" : "var(--cream-2)"}
                    style={{ transition: "r .2s, fill .2s" }}
                  />
                  {/* Roman numeral */}
                  <text
                    x={n.x} y={n.y + 4.5}
                    textAnchor="middle"
                    fontFamily="var(--font-serif)"
                    fontStyle="italic"
                    fontSize="14"
                    fontWeight="400"
                    fill={isHover ? "var(--cream)" : "var(--gold-dark)"}
                    style={{ transition: "fill .2s" }}
                  >
                    {["I", "II", "III", "IV", "V"][n.id - 1]}
                  </text>

                  {/* Larger hit target */}
                  <circle cx={n.x} cy={n.y} r="28" fill="transparent" />
                </g>
              );
            })}
          </svg>
        </div>

        {/* Chapter list */}
        <ol style={{ listStyle: "none", padding: "20px 4px 0", margin: 0 }}>
          {T.chapters.map((c) => (
            <li key={c.id}>
              <button
                onClick={() => onPick(c.id)}
                onMouseEnter={() => setHover(c.id)}
                onMouseLeave={() => setHover(null)}
                style={{
                  width: "100%",
                  display: "flex",
                  alignItems: "center",
                  gap: 16,
                  padding: "13px 6px",
                  background: hover === c.id ? "var(--cream-2)" : "transparent",
                  border: "none",
                  borderTop: "1px solid var(--border)",
                  cursor: "pointer",
                  textAlign: "left",
                  color: "var(--ink)",
                  transition: "background .15s",
                }}
              >
                <span
                  style={{
                    flex: "0 0 36px",
                    height: 36,
                    border: "1px solid var(--gold-dark)",
                    display: "flex",
                    alignItems: "center",
                    justifyContent: "center",
                    fontFamily: "var(--font-serif)",
                    fontStyle: "italic",
                    fontSize: 16,
                    color: "var(--gold-dark)",
                    background: hover === c.id ? "var(--gold-dark)" : "var(--cream)",
                    transition: "background .15s, color .15s",
                    ...(hover === c.id ? { color: "white" } : {}),
                  }}
                >
                  {["I", "II", "III", "IV", "V"][c.id - 1]}
                </span>
                <span style={{ flex: 1, minWidth: 0 }}>
                  <span
                    style={{
                      display: "block",
                      fontFamily: "var(--font-sans)",
                      fontSize: 9,
                      letterSpacing: 2.2,
                      textTransform: "uppercase",
                      color: "var(--gold-dark)",
                      marginBottom: 2,
                    }}
                  >
                    Chapter {String(c.id).padStart(2, "0")} · {c.duration}
                  </span>
                  <span
                    style={{
                      display: "block",
                      fontFamily: "var(--font-serif)",
                      fontStyle: "italic",
                      fontSize: 16,
                      lineHeight: 1.2,
                      color: "var(--ink)",
                      textWrap: "balance",
                    }}
                  >
                    {c.title}
                  </span>
                </span>
                <Icon.ArrowRight size={14} />
              </button>
            </li>
          ))}
          <li style={{ borderTop: "1px solid var(--border)" }} />
        </ol>
      </div>
    </div>
  );
}

window.LMap = LMap;
