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

export default {
  mapUtil: null,
  // 是否中断未完成的绘制
  abort: false,
  // 标记
  markers: null,
  marker: null,
  // 标签
  label: null,
  // 地区
  district: null,
  districtPolygons: null,
  // 多边形
  polygon: null,
  // 圆形
  circle: null,
  // 初始化地图
  initMap: function initMap(container, center, callback) {
    var self = this;
    Bridge.showLoading();
    var mapUtil = new MapUtil(container, {
      // 缩放导航
      navigation: {
        position: 'bottom-right',
        type: 'zoom'
      },
      // 中心位置
      center: {
        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);
  },
  // 标记: 绘制全部标记
  drawMarkers: function drawMarkers(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.markers.length === points.length) {
      // 更新标记
      for (var _iterator = points.entries(), _isArray = Array.isArray(_iterator), _i = 0, _iterator = _isArray ? _iterator : _iterator[Symbol.iterator]();;) {
        var _ref2;

        if (_isArray) {
          if (_i >= _iterator.length) break;
          _ref2 = _iterator[_i++];
        } else {
          _i = _iterator.next();
          if (_i.done) break;
          _ref2 = _i.value;
        }

        var _ref = _ref2;
        var index = _ref[0];
        var point = _ref[1];

        self.drawMarker(point, self.markers[index]);
      }
    } else {
      // 绘制标记
      if (self.markers) {
        self.mapUtil.clearOverlays(self.markers);
      }
      self.markers = [];
      for (var _iterator2 = points, _isArray2 = Array.isArray(_iterator2), _i2 = 0, _iterator2 = _isArray2 ? _iterator2 : _iterator2[Symbol.iterator]();;) {
        var _ref3;

        if (_isArray2) {
          if (_i2 >= _iterator2.length) break;
          _ref3 = _iterator2[_i2++];
        } else {
          _i2 = _iterator2.next();
          if (_i2.done) break;
          _ref3 = _i2.value;
        }

        var _point = _ref3;

        var marker = self.drawMarker(_point);
        self.markers.push(marker);
      }
    }
    setTimeout(function () {
      var bdPoints = [];
      for (var _iterator3 = self.markers, _isArray3 = Array.isArray(_iterator3), _i3 = 0, _iterator3 = _isArray3 ? _iterator3 : _iterator3[Symbol.iterator]();;) {
        var _ref4;

        if (_isArray3) {
          if (_i3 >= _iterator3.length) break;
          _ref4 = _iterator3[_i3++];
        } else {
          _i3 = _iterator3.next();
          if (_i3.done) break;
          _ref4 = _i3.value;
        }

        var _marker = _ref4;

        bdPoints.push(_marker.getPosition());
      }
      self.mapUtil.centerToPoints(bdPoints);
      console.log('绘制标记完成');
    }, 500);
    return self.markers;
  },
  // 标记: 绘制标记, 更新原marker, 则传入marker
  drawMarker: function drawMarker(point, marker) {
    var self = this;
    self.marker = marker;
    var bdPoint = GeoUtil.coordtransform(point);
    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;
  },
  // 圆形: 绘制标签
  drawLabel: function drawLabel(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)]));
    }
  },
  // 绘制圆形
  drawCircle: function drawCircle(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);
  },
  // 绘制多边形
  drawPolygon: function drawPolygon(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('绘制多边形完成');
  },
  /**
   * 地区
   */
  drawDistrict: function drawDistrict(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 });
      }
    });
  },
  destroy: function destroy() {
    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;
  }
};