import PolylineTrailLinkImage from "~/assets/images/map_bi/wall_img.png";

const Cesium = window.Cesium;

function WallPolylineTrailLinkMaterialProperty(
  viewer,
  options = {
    color: Cesium.Color.fromBytes(201, 118, 243).withAlpha(0.5),
    duration: 2000,
  },
) {
  this.viewer = viewer;
  this._definitionChanged = new Cesium.Event();
  this._color = undefined;
  this._colorSubscription = undefined;
  this.color = options.color;
  this.duration = options.duration;
  this._time = new Date().getTime();
}

Object.defineProperties(WallPolylineTrailLinkMaterialProperty.prototype, {
  isConstant: {
    get() {
      return false;
    },
  },
  definitionChanged: {
    get() {
      return this._definitionChanged;
    },
  },
  color: Cesium.createPropertyDescriptor("color"),
});
WallPolylineTrailLinkMaterialProperty.prototype.getType = function () {
  return "PolylineTrailLink";
};
WallPolylineTrailLinkMaterialProperty.prototype.getValue = function (
  time,
  result,
) {
  if (!Cesium.defined(result)) {
    result = {};
  }
  result.color = Cesium.Property.getValueOrClonedDefault(
    this._color,
    time,
    Cesium.Color.WHITE,
    result.color,
  );
  result.image = Cesium.Material.PolylineTrailLinkImage;

  if (this.duration) {
    result.time
      = ((new Date().getTime() - this._time) % this.duration) / this.duration;
  }
  this.viewer.scene.requestRender();
  return result;
};
WallPolylineTrailLinkMaterialProperty.prototype.equals = function (other) {
  return (
    this === other
    || (other instanceof WallPolylineTrailLinkMaterialProperty
      && Cesium.Property.equals(this._color, other._color))
  );
};
Cesium.WallPolylineTrailLinkMaterialProperty
  = WallPolylineTrailLinkMaterialProperty;
Cesium.Material.PolylineTrailLinkType = "PolylineTrailLink";
Cesium.Material.PolylineTrailLinkImage = PolylineTrailLinkImage;
Cesium.Material.PolylineTrailLinkSource = `czm_material czm_getMaterial(czm_materialInput
                                            materialInput)\n\
                                            {\n\
                                            czm_material material =
                                              czm_getDefaultMaterial(materialInput);\n\
                                            vec2 st = materialInput.st;\n\
                                            vec4 colorImage = texture2D(image,
                                              vec2(fract(st.t - time), st.t));\n\
                                            vec4 fragColor;\n\
                                            fragColor.rgb = color.rgb / 1.0;\n\
                                            fragColor = czm_gammaCorrect(fragColor);\n\
                                            material.alpha = colorImage.a * color.a;\n\
                                            material.diffuse = color.rgb;\n\
                                            material.emission = fragColor.rgb;\n\
                                            return material;\n\
                                            }`;
Cesium.Material._materialCache.addMaterial(
  Cesium.Material.PolylineTrailLinkType,
  {
    fabric: {
      type: Cesium.Material.PolylineTrailLinkType,
      uniforms: {
        color: new Cesium.Color(1.0, 1.0, 1.0, 1),
        image: Cesium.Material.PolylineTrailLinkImage,
        time: 0,
      },
      source: Cesium.Material.PolylineTrailLinkSource,
    },
    translucent() {
      return true;
    },
  },
);
