// utils.js - Utility functions for coordinate conversion

import * as THREE from 'three';

import { CONFIG } from './constants.js';

// Convert latitude/longitude to 3D vector
export function latLonToVector3(lat, lon, radius = CONFIG.earthRadius) {
  const phi = (90 - lat) * (Math.PI / 180);
  const theta = (lon + 180) * (Math.PI / 180);

  const x = -(radius * Math.sin(phi) * Math.cos(theta));
  const z = radius * Math.sin(phi) * Math.sin(theta);
  const y = radius * Math.cos(phi);

  return new THREE.Vector3(x, y, z);
}

// Convert 3D vector to latitude/longitude
export function vector3ToLatLon(vector) {
  const radius = Math.sqrt(vector.x * vector.x + vector.y * vector.y + vector.z * vector.z);
  const lat = 90 - (Math.acos(vector.y / radius) * 180 / Math.PI);
  
  let lon = (Math.atan2(vector.z, -vector.x) * 180 / Math.PI) - 180;
  
  while (lon <= -180) lon += 360;
  while (lon > 180) lon -= 360;

  return {
    lat: parseFloat(lat.toFixed(4)),
    lon: parseFloat(lon.toFixed(4)),
    alt: radius - CONFIG.earthRadius
  };
}

// Convert screen coordinates to Earth surface 3D coordinates
export function screenToEarthCoords(clientX, clientY, camera, earth, domElement = document.body) {
  const raycaster = new THREE.Raycaster();
  const mouse = new THREE.Vector2();

  if (domElement === document.body) {
    mouse.x = (clientX / window.innerWidth) * 2 - 1;
    mouse.y = -(clientY / window.innerHeight) * 2 + 1;
  } else {
    const rect = domElement.getBoundingClientRect();
    mouse.x = ((clientX - rect.left) / rect.width) * 2 - 1;
    mouse.y = -((clientY - rect.top) / rect.height) * 2 + 1;
  }

  raycaster.setFromCamera(mouse, camera);
  const intersects = raycaster.intersectObject(earth);

  if (intersects.length > 0) {
    const localPoint = intersects[0].point.clone();
    earth.worldToLocal(localPoint);
    return localPoint;
  }

  return null;
}

// Calculate accurate spherical distance between two points (Haversine formula)
export function calculateDistance(lat1, lon1, lat2, lon2, radius = CONFIG.earthRadius) {
  const toRad = (angle) => (angle * Math.PI) / 180;
  
  const dLat = toRad(lat2 - lat1);
  const dLon = toRad(lon2 - lon1);
  
  const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
            Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
            Math.sin(dLon / 2) * Math.sin(dLon / 2);
            
  const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  
  return radius * c;
}
