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 () {
        self.showTempMarker();
        self.hideMarker();
      });
      mapUtil.map.addEventListener('dragend', function () {
        var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(e) {
          var point, result;
          return _regeneratorRuntime.wrap(function _callee$(_context) {
            while (1) {
              switch (_context.prev = _context.next) {
                case 0:
                  self.hideTempMarker();

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

                  return _context.abrupt('return');

                case 3:
                  // 获取中心点, 并绘制坐标点
                  point = self.getCenterPoint(self.marker);
                  // 显示坐标点

                  self.showMarker();
                  // 地址逆解析
                  _context.next = 7;
                  return self.getAddress(point);

                case 7:
                  result = _context.sent;

                  if (self.onDragEnd) {
                    self.onDragEnd(result);
                  }

                case 9:
                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);
  },
  // 获取中心点
  getCenterPoint: function getCenterPoint(marker) {
    var self = this;
    var bdPoint = self.mapUtil.map.getCenter();
    if (marker) marker.setPosition(bdPoint);
    var point = [bdPoint.lng, bdPoint.lat];
    return GeoUtil.coordtransform(point, 'bd09', 'gcj02');
  },
  // 绘制临时标记, 拖拽显示, 停止拖拽隐藏
  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.showTempMarker();
    } else {
      self.hideTempMarker();
    }
  },
  showTempMarker: function showTempMarker() {
    var self = this;
    if (self.tempMarker) {
      self.tempMarker.classList.remove('hide');
    }
  },
  hideTempMarker: function hideTempMarker() {
    var self = this;
    if (self.tempMarker) {
      self.tempMarker.classList.add('hide');
    }
  },
  // 初始化标记, 并逆解析地址
  initMarker: function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(point, callback) {
      var self, 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, callback);
              }, 500);
              return _context2.abrupt('return');

            case 9:

              // 绘制坐标点
              self.drawMarker(point);

              // 地址逆解析
              _context2.next = 12;
              return self.getAddress(point);

            case 12:
              result = _context2.sent;

              if (callback) callback(result);

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

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

    return initMarker;
  }(),
  // 绘制坐标点
  drawMarker: function drawMarker(point) {
    var self = this;
    if (!point) {
      debugger;
      console.error('绘制标记: 定位坐标参数不能为空');
      return null;
    }
    if (self.abort) return;
    if (!self.mapUtil) {
      setTimeout(function () {
        self.drawMarker(point);
      }, 500);
      return;
    }
    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);
    }
    // 只绘制一个定位到地图中心点
    self.mapUtil.centerToPoints(bdPoint);
  },
  hideMarker: function hideMarker() {
    var self = this;
    if (self.marker) {
      self.marker.setOffset(new BMap.Size(10000, 10000));
    }
  },
  showMarker: function showMarker() {
    var self = this;
    if (self.marker) {
      self.marker.setOffset(new BMap.Size(0, 0));
    }
  },
  // 地址逆解析
  getAddress: function () {
    var _ref3 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(point) {
      var _this2 = this;

      return _regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              return _context4.abrupt('return', new Promise(function () {
                var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3(resolve) {
                  var result;
                  return _regeneratorRuntime.wrap(function _callee3$(_context3) {
                    while (1) {
                      switch (_context3.prev = _context3.next) {
                        case 0:
                          _context3.next = 2;
                          return Bridge.getAddress({ // 只支持gcj02
                            latitude: point[1],
                            longitude: point[0]
                          });

                        case 2:
                          result = _context3.sent;

                          resolve(result);

                        case 4:
                        case 'end':
                          return _context3.stop();
                      }
                    }
                  }, _callee3, _this2);
                }));

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

            case 1:
            case 'end':
              return _context4.stop();
          }
        }
      }, _callee4, this);
    }));

    function getAddress(_x4) {
      return _ref3.apply(this, arguments);
    }

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