import { adu } from "./drawUtils.js";

// 点-标绘工具类
class PointDrawer {
  constructor(arg) {
    this.viewer = arg.viewer;

    this.id = arg.id; //id
    this.type = arg.type; //类型 point-点；marker-图标
    this.pointOpts = arg.pointOpts; //点属性
    this.icon = arg.icon; //图标

    this.beforeDraw = arg.beforeDraw; //标注前
    this.afterDraw = arg.afterDraw; //标注完成

    this.pos = null; //标注坐标
    this.entity = null; //标注
  }
  //开始
  start() {
    this.handler = new Cesium.ScreenSpaceEventHandler(this.viewer.scene.canvas);
    //单击-标注
    this.handler.setInputAction((e) => {
      this.pos = adu.getPos(e.position, this.viewer); //点坐标
      if (this.pos) {
        //标注
        if ("marker" == this.type) {
          this.entity = adu.addMarker(
            {
              id: this.id,
              icon: this.icon,
              ...this.pos,
            },
            this.viewer
          );
        } else {
          this.entity = adu.addPoint(
            {
              id: this.id,
              ...this.pointOpts,
              ...this.pos,
            },
            this.viewer
          );
        }

        //标注完成回调
        if (this.afterDraw) {
          this.afterDraw(this.pos, this.entity);
        }
      }
    }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
  }
  //重置参数
  reset(arg) {
    if (arg) {
      this.id = arg.id; //id
      this.type = arg.type; //类型 point-点；marker-图标
      this.pointOpts = arg.pointOpts; //点属性
      this.icon = arg.icon; //图标
    }

    this.pos = null;
    this.entity = null;
  }
  //销毁鼠标事件
  destroy() {
    if (this.handler) {
      this.handler.destroy();
      this.handler = null;
    }
  }
}

export default PointDrawer;
