import { Entity, PointGraphics, useCesium } from "resium";

// 从拉布拉多海到青藏高原
const dataLabToQTP = [
  { longitude: -55, latitude: 58, height: 1000000, time: 10 },
  { longitude: -32, latitude: 72.2, height: 1000000, time: 20 },
  { longitude: 20, latitude: 77, height: 1000000, time: 30 },
  { longitude: 63, latitude: 69.9, height: 1000000, time: 40 },
  { longitude: 88, latitude: 60, height: 1000000, time: 50 },
];

function PathLabToQTP() {
  const { viewer } = useCesium();

  const start = viewer.clock.startTime;
  const stop = viewer.clock.stopTime;

  const pathMaterial = new Cesium.PolylineDashMaterialProperty({
    dashLength: 20,
    color: new Cesium.Color(4 / 255, 251 / 255, 253 / 255),
  });

  /**
   * 计算飞行路径
   * 数据坐标
   * {SampledPositionProperty|*}
   */
  function createProperty(source) {
    let property = new Cesium.SampledPositionProperty();
    for (let i = 0; i < source.length; i++) {
      const start = viewer.clock.startTime;
      const stop = viewer.clock.stopTime;
      let time = Cesium.JulianDate.addSeconds(
        start,
        source[i].time,
        new Cesium.JulianDate()
      );
      let position = Cesium.Cartesian3.fromDegrees(
        source[i].longitude,
        source[i].latitude,
        source[i].height
      );
      // 添加位置，和时间对应
      property.addSample(time, position);
      console.log("property for:>> ", property, start, stop, viewer);
    }
    property.setInterpolationOptions({
      interpolationDegree: 10,
      interpolationAlgorithm: Cesium.LagrangePolynomialApproximation,
    });

    return property;
  }

  const property = createProperty(dataLabToQTP);

  return (
    <Entity
      id={"point"}
      position={property}
      availability={
        new Cesium.TimeIntervalCollection([
          new Cesium.TimeInterval({
            start: start,
            stop: stop,
          }),
        ])
      }
      orientation={new Cesium.VelocityOrientationProperty(property)}
      path={{
        resolution: 1,
        material: pathMaterial,
        // leadTime、trailTime 不设置 path全显示
        leadTime: 0, // 设置为0时 模型通过后显示path
        // trailTime: 0, // 设置为0时 模型通过后隐藏path
        width: 2,
      }}
    >
      <PointGraphics
        show={true}
        color={Cesium.Color.SKYBLUE}
        pixelSize={10}
        outlineColor={Cesium.Color.YELLOW}
        outlineWidth={3}
      />
    </Entity>
  );
}

export default PathLabToQTP;
