/* ============================================================
   SKI NEW ENGLAND — animated chairlift footer banner
   Flat vector remake of the footer illustration; chairs ride
   the cable on a continuous loop. Exported to window.
   ============================================================ */
const LS = { metal: "#4e5c6e", light: "#dfe8f2" };

/* tiered conifer: returns a path string */
function pinePath(x, base, h, w, tiers) {
  let d = "";
  for (let i = 0; i < tiers; i++) {
    const f = i / (tiers - 1 || 1);
    const topY = base - h + h * 0.72 * f;
    const botY = topY + h * 0.33;
    const half = (w / 2) * (0.36 + 0.64 * f);
    d += `M${x},${topY} L${x + half},${botY} L${x - half},${botY} Z `;
  }
  d += `M${x - w * 0.055},${base - h * 0.12} L${x + w * 0.055},${base - h * 0.12} L${x + w * 0.055},${base} L${x - w * 0.055},${base} Z`;
  return d;
}

function Pines({ list, fill, shade }) {
  return (
    <g>
      {shade && list.map((p, i) => <path key={`s${i}`} d={pinePath(p[0] + p[3] * 0.11, p[1], p[2], p[3], p[4])} fill={shade} />)}
      {list.map((p, i) => <path key={i} d={pinePath(p[0], p[1], p[2], p[3], p[4])} fill={fill} />)}
    </g>
  );
}

function TreeBandLS({ from, to, step, base, h, w, fill, jitter = 0 }) {
  let d = "";
  let n = 0;
  for (let x = from; x <= to; x += step, n++) {
    const j = jitter ? ((n * 37) % 11) / 11 * jitter : 0;
    const hh = h - j;
    d += `M${x},${base - hh} L${x + w / 2},${base} L${x - w / 2},${base} Z `;
  }
  return <path d={d} fill={fill} />;
}

function LiftChair({ track, delay }) {
  return (
    <g className={`lift-chair lift-${track}`} style={{ animationDelay: delay }}>
      <rect x="-30" y="-9" width="62" height="17" rx="6" fill={LS.metal} />
      <rect x="-8" y="2" width="16" height="122" fill={LS.metal} />
      <rect x="-8" y="104" width="56" height="18" rx="4" fill={LS.metal} />
      <path d="M-118,250 L-112,152 Q-109,118 -80,118 L86,118 Q115,118 118,152 L124,250 Z" fill={LS.metal} />
      <rect x="-108" y="152" width="222" height="11" fill={LS.light} />
      <rect x="-114" y="230" width="234" height="9" fill={LS.light} />
      <rect x="-122" y="248" width="244" height="42" rx="5" fill={LS.metal} />
      <rect x="-112" y="256" width="224" height="6" fill={LS.light} />
      <rect x="-112" y="267" width="224" height="6" fill={LS.light} />
      <rect x="-112" y="278" width="224" height="6" fill={LS.light} />
    </g>
  );
}

function SnowGun() {
  return (
    <g>
      <path d="M1004,704 L1014,650 L1023,650 L1013,704 Z" fill={LS.metal} />
      <path d="M1040,704 L1030,650 L1021,650 L1031,704 Z" fill={LS.metal} />
      <rect x="1014" y="612" width="15" height="46" fill={LS.metal} />
      <g transform="rotate(-26 1021 612)">
        <rect x="1012" y="592" width="98" height="40" rx="19" fill={LS.metal} />
        <rect x="1030" y="601" width="64" height="7" fill={LS.light} />
        <rect x="1100" y="586" width="13" height="52" rx="6" fill={LS.metal} />
      </g>
      <g className="snow-plume" fill="#ffffff">
        <path d="M1096,562 Q 1182,520 1272,496 Q 1212,540 1182,588 Q 1140,588 1096,562 Z" opacity="0.9" />
        <path d="M1106,552 Q 1202,500 1304,474 Q 1232,520 1198,570 Z" opacity="0.5" />
        <circle cx="1312" cy="468" r="5" opacity="0.7" />
        <circle cx="1342" cy="484" r="3.5" opacity="0.5" />
        <circle cx="1288" cy="448" r="3" opacity="0.45" />
      </g>
    </g>
  );
}

function SceneArt({ lift = true }) {
  const N = 7, D = 34;
  const aDelays = Array.from({ length: N }, (_, i) => -(D / N) * i);
  const bDelays = aDelays.map((d) => d - D / (2 * N));
  return (
    <>
        <defs>
          <linearGradient id="ls-sky" x1="0" y1="0" x2="0" y2="1">
            <stop offset="0%" stopColor="#d9e4f1" />
            <stop offset="100%" stopColor="#e7eef7" />
          </linearGradient>
          <linearGradient id="ls-fade" gradientUnits="userSpaceOnUse" x1="1108" y1="646" x2="1186" y2="678">
            <stop offset="0%" stopColor={LS.metal} stopOpacity="0.85" />
            <stop offset="100%" stopColor={LS.metal} stopOpacity="0" />
          </linearGradient>
        </defs>

        <rect width="1872" height="840" fill="url(#ls-sky)" />

        {/* clouds */}
        <g fill="#eef4fa">
          <path d="M0,430 L0,392 Q 36,340 104,352 Q 140,310 206,330 Q 268,318 288,366 Q 340,372 348,430 Z" />
          <path d="M352,452 Q 384,402 448,414 Q 486,386 548,404 Q 606,396 628,440 Q 664,446 670,452 Z" />
          <path d="M1032,450 Q 1086,402 1168,412 Q 1214,372 1288,386 Q 1356,370 1392,410 Q 1444,418 1462,450 Z" />
        </g>

        {/* mountain ranges, far to near */}
        {/* trail-cut peak, sitting behind the back range */}
        <g>
          <path d="M640,442 L740,532 L544,532 Z" fill="#ffffff" />
          <path d="M640,444 Q 598,482 548,532 L582,532 Q 616,478 642,458 Z" fill="#8292ab" />
          <path d="M640,444 Q 684,482 736,532 L702,532 Q 666,478 640,458 Z" fill="#8292ab" />
          <g fill="none" stroke="#b6c2d4" strokeWidth="1.6" strokeLinecap="round">
            <path d="M637,468 Q 633,498 627,528" />
            <path d="M646,474 Q 650,502 654,526" />
            <path d="M625,474 Q 616,500 609,526" />
            <path d="M656,480 Q 664,504 672,526" />
          </g>
        </g>
        <path d="M0,560 L0,492 Q 110,462 232,486 Q 350,508 470,484 Q 590,458 706,490 Q 830,518 952,486 Q 1072,456 1196,484 Q 1320,510 1440,482 Q 1560,456 1684,486 Q 1790,508 1872,482 L1872,560 Z" fill="#c6d0de" />
        <path d="M0,620 L0,540 Q 100,510 230,530 Q 360,548 500,512 Q 640,480 780,520 Q 920,556 1060,524 Q 1200,494 1340,528 Q 1480,560 1620,520 Q 1760,486 1872,512 L1872,620 Z" fill="#b5c2d4" />
        <path d="M0,690 L0,585 Q 140,555 300,580 Q 450,604 600,570 Q 760,535 920,575 Q 1080,612 1240,578 Q 1400,546 1560,580 Q 1720,610 1872,575 L1872,690 Z" fill="#a3b1c6" />
        <path d="M0,730 L0,635 Q 160,608 330,632 Q 500,655 680,625 Q 860,595 1040,628 Q 1220,660 1400,630 Q 1580,602 1872,628 L1872,730 Z" fill="#919fb6" />

        {/* dark treeline */}
        <TreeBandLS from={430} to={1520} step={22} base={700} h={54} w={26} fill="#7b8aa2" jitter={10} />
        <TreeBandLS from={440} to={1500} step={29} base={712} h={72} w={33} fill="#6e7e97" jitter={14} />

        {/* midground pale pines */}
        <Pines fill="#cfdcea" shade="#bccbdf" list={[[516, 768, 112, 70, 5], [580, 762, 96, 60, 4], [644, 758, 124, 74, 5], [704, 748, 102, 62, 5], [752, 742, 86, 55, 4]]} />

        {/* snow-laden pines, left and right stands */}
        <Pines fill="#ffffff" shade="#dbe6f2" list={[
          [38, 772, 300, 150, 5], [150, 800, 334, 172, 5], [262, 756, 248, 138, 5],
          [352, 742, 212, 124, 5], [436, 758, 236, 132, 5], [500, 730, 180, 104, 4],
          [1398, 694, 152, 86, 5], [1452, 682, 120, 70, 4], [1506, 704, 192, 106, 5],
          [1574, 698, 162, 92, 5], [1646, 692, 232, 126, 5], [1724, 704, 202, 112, 5],
          [1792, 692, 262, 140, 5], [1862, 682, 300, 150, 5],
        ]} />

        {/* snowfield */}
        <path d="M0,840 L0,802 Q 420,762 900,708 Q 1330,662 1872,688 L1872,840 Z" fill="#ffffff" />
        <path d="M0,828 Q 500,792 980,744 Q 1400,702 1872,726" fill="none" stroke="#f1f6fc" strokeWidth="11" strokeLinecap="round" />

        {lift ? (<>
        {/* lift line */}
        <g stroke={LS.metal} fill="none" strokeLinecap="round">
          <path d="M976,526 L205,0" strokeWidth="3.4" />
          <path d="M1054,526 L690,0" strokeWidth="3.4" />
          <path d="M976,526 L1108,646" strokeWidth="2" />
          <path d="M1054,526 L1156,646" strokeWidth="1.7" />
          <path d="M1108,646 L1152,668" strokeWidth="1.8" stroke="url(#ls-fade)" />
          <path d="M1156,646 L1192,674" strokeWidth="1.5" stroke="url(#ls-fade)" />
        </g>
        <g fill={LS.metal}>
          <rect x="972" y="513" width="9" height="18" />
          <rect x="1050" y="513" width="9" height="18" />
        </g>

        {/* chairs */}
        {aDelays.map((d, i) => <LiftChair key={`a${i}`} track="a" delay={`${d.toFixed(3)}s`} />)}
        {bDelays.map((d, i) => <LiftChair key={`b${i}`} track="b" delay={`${d.toFixed(3)}s`} />)}

        {/* towers draw last so chairs pass behind them */}
        <g fill={LS.metal}>
          <rect x="1128" y="650" width="8" height="40" />
          <rect x="1102" y="642" width="58" height="8" rx="3" />
          <rect x="1106" y="650" width="6" height="10" />
          <rect x="1152" y="650" width="6" height="10" />
          <rect x="1008" y="512" width="18" height="180" />
          <rect x="962" y="500" width="108" height="13" rx="4" />
        </g>
        <path d="M996,702 Q 1000,674 1017,674 Q 1034,674 1038,702 Z" fill="#eaf1f8" />
        </>) : <SnowGun />}
    </>
  );
}

function LiftScene() {
  const half = { display: "block", width: "50%", height: "auto" };
  return (
    <div aria-hidden="true" style={{ position: "relative", lineHeight: 0, background: "#e4ecf6", display: "flex" }}>
      <SnowEdge fill="var(--retro-900)" height={34} side="top" flip />
      <svg viewBox="0 0 1872 840" style={half}><SceneArt /></svg>
      <svg viewBox="0 0 1872 840" style={{ ...half, transform: "scaleX(-1)" }}><SceneArt lift={false} /></svg>
    </div>
  );
}

Object.assign(window, { LiftScene, SceneArt, LiftChair, SnowGun });
