import _regeneratorRuntime from 'babel-runtime/regenerator';

function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }

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

export default {
  mapUtil: null,
  // 是否中断未完成的绘制
  abort: false,
  // 标记
  markers: null,
  tempMarker: null,
  marker: null,
  // 标签
  label: null,
  // 地区
  district: null,
  districtPolygons: null,
  // 多边形
  polygon: null,
  // 圆形
  circle: null,
  // 初始化地图
  initMap: function initMap(container, center, callback) {
    var _this = this;

    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);
      self.drawTempMarker(false);
      // 拖拽点
      mapUtil.map.addEventListener('dragstart', function () {
        if (self.tempMarker) {
          self.tempMarker.classList.remove('hide');
        }
        if (self.marker) {
          self.marker.setOffset(new BMap.Size(10000, 10000));
        }
      });
      mapUtil.map.addEventListener('dragend', function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) {
          var bdPoint, point, result;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  if (self.tempMarker) {
                    self.tempMarker.classList.add('hide');
                  }

                  if (self.marker) {
                    _context.next = 3;
                    break;
                  }

                  return _context.abrupt('return');

                case 3:
                  self.marker.setOffset(new BMap.Size(0, 0));
                  bdPoint = mapUtil.map.getCenter();

                  self.marker.setPosition(bdPoint);
                  point = [bdPoint.lng, bdPoint.lat];

                  point = GeoUtil.coordtransform(point, 'bd09', 'gcj02');
                  _context.next = 10;
                  return Bridge.getAddress({ // 只支持gcj02
                    latitude: point[1],
                    longitude: point[0]
                  });

                case 10:
                  result = _context.sent;

                  // const address = result && result.address ? result.address : ''
                  if (self.onDragEnd) {
                    self.onDragEnd(result);
                  }

                case 12:
                case 'end':
                  return _context.stop();
              }
            }
          }, _callee, _this);
        }));

        return function (_x) {
          return _ref.apply(this, arguments);
        };
      }());
    }, 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.mapUtil.clearOverlays(self.markers);
    }
    self.markers = [];
    for (var _iterator = points, _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 point = _ref2;

      var marker = self.drawMarker(point);
      self.markers.push(marker);
    }
    setTimeout(function () {
      var bdPoints = [];
      for (var _iterator2 = self.markers, _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 marker = _ref3;

        bdPoints.push(marker.getPosition());
      }
      self.mapUtil.centerToPoints(bdPoints);
      console.log('绘制标记完成');
    }, 500);
    return self.markers;
  },
  // 绘制临时标记, 用于拖动
  drawTempMarker: function drawTempMarker(show) {
    var self = this;
    if (!self.tempMarker) {
      self.tempMarker = document.createElement('span');
      self.tempMarker.className = 'map-marker-center';
      self.mapUtil.container.appendChild(self.tempMarker);
    }
    if (show) {
      self.tempMarker.classList.remove('hide');
    } else {
      self.tempMarker.classList.add('hide');
    }
  },
  // 标记: 绘制标记, 更新原marker, 则传入marker
  initMarker: function () {
    var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(point) {
      var self, bdPoint, gcjPoint, result;
      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              self = this;

              if (point) {
                _context2.next = 4;
                break;
              }

              console.error('绘制标记: 定位坐标参数不能为空');
              return _context2.abrupt('return', null);

            case 4:
              if (!self.abort) {
                _context2.next = 6;
                break;
              }

              return _context2.abrupt('return');

            case 6:
              if (self.mapUtil) {
                _context2.next = 9;
                break;
              }

              setTimeout(function () {
                self.initMarker(point);
              }, 500);
              return _context2.abrupt('return');

            case 9:
              bdPoint = GeoUtil.coordtransform(point);

              bdPoint = self.mapUtil.pointToBdPoint(bdPoint); // eslint-disable-line

              if (bdPoint) {
                _context2.next = 14;
                break;
              }

              console.error('绘制标记: 定位坐标错误');
              return _context2.abrupt('return', null);

            case 14:
              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);
              }
              // 只绘制一个定位到地图中心点
              self.mapUtil.centerToPoints(bdPoint);
              // 地址逆解析
              gcjPoint = [bdPoint.lng, bdPoint.lat];

              gcjPoint = GeoUtil.coordtransform(point, 'bd09', 'gcj02');
              _context2.next = 20;
              return Bridge.getAddress({ // 只支持gcj02
                latitude: gcjPoint[1],
                longitude: gcjPoint[0]
              });

            case 20:
              result = _context2.sent;

              // const address = result && result.address ? result.address : ''
              if (self.onDragEnd) {
                self.onDragEnd(result);
              }
              return _context2.abrupt('return', self.marker);

            case 23:
            case 'end':
              return _context2.stop();
          }
        }
      }, _callee2, this);
    }));

    function initMarker(_x2) {
      return _ref4.apply(this, arguments);
    }

    return initMarker;
  }(),
  destroy: function destroy() {
    var self = this;
    console.log('移除标注');
    self.mapUtil = null;
    // 标记
    self.markers = null;
    self.marker = null;
  }
};