import { Cartesian2, Cartesian3, Math } from "cesium";
import { CloudCollection, useCesium } from "resium";

function Cloud() {
  const { viewer } = useCesium();
  const scene = viewer.scene;
  const position = Cartesian3.fromDegrees(-123.0744619, 44.0503706, 50);

  Math.setRandomNumberSeed(2.5);
  function getRandomNumberInRange(minValue, maxValue) {
    return minValue + Math.nextRandomNumber() * (maxValue - minValue);
  }

  const clouds = new CloudCollection();

  // manually position clouds in the mountains
  function createBackLayerClouds() {
    clouds.add({
      position: new Cartesian3.fromDegrees(-122.6908, 45.496, 300),
      scale: new Cartesian2(1500, 250),
      maximumSize: new Cartesian3(50, 15, 13),
      slice: 0.3,
    });

    clouds.add({
      position: new Cartesian3.fromDegrees(-122.72, 45.5, 335),
      scale: new Cartesian2(1500, 300),
      maximumSize: new Cartesian3(50, 12, 15),
      slice: 0.36,
    });
  }
  let long, lat, height, scaleX, scaleY, aspectRatio, cloudHeight, depth, slice;

  // randomly generate clouds in a certain area
  function createRandomClouds(
    numClouds,
    startLong,
    stopLong,
    startLat,
    stopLat,
    minHeight,
    maxHeight
  ) {
    const rangeLong = stopLong - startLong;
    const rangeLat = stopLat - startLat;
    for (let i = 0; i < numClouds; i++) {
      long = startLong + getRandomNumberInRange(0, rangeLong);
      lat = startLat + getRandomNumberInRange(0, rangeLat);
      height = getRandomNumberInRange(minHeight, maxHeight);
      scaleX = getRandomNumberInRange(150, 350);
      scaleY = scaleX / 2.0 - getRandomNumberInRange(0, scaleX / 4.0);
      slice = getRandomNumberInRange(0.3, 0.7);
      depth = getRandomNumberInRange(5, 20);
      aspectRatio = getRandomNumberInRange(1.5, 2.1);
      cloudHeight = getRandomNumberInRange(5, 20);
      clouds.add({
        position: Cartesian3.fromDegrees(long, lat, height),
        scale: new Cartesian2(scaleX, scaleY),
        maximumSize: new Cartesian3(
          aspectRatio * cloudHeight,
          cloudHeight,
          depth
        ),
        slice: slice,
      });
    }
  }
  // manually position clouds in front
  const scratch = new Cartesian3();
  function createFrontLayerClouds() {
    clouds.add({
      position: Cartesian3.fromDegrees(-122.666, 45.5126, 97),
      scale: new Cartesian2(400, 150),
      maximumSize: new Cartesian3(25, 12, 15),
      slice: 0.36,
    });

    clouds.add({
      position: Cartesian3.fromDegrees(-122.6665, 45.5262, 76),
      scale: new Cartesian2(450, 200),
      maximumSize: new Cartesian3(25, 14, 12),
      slice: 0.3,
    });
  }

  createBackLayerClouds();
  createRandomClouds(8, -122.685, -122.67, 45.51, 45.525, 50, 250);
  createFrontLayerClouds();

  scene.primitives.add(clouds);
}

export default Cloud;
