function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (it) return (it = it.call(o)).next.bind(it); if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }

function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }

function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }

import MapUtil from './../MapUtil';
import GeoUtil from './../GeoUtil';
import Bridge from './../Bridge';
import locale from './../locale'; // 国际化

export default function () {
  var s = this;
  s.mapUtil = null; // 是否中断未完成的绘制

  s.abort = false; // 标记

  s.markers = null;
  s.marker = null; // 标签

  s.label = null; // 地区

  s.district = null;
  s.districtPolygons = null; // 多边形

  s.polygon = null; // 圆形

  s.circle = null; // 初始化地图

  s.initMap = function (container, center, callback) {
    var self = this;
    Bridge.showLoading();
    var mapUtil = new MapUtil(container, {
      onError: function onError(s, err) {
        if (callback) callback(err.errMsg);
      },
      // 缩放导航
      navigation: {
        position: 'bottom-right',
        type: 'zoom'
      },
      // 禁用点击景点弹出详细信息的方法
      mapOptions: {
        enableMapClick: false
      },
      // 中心位置
      center: center
    });
    mapUtil.map.addEventListener('load', function (e) {
      Bridge.hideLoading();
      window.clearTimeout(self.loadTimeout); // 加载完成开始绘制

      self.mapUtil = mapUtil;
      console.log('初始化地图完成');
      callback(mapUtil);
    }, false); // 超时处理

    if (self.loadTimeout) window.clearTimeout(self.loadTimeout);
    self.loadTimeout = setTimeout(function () {
      Bridge.hideLoading();
      callback(locale('初始化地图超时, 请检查当前网络是否稳定', 'hint_map_init_timeout'));
    }, 20000);
  }; // 标记: 绘制全部标记


  s.drawMarkers = function (points) {
    var self = this;

    if (!points || !points.length) {
      console.error('绘制标记: 定位坐标参数不能为空');
      return null;
    }

    if (self.abort) return;

    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawMarkers(points);
      }, 500);
      return;
    } // 绘制标记


    if (self.markers) {
      self.mapUtil.clearOverlays(self.markers);
    }

    self.markers = [];

    for (var _iterator = _createForOfIteratorHelperLoose(points), _step; !(_step = _iterator()).done;) {
      var point = _step.value;
      var marker = self.drawMarker(point);
      self.markers.push(marker);
    }

    setTimeout(function () {
      var bdPoints = [];

      for (var _iterator2 = _createForOfIteratorHelperLoose(self.markers), _step2; !(_step2 = _iterator2()).done;) {
        var marker = _step2.value;
        bdPoints.push(marker.getPosition());
      }

      self.mapUtil.centerToPoints(bdPoints);
      console.log('绘制标记完成');
    }, 500);
    return self.markers;
  }; // 标记: 绘制标记, 更新原marker, 则传入marker


  s.drawMarker = function (point, marker) {
    var self = this;
    self.marker = marker; // 国测局转百度坐标

    var bdPoint = GeoUtil.coordtransform(point, 'gcj02', 'bd09');
    bdPoint = self.mapUtil.pointToBdPoint(bdPoint); // eslint-disable-line

    if (!bdPoint) {
      console.error('绘制标记: 定位坐标错误');
      return null;
    }

    if (!self.marker) {
      self.marker = self.mapUtil.drawMarker(bdPoint, {
        options: {
          iconStyle: {
            width: '16px',
            height: '27px',
            backgroundImage: "url(//res.waiqin365.com/d/seedsui/mapview/location_red_shadow.png)"
          }
        }
      });
    } else {
      self.marker.setPosition(bdPoint);
    }

    return self.marker;
  }; // 圆形: 绘制标签


  s.drawLabel = function (point, radius) {
    var self = this;
    if (self.abort) return;

    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawLabel(point, radius);
      }, 500);
      return;
    }

    var bdPoint = GeoUtil.coordtransform(point);
    bdPoint = this.mapUtil.pointToBdPoint(bdPoint);

    if (!self.label) {
      self.label = self.mapUtil.drawLabel(bdPoint, locale("\u534A\u5F84" + radius + "\u7C73", 'radius_of_m', [radius]), {
        offset: {
          width: 0,
          height: 14
        },
        style: {
          pointerEvents: 'none'
        }
      });
    } else {
      self.label.setPosition(bdPoint);
      self.label.setContent(locale("\u534A\u5F84" + Math.trunc(radius, 2) + "\u7C73", 'radius_of_m', [Math.trunc(radius, 2)]));
    }
  }; // 绘制圆形


  s.drawCircle = function (point, radius) {
    var self = this;
    if (self.abort) return;

    if (!point || point.length !== 2) {
      console.log("point\u53C2\u6570" + JSON.stringify(point) + "\u683C\u5F0F\u4E0D\u6B63\u786E, \u8BF7\u4F20\u5165[lng, lat]");
      return;
    }

    if (!radius) return;

    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawCircle(point, radius);
      }, 500);
      return;
    }

    var bdPoint = GeoUtil.coordtransform(point);
    bdPoint = this.mapUtil.pointToBdPoint(bdPoint);

    if (self.circle) {
      self.circle.setCenter(bdPoint);
    } else {
      self.circle = self.mapUtil.drawCircle(bdPoint, radius);
    }

    if (!self.circle) return;
    setTimeout(function () {
      self.mapUtil.centerToCircle(self.circle, {
        zoomFactor: -1
      });
    }, 300);
    console.log('绘制圆形完成');
    self.drawLabel(point, radius);
  }; // 绘制多边形


  s.drawPolygon = function (polygon) {
    var self = this;
    if (self.abort) return;
    if (!polygon || !polygon.length) return;

    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawPolygon(polygon);
      }, 500);
      return;
    }

    var bdPolygons = polygon.map(function (point) {
      return GeoUtil.coordtransform(point);
    });

    if (self.polygon) {
      self.mapUtil.clearOverlay(self.polygon);
    }

    self.polygon = self.mapUtil.drawPolygon(bdPolygons);
    self.mapUtil.centerToPoints(bdPolygons);
    console.log('绘制多边形完成');
  };
  /**
   * 地区
   */


  s.drawDistrict = function (districtName) {
    var self = this;
    if (self.abort) return;
    if (!districtName) return;

    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawDistrict(districtName);
      }, 500);
      return;
    }

    if (self.district) {
      self.mapUtil.clearOverlays(self.district);
    }

    self.mapUtil.drawBoundary(districtName, null, {
      success: function success(res) {
        self.district = res.bdPolygons;
        self.districtPolygons = res.polygons;
        self.mapUtil.centerToPoints(res.bdPolygonsPath);
        console.log('绘制区域完成');
      },
      fail: function fail(err) {
        Bridge.showToast(locale("\u6682\u65E0" + districtName + "\u7684\u8FB9\u754C\u6570\u636E", 'hint_map_no_boundary_data', [districtName]), {
          mask: false
        });
      }
    });
  };

  s.destroy = function () {
    var self = this;
    console.log('移除标注');
    self.mapUtil = null; // 标记

    self.markers = null; // 标签

    self.label = null; // 地区

    self.district = null;
    self.districtPolygons = null; // 多边形

    self.polygon = null; // 圆形

    self.circle = null;
  };
}