import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js";

const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(
  75,
  window.innerWidth / window.innerHeight,
  0.1,
  1000
);

// Antialiasing and responsiveness
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(window.devicePixelRatio);
document.body.appendChild(renderer.domElement);

// Orbit Controls for better interaction
const controls = new OrbitControls(camera, renderer.domElement);
controls.autoRotate = true; // Enable auto-rotate initially
controls.enableDamping = true;

// Camera initial position
camera.position.set(0, 0, 10);
controls.update();

// Create Globe geometry
const globeGeometry = new THREE.SphereGeometry(5, 180, 90);

// Load map texture (earth texture)
const textureLoader = new THREE.TextureLoader();
const earthTexture = textureLoader.load("/map.jpg");
const pointTexture = textureLoader.load("/circle.png"); // Particle shape texture

// Material for the Points (using ShaderMaterial)
const globeMaterial = new THREE.ShaderMaterial({
  uniforms: {
    visibilityTexture: { value: earthTexture }, // Earth texture map
    pointTexture: { value: pointTexture }, // Particle circular texture
    pointSize: { value: 0.1 }, // Size of particles
    scale: { value: window.innerHeight / 2 }, // Scale relative to screen
  },
  vertexShader: `
    uniform float scale;
    uniform float pointSize;
    varying vec2 vUv; // Pass UVs to fragment shader for texture lookup

    void main() {
      vUv = uv; // Pass uv coordinates to fragment shader
      vec4 mvPosition = modelViewMatrix * vec4(position, 1.0); // Transform vertex
      gl_PointSize = pointSize * (scale / length(mvPosition.xyz)); // Set size of particles
      gl_Position = projectionMatrix * mvPosition; // Final position
    }
  `,
  fragmentShader: `
    uniform sampler2D visibilityTexture; // Texture for visibility
    uniform sampler2D pointTexture; // Shape of the particles
    varying vec2 vUv;

    void main() {
      // Sample from the visibility texture (earth texture)
      vec4 visibility = texture2D(visibilityTexture, vUv);

      // Discard invisible pixels (based on earth texture brightness)
      if (visibility.r < 0.1) discard;

      // Sample the particle shape texture
      vec4 shape = texture2D(pointTexture, gl_PointCoord);

      // Discard based on alpha channel of shape
      if (shape.a < 0.1) discard;

      // Final color
      gl_FragColor = vec4(visibility.rgb, 1.0) * shape;
    }
  `,
  transparent: true,
});

// Create particle-based globe using Points
const globePoints = new THREE.Points(globeGeometry, globeMaterial);
scene.add(globePoints);

// Create an invisible globe (for hover detection)
const globeBasicMaterial = new THREE.MeshBasicMaterial({
  transparent: true,
  opacity: 0, // Fully invisible
});

const globe = new THREE.Mesh(globeGeometry, globeBasicMaterial);
scene.add(globe);

// Raycaster for mouse interaction
const raycaster = new THREE.Raycaster();
const mouse = new THREE.Vector2();
let isHovering = false;

// Event listeners for mouse movement
const onMouseMove = (event) => {
  // Calculate mouse position in normalized device coordinates (-1 to +1)
  mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;

  // Update the raycaster
  raycaster.setFromCamera(mouse, camera);

  // Check for intersection with the invisible globe
  const intersects = raycaster.intersectObject(globe);

  if (intersects.length > 0) {
    // Mouse is hovering over the globe
    if (!isHovering) {
      isHovering = true;
      controls.autoRotate = false; // Disable auto-rotate
    }
  } else {
    // Mouse is not hovering over the globe
    if (isHovering) {
      isHovering = false;
      controls.autoRotate = true; // Re-enable auto-rotate
    }
  }
};

window.addEventListener("mousemove", onMouseMove);

// Responsive resizing
window.addEventListener("resize", () => {
  camera.aspect = window.innerWidth / window.innerHeight;
  camera.updateProjectionMatrix();
  renderer.setSize(window.innerWidth, window.innerHeight);
});

// Animation loop
function animate() {
  controls.update();
  renderer.render(scene, camera);
}

renderer.setAnimationLoop(animate);


"add levitating effect on globe "