import * as THREE from "three";
import type { ShipSnapshot } from "./contracts";

export function shipSize(ship: ShipSnapshot) {
  switch (ship.type) {
    case "carrier":
      return 0.018;
    case "battleship":
      return 0.012;
    case "destroyer":
      return 0.009;
    case "builder":
    case "freighter":
    case "transporter":
    case "resupplier":
    case "miner":
    case "largeminer":
      return 0.01;
    default:
      return 0.007;
  }
}

export function shipLength(ship: ShipSnapshot) {
  return shipSize(ship) * 2.6;
}

export function shipColor(purpose: ShipSnapshot["purpose"]) {
  if (purpose === "mine") {
    return "#ffcf6e";
  }
  if (purpose === "trade") {
    return "#9ff0aa";
  }
  return "#8bc0ff";
}

export function shipPresentationColor(ship: ShipSnapshot) {
  if (ship.spatialState.spaceLayer !== "local-space") {
    return "#c77dff";
  }
  if (ship.spatialState.movementRegime === "warp") {
    return "#ffd166";
  }
  if (ship.spatialState.movementRegime === "ftl-transit") {
    return "#ff6ad5";
  }
  return shipColor(ship.purpose);
}

export function celestialColor(kind: string) {
  if (kind.includes("lagrange")) {
    return "#7fe8ff";
  }
  if (kind.includes("station")) {
    return "#ffc36e";
  }
  if (kind.includes("planet")) {
    return "#8bc0ff";
  }
  if (kind.includes("moon")) {
    return "#c7d7e8";
  }
  return "#ffe082";
}

export function createCirclePoints(radius: number, segments: number) {
  return Array.from({ length: segments }, (_, index) => {
    const theta = (index / segments) * Math.PI * 2;
    return new THREE.Vector3(Math.cos(theta) * radius, 0, Math.sin(theta) * radius);
  });
}
