"use strict";

exports.__esModule = true;
exports["default"] = _default;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _MapUtil = _interopRequireDefault(require("./../MapUtil"));

var _GeoUtil = _interopRequireDefault(require("./../GeoUtil"));

var _Bridge = _interopRequireDefault(require("./../Bridge"));

var _locale = _interopRequireDefault(require("./../locale"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

// 国际化
function _default() {
  var s = this;
  s.mapUtil = null; // 是否中断未完成的绘制

  s.abort = false; // 标记

  s.markers = null;
  s.tempMarker = 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["default"].showLoading();

    var mapUtil = new _MapUtil["default"](container, {
      // 缩放导航
      navigation: {
        position: 'bottom-right',
        type: 'zoom'
      },
      // 中心位置
      center: center
    });
    mapUtil.map.addEventListener('load', function (e) {
      _Bridge["default"].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 (e) {
        self.hideTempMarker();
        if (!self.marker) return; // 获取中心点, 并绘制坐标点, 获取的point为国测局坐标

        var point = self.getCenterPoint(self.marker); // 显示坐标点

        self.showMarker(); // 地址逆解析, point为国测局坐标

        self.getAddress(point, {
          success: function success(result) {
            if (self.onDragEnd) self.onDragEnd(result);
          },
          fail: function fail() {
            if (self.onDragEnd) self.onDragEnd(null);
          }
        });
      });
    }, false); // 超时处理

    if (self.loadTimeout) window.clearTimeout(self.loadTimeout);
    self.loadTimeout = setTimeout(function () {
      _Bridge["default"].hideLoading();

      callback((0, _locale["default"])('初始化地图超时, 请检查当前网络是否稳定', 'hint_map_init_timeout'));
    }, 20000);
  }; // 获取中心点


  s.getCenterPoint = function (marker) {
    var self = this;
    var bdPoint = self.mapUtil.map.getCenter();
    if (marker) marker.setPosition(bdPoint);
    var point = [bdPoint.lng, bdPoint.lat];
    return _GeoUtil["default"].coordtransform(point, 'bd09', 'gcj02');
  }; // 绘制临时标记, 拖拽显示, 停止拖拽隐藏


  s.drawTempMarker = function (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();
    }
  };

  s.showTempMarker = function () {
    var self = this;

    if (self.tempMarker) {
      self.tempMarker.classList.remove('hide');
    }
  };

  s.hideTempMarker = function () {
    var self = this;

    if (self.tempMarker) {
      self.tempMarker.classList.add('hide');
    }
  }; // 初始化标记, 并逆解析地址


  s.initMarker = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee(point, callback) {
      var self, result;
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              self = this;

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

              console.error('初始化标记: 定位坐标参数不能为空');
              return _context.abrupt("return", null);

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

              return _context.abrupt("return");

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

              setTimeout(function () {
                self.initMarker(point, callback);
              }, 500);
              return _context.abrupt("return");

            case 9:
              // 绘制坐标点
              self.drawMarker(point); // 地址逆解析

              _context.next = 12;
              return self.getAddress(point);

            case 12:
              result = _context.sent;
              if (callback) callback(result);

            case 14:
            case "end":
              return _context.stop();
          }
        }
      }, _callee, this);
    }));

    return function (_x, _x2) {
      return _ref.apply(this, arguments);
    };
  }(); // 绘制坐标点, point为国测局坐标


  s.drawMarker = function (point) {
    var self = this;

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

    if (self.abort) return;

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

    var bdPoint = _GeoUtil["default"].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);
    } // 只绘制一个定位到地图中心点


    setTimeout(function () {
      self.mapUtil.centerToPoints(bdPoint);
    }, 500);
  };

  s.hideMarker = function () {
    var self = this;

    if (self.marker) {
      self.marker.setOffset(new BMap.Size(10000, 10000));
    }
  };

  s.showMarker = function () {
    var self = this;

    if (self.marker) {
      self.marker.setOffset(new BMap.Size(0, 0));
    }
  }; // 地址逆解析


  s.getAddress = /*#__PURE__*/function () {
    var _ref2 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee3(point, params) {
      return _regenerator["default"].wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              if (params === void 0) {
                params = {};
              }

              return _context3.abrupt("return", new Promise( /*#__PURE__*/function () {
                var _ref3 = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee2(resolve) {
                  var result;
                  return _regenerator["default"].wrap(function _callee2$(_context2) {
                    while (1) {
                      switch (_context2.prev = _context2.next) {
                        case 0:
                          _context2.next = 2;
                          return _Bridge["default"].getAddress(_extends({
                            // 只支持gcj02
                            latitude: point[1],
                            longitude: point[0]
                          }, params || {}));

                        case 2:
                          result = _context2.sent;
                          resolve(result);

                        case 4:
                        case "end":
                          return _context2.stop();
                      }
                    }
                  }, _callee2);
                }));

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

            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }));

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

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

    self.markers = null;
    self.marker = null;
  };
}

module.exports = exports.default;