/**
 * vue-cesium - https://github.com/zouyaoji/vue-cesium
 * Vue 2.x components for CesiumJS.
 *
 * @package VueCesium
 * @author zouyaoji <370681295@qq.com>
 * @version 2.2.12
 * @license MIT
 * @homepage https://zouyaoji.top/vue-cesium
 * @copyright (c) 2018-2021, zouyaoji <370681295@qq.com>
 */
import _regeneratorRuntime from '@babel/runtime-corejs2/regenerator';
import _getIterator from '@babel/runtime-corejs2/core-js/get-iterator';
import cmp from '../virtualCmp';

var props = {
  mode: {
    type: Number,
    default: 1
  },
  show: {
    type: Boolean,
    default: true
  },
  pointColor: {
    type: String | Object | Array,
    default: 'rgb(255,229,0)'
  },
  pointPixelSize: {
    type: Number,
    default: 8
  },
  editable: {
    type: Boolean,
    default: false
  },
  showDrawTip: {
    type: Boolean,
    default: true
  }
};
var watch = {
  editable: function editable(val) {
    if (!val) {
      this.showToolbar = false;
    } else {
      this.drawing = false;
    }
  },
  drawing: function drawing(val) {
    var nextTick, polylines, startNew, drawType, getVcParent, polyline, drawCmpNames, measureCmpNames, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, $node, listener;

    return _regeneratorRuntime.async(function drawing$(_context) {
      while (1) {
        switch (_context.prev = _context.next) {
          case 0:
            nextTick = false;
            polylines = this.polylines, startNew = this.startNew, drawType = this.drawType, getVcParent = this.getVcParent;
            polyline = polylines[polylines.length - 1];

            if (!(!val && polyline && !polyline.positions.length)) {
              _context.next = 8;
              break;
            }

            this.polylines.pop();
            this.showTooltip = false;
            _context.next = 47;
            break;

          case 8:
            if (!val) {
              _context.next = 46;
              break;
            }

            drawCmpNames = [];
            _context.t0 = drawType;
            _context.next = _context.t0 === 'pointDrawing' ? 13 : _context.t0 === 'polylineDrawing' ? 16 : _context.t0 === 'polygonDrawing' ? 19 : 22;
            break;

          case 13:
            drawCmpNames.push('vc-handler-draw-polyline');
            drawCmpNames.push('vc-handler-draw-polygon');
            return _context.abrupt("break", 22);

          case 16:
            drawCmpNames.push('vc-handler-draw-point');
            drawCmpNames.push('vc-handler-draw-polygon');
            return _context.abrupt("break", 22);

          case 19:
            drawCmpNames.push('vc-handler-draw-polyline');
            drawCmpNames.push('vc-handler-draw-point');
            return _context.abrupt("break", 22);

          case 22:
            measureCmpNames = ['vc-measure-height', 'vc-measure-distance', 'vc-measure-area'];
            _iteratorNormalCompletion = true;
            _didIteratorError = false;
            _iteratorError = undefined;
            _context.prev = 26;

            for (_iterator = _getIterator(getVcParent(this).$slots.default || []); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
              $node = _step.value;

              if ($node.componentOptions && drawCmpNames.indexOf($node.componentOptions.tag) !== -1) {
                $node.child.drawing = false;
                nextTick = true;
              }

              if ($node.componentOptions && measureCmpNames.indexOf($node.componentOptions.tag) !== -1) {
                $node.child.measuring = false;
                nextTick = true;
              }
            }

            _context.next = 34;
            break;

          case 30:
            _context.prev = 30;
            _context.t1 = _context["catch"](26);
            _didIteratorError = true;
            _iteratorError = _context.t1;

          case 34:
            _context.prev = 34;
            _context.prev = 35;

            if (!_iteratorNormalCompletion && _iterator.return != null) {
              _iterator.return();
            }

          case 37:
            _context.prev = 37;

            if (!_didIteratorError) {
              _context.next = 40;
              break;
            }

            throw _iteratorError;

          case 40:
            return _context.finish(37);

          case 41:
            return _context.finish(34);

          case 42:
            startNew();
            this.showTooltip = true;
            _context.next = 47;
            break;

          case 46:
            this.showTooltip = false;

          case 47:
            if (!this.showTooltip) {
              this.tooltipPosition = [0, 0, 0];
            }

            _context.t2 = nextTick;

            if (!_context.t2) {
              _context.next = 52;
              break;
            }

            _context.next = 52;
            return _regeneratorRuntime.awrap(this.$nextTick());

          case 52:
            this.viewer.canvas.setAttribute('style', val ? 'cursor: crosshair' : 'cursor: auto');
            listener = this.$listeners.activeEvt;
            listener && this.$emit('activeEvt', {
              type: drawType,
              isActive: val
            });

          case 55:
          case "end":
            return _context.stop();
        }
      }
    }, null, this, [[26, 30, 34, 42], [35,, 37, 41]]);
  }
};
var computed = {
  points: function points() {
    var _this = this;

    var points = [];
    this.polylines.forEach(function (polyline, index) {
      polyline.positions.forEach(function (position, subIndex) {
        var point = {
          color: _this.pointColor,
          pixelSize: _this.pointPixelSize,
          position: position,
          polylineIndex: index,
          positionIndex: subIndex
        };
        points.push(point);
      });
    });
    return points;
  }
};
var methods = {
  createCesiumObject: function createCesiumObject() {
    var viewer, handler;
    return _regeneratorRuntime.async(function createCesiumObject$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            viewer = this.viewer;
            handler = new Cesium.ScreenSpaceEventHandler(viewer.canvas);
            handler.setInputAction(this.LEFT_CLICK, Cesium.ScreenSpaceEventType.LEFT_CLICK);
            handler.setInputAction(this.MOUSE_MOVE, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
            handler.setInputAction(this.RIGHT_CLICK, Cesium.ScreenSpaceEventType.RIGHT_CLICK);
            this.handler = handler;
            return _context2.abrupt("return", this.polylines);

          case 7:
          case "end":
            return _context2.stop();
        }
      }
    }, null, this);
  },
  mount: function mount() {
    return _regeneratorRuntime.async(function mount$(_context3) {
      while (1) {
        switch (_context3.prev = _context3.next) {
          case 0:
            return _context3.abrupt("return", true);

          case 1:
          case "end":
            return _context3.stop();
        }
      }
    });
  },
  unload: function unload() {
    return _regeneratorRuntime.async(function unload$(_context4) {
      while (1) {
        switch (_context4.prev = _context4.next) {
          case 0:
            return _context4.abrupt("return", true);

          case 1:
          case "end":
            return _context4.stop();
        }
      }
    });
  },
  LEFT_CLICK: function LEFT_CLICK(movement) {
    if (!this.drawing) {
      return;
    }

    if (this.editingPoint) {
      this.RIGHT_CLICK(movement);
      return;
    }

    var Cesium = this.Cesium,
        viewer = this.viewer,
        polylines = this.polylines,
        onDrawingEvt = this.onDrawingEvt;
    var cartesian = viewer.scene.pickPosition(movement.position);

    if (!Cesium.defined(cartesian)) {
      return;
    }

    var nIndex = polylines.length - 1;
    var polyline = polylines[nIndex];
    polyline.positions.push(cartesian);
    onDrawingEvt(polyline, nIndex);
  },
  MOUSE_MOVE: function MOUSE_MOVE(movement) {
    var Cesium, viewer, polylines, onDrawingEvt, drawType, cartesian, nIndex, polyline, listener;
    return _regeneratorRuntime.async(function MOUSE_MOVE$(_context5) {
      while (1) {
        switch (_context5.prev = _context5.next) {
          case 0:
            if (this.drawing) {
              _context5.next = 2;
              break;
            }

            return _context5.abrupt("return");

          case 2:
            Cesium = this.Cesium, viewer = this.viewer, polylines = this.polylines, onDrawingEvt = this.onDrawingEvt, drawType = this.drawType;
            cartesian = viewer.scene.pickPosition(movement.endPosition);

            if (Cesium.defined(cartesian)) {
              _context5.next = 6;
              break;
            }

            return _context5.abrupt("return");

          case 6:
            this.tooltipPosition = cartesian;
            this.tooltip = this.$vc.lang.draw.drawingTip1;

            if (polylines.length) {
              _context5.next = 10;
              break;
            }

            return _context5.abrupt("return");

          case 10:
            nIndex = this.editingPoint ? this.editingPoint.polylineIndex : polylines.length - 1;
            polyline = polylines[nIndex];

            if (polyline.positions.length) {
              _context5.next = 14;
              break;
            }

            return _context5.abrupt("return");

          case 14:
            if (polyline.positions.length >= 2) {
              this.tooltip = this.editingPoint ? this.$vc.lang.draw.drawingTip3 : this.$vc.lang.draw.drawingTip2;
            }

            if (this.editingPoint) {
              polyline.positions.splice(this.editingPoint.positionIndex, 1, cartesian);
            } else {
              if (polyline.positions.length >= 2) {
                polyline.positions.pop();
              }

              polyline.positions.push(cartesian);
            }

            listener = this.$listeners.movingEvt;
            listener && this.$emit('movingEvt', movement.endPosition, drawType);
            onDrawingEvt(polyline, nIndex);

          case 19:
          case "end":
            return _context5.stop();
        }
      }
    }, null, this);
  },
  RIGHT_CLICK: function RIGHT_CLICK(movement) {
    var viewer, polylines, mode, startNew, onDrawingEvt, nIndex, polyline, cartesian;
    return _regeneratorRuntime.async(function RIGHT_CLICK$(_context6) {
      while (1) {
        switch (_context6.prev = _context6.next) {
          case 0:
            if (this.drawing) {
              _context6.next = 2;
              break;
            }

            return _context6.abrupt("return");

          case 2:
            viewer = this.viewer, polylines = this.polylines, mode = this.mode, startNew = this.startNew, onDrawingEvt = this.onDrawingEvt;

            if (polylines.length) {
              _context6.next = 5;
              break;
            }

            return _context6.abrupt("return");

          case 5:
            nIndex = this.editingPoint ? this.editingPoint.polylineIndex : polylines.length - 1; // const nIndex = polylines.length - 1

            polyline = polylines[nIndex];

            if (!(polyline.positions.length === 0)) {
              _context6.next = 9;
              break;
            }

            return _context6.abrupt("return");

          case 9:
            cartesian = viewer.scene.pickPosition(movement.position);

            if (Cesium.defined(cartesian)) {
              _context6.next = 12;
              break;
            }

            return _context6.abrupt("return");

          case 12:
            if (!this.editingPoint) {
              if (polyline.positions.length > 1) {
                polyline.positions.pop();
              }

              if (mode === 0) {
                startNew();
              } else {
                this.drawing = false;
              }
            } else {
              this.editingPoint = undefined;
              this.drawing = false;
            }

            _context6.next = 15;
            return _regeneratorRuntime.awrap(this.$nextTick());

          case 15:
            onDrawingEvt(polyline, nIndex, true);

          case 16:
          case "end":
            return _context6.stop();
        }
      }
    }, null, this);
  },
  startNew: function startNew() {
    var polylines = this.polylines;
    var poyline = {
      positions: []
    };
    Cesium.defined(polylines) && polylines.push(poyline);
  },
  clear: function clear() {
    this.polylines = [];
    this.drawing = false;
  },
  onDrawingEvt: function onDrawingEvt(polyline, index) {
    var flag = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
    this.index = index;

    if (!this.depthTest) {
      var rs = Cesium.RenderState.fromCache({
        depthMask: true,
        depthTest: {
          enabled: false
        }
      });
      this.$refs.pointCollection && (this.$refs.pointCollection.cesiumObject._rsOpaque = rs);

      if (Cesium.SuperMapImageryProvider) {
        this.$refs.polylineCollection && (this.$refs.polylineCollection.cesiumObject._opaqueRS = rs);
      } else {
        this.$refs.polylineCollection && (this.$refs.polylineCollection.cesiumObject._opaqueRS.depthTest.enabled = false);
      }
    }

    var listener = this.$listeners.drawEvt;
    listener && this.$emit('drawEvt', {
      polyline: polyline,
      type: this.drawType,
      finished: flag
    });
  },
  pointMouseOver: function pointMouseOver(e) {
    if (!this.editable) {
      return;
    }

    if (this.editingPoint) {
      return;
    }

    e.pickedFeature.primitive.pixelSize = this.pointPixelSize * 2.0;
    this.toolbarPosition = e.pickedFeature.primitive.position;
    this.showToolbar = true;
    this.mouseoverPoint = e.pickedFeature.primitive;
  },
  pointMouseOut: function pointMouseOut(e) {
    if (!this.editable) {
      return;
    }

    e.pickedFeature.primitive.pixelSize = this.pointPixelSize * 1.0;
    this.toolbarPosition = [0, 0, 0];
    this.showToolbar = false;
    this.mouseoverPoint = undefined;
  },
  onEditClick: function onEditClick(e) {
    this.editType = e;
    this.toolbarPosition = [0, 0, 0];
    this.showToolbar = false;

    if (!this.editable) {
      return;
    }

    this.drawing = false;

    if (e === 'delete') {
      var nIndex = this.mouseoverPoint.polylineIndex;
      var polyline = this.polylines[nIndex];
      polyline.positions.splice(this.mouseoverPoint.positionIndex, 1);
    } else if (e === 'insert') {
      var _nIndex = this.mouseoverPoint.polylineIndex;
      var _polyline = this.polylines[_nIndex];

      _polyline.positions.splice(this.mouseoverPoint.positionIndex, 0, this.mouseoverPoint.position);

      this.editingPoint = this.mouseoverPoint;
      this.drawing = true;
    } else {
      this.editingPoint = this.mouseoverPoint;
      this.drawing = true;
    }
  }
};
var mixinDraw = {
  mixins: [cmp],
  data: function data() {
    return {
      drawing: false,
      polylines: [],
      toolbarPosition: [0, 0, 0],
      showToolbar: false,
      tooltipPosition: [0, 0, 0],
      showTooltip: false,
      tooltip: ''
    };
  },
  props: props,
  watch: watch,
  computed: computed,
  methods: methods,
  destroyed: function destroyed() {
    var handler = this.handler;

    if (handler) {
      handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
      handler.removeInputAction(Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      handler.removeInputAction(Cesium.ScreenSpaceEventType.RIGHT_CLICK);
      handler.destroy();
    }
  }
};

export default mixinDraw;
