/* Yellow Stone Traders & General Supplies — About page hero background
 * A small stacked vault of gold ingots (bars) — the "built on gold"
 * foundation story, literalized. Same interaction model as the other hero
 * scenes: slow auto-rotation, mouse parallax, scroll-driven fade/scale.
 * Skipped on small screens and prefers-reduced-motion.
 */
(function () {
  var canvas = document.getElementById('heroCanvas');
  if (!canvas || typeof THREE === 'undefined') return;
  if (window.matchMedia('(max-width: 700px)').matches) return;

  var reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;

  var scene = new THREE.Scene();
  var camera = new THREE.PerspectiveCamera(42, window.innerWidth / window.innerHeight, 0.1, 100);
  camera.position.set(0, 0.7, 9.5);

  var renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true, alpha: true });
  renderer.setSize(window.innerWidth, window.innerHeight);
  renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
  renderer.setClearColor(0x000000, 0);

  // Same warm/cool studio lighting rig as the other hero scenes.
  scene.add(new THREE.AmbientLight(0x2a2116, 1.15));
  var key = new THREE.PointLight(0xffcf7a, 3.4, 40);
  key.position.set(6, 6, 8);
  scene.add(key);
  var rim = new THREE.PointLight(0x6d7dff, 1.1, 40);
  rim.position.set(-7, -3, 5);
  scene.add(rim);
  var fill = new THREE.PointLight(0xffb347, 1.6, 40);
  fill.position.set(-4, 5, -4);
  scene.add(fill);

  var goldMat = new THREE.MeshStandardMaterial({
    color: 0xf3ab1c, emissive: 0x3a2200, emissiveIntensity: 0.32,
    metalness: 0.88, roughness: 0.28, flatShading: true
  });

  function makeIngot(w, h, d) {
    // A tapered bar: wider base, slightly narrower top — the classic ingot
    // silhouette — built by scaling the top face of a box inward.
    var geo = new THREE.BoxGeometry(w, h, d, 1, 1, 1);
    var pos = geo.attributes.position;
    var taper = 0.78;
    for (var i = 0; i < pos.count; i++) {
      var y = pos.getY(i);
      if (y > 0) {
        pos.setX(i, pos.getX(i) * taper);
        pos.setZ(i, pos.getZ(i) * taper);
      }
    }
    geo.computeVertexNormals();
    return new THREE.Mesh(geo, goldMat);
  }

  var group = new THREE.Group();
  var bars = [];

  // A small stacked vault: 3 on the bottom row, 2 on top, like ingots at rest.
  var layout = [
    { w: 1.7, h: 0.55, d: 1.0, x: -1.35, y: -0.9, z: 0.4 },
    { w: 1.7, h: 0.55, d: 1.0, x: 0.35,  y: -0.9, z: 0.4 },
    { w: 1.7, h: 0.55, d: 1.0, x: -0.5,  y: -0.9, z: -0.7 },
    { w: 1.55, h: 0.5, d: 0.92, x: -0.9, y: -0.32, z: -0.1 },
    { w: 1.55, h: 0.5, d: 0.92, x: 0.55, y: -0.32, z: -0.1 },
    { w: 1.4, h: 0.46, d: 0.85, x: -0.15, y: 0.22, z: -0.35 }
  ];
  layout.forEach(function (d) {
    var bar = makeIngot(d.w, d.h, d.d);
    bar.position.set(d.x, d.y, d.z);
    bar.rotation.y = (Math.random() - 0.5) * 0.25;
    group.add(bar);
    bars.push(bar);
  });

  group.position.x = 0.7;
  scene.add(group);

  // Faint wireframe orbit ring — the survey/blueprint nod shared across hero scenes.
  var ringGeo = new THREE.TorusGeometry(4.2, 0.008, 8, 96);
  var ringMat = new THREE.MeshBasicMaterial({ color: 0xfdaf17, transparent: true, opacity: 0.22 });
  var ring = new THREE.Mesh(ringGeo, ringMat);
  ring.rotation.x = Math.PI / 2.4;
  ring.position.copy(group.position);
  scene.add(ring);

  function handleResize() {
    camera.aspect = window.innerWidth / window.innerHeight;
    camera.updateProjectionMatrix();
    renderer.setSize(window.innerWidth, window.innerHeight);
  }
  window.addEventListener('resize', handleResize);

  var scrollProgress = 0;
  window.addEventListener('scroll', function () {
    var doc = document.documentElement;
    var max = doc.scrollHeight - doc.clientHeight;
    scrollProgress = max > 0 ? Math.min(window.scrollY / (window.innerHeight * 1.2), 1) : 0;
  }, { passive: true });

  var mouseX = 0, mouseY = 0;
  window.addEventListener('mousemove', function (e) {
    mouseX = (e.clientX / window.innerWidth - 0.5);
    mouseY = (e.clientY / window.innerHeight - 0.5);
  });

  if (reduceMotion) {
    renderer.render(scene, camera);
    return;
  }

  var clock = new THREE.Clock();
  function animate() {
    requestAnimationFrame(animate);
    var delta = clock.getDelta();

    group.rotation.y += delta * 0.1;
    ring.rotation.z += delta * 0.05;

    camera.position.x += (mouseX * 1.2 - camera.position.x) * 0.03;
    camera.position.y += (0.7 - mouseY * 0.6 - camera.position.y) * 0.03;
    camera.lookAt(0.7, -0.1, 0);

    group.position.y = -scrollProgress * 1.4;
    group.scale.setScalar(1 - scrollProgress * 0.1);

    renderer.render(scene, camera);
  }
  animate();
})();
