'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _regenerator = require('babel-runtime/regenerator');

var _regenerator2 = _interopRequireDefault(_regenerator);

var _asyncToGenerator2 = require('babel-runtime/helpers/asyncToGenerator');

var _asyncToGenerator3 = _interopRequireDefault(_asyncToGenerator2);

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _assign = require('lodash/assign');

var _assign2 = _interopRequireDefault(_assign);

var _methodsUtil = require('../../../_util/methods-util');

var _index = require('./index');

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

/**
 * 解析GeoTile类型的服务地址获取图层服务数据信息
 */
var fetchGeoTileCapabilities = function fetchGeoTileCapabilities(own, url, options) {
  var GeoGlobe = own.exports.GeoGlobe;

  var isProxyUrl = (0, _methodsUtil.isBooleanFlase)(options.proxy) ? false : true;

  return new Promise(function (resolve, reject) {
    var capabilitiesUrl = url;
    // 处理吉奥瓦片服务的地址
    var urlParams = url.indexOf('?') !== -1 ? url.split('?')[1] : '';
    if (url.indexOf('/DataServer') !== -1) {
      capabilitiesUrl = url.replace(/\/DataServer/g, '/services/tile/GetCapabilities');
    } else if (url.indexOf('/services/tile') !== -1) {
      capabilitiesUrl = url + '/GetCapabilities';
    }
    isProxyUrl && (capabilitiesUrl = '' + own.proxyURL + capabilitiesUrl);
    urlParams && (capabilitiesUrl = capabilitiesUrl + '?' + urlParams);

    // 请求图层数据信息
    var errorMsg = '\u5409\u5965\u74E6\u7247GeoTile\u670D\u52A1\u5730\u5740[ ' + url + ' ]\u6570\u636E\u89E3\u6790\u5931\u8D25.';
    GeoGlobe.Request.GET({
      url: capabilitiesUrl,
      success: function success(data) {
        var opts = {};
        try {
          var capabilities = new GeoGlobe.Format.XML().read(data.responseText);
          if (capabilities) {
            var tileData = capabilities.querySelector('Data').querySelector('TileData');
            // 图层标识
            opts.layerName = capabilities.querySelector('Name').textContent;
            // 版本号
            opts.version = capabilities.querySelector('Version').textContent;
            // 瓦片大小
            var tileSize = tileData.querySelector('TilePixelsX').textContent;
            opts.tileSize = tileSize || 256;
            // CRS
            opts.crs = tileData.querySelector('CRS').textContent;
            // 矩形范围
            var bounding = tileData.querySelector('BoundBox');
            opts.tileBBox = [bounding.getAttribute('minx'), bounding.getAttribute('miny'), bounding.getAttribute('maxx'), bounding.getAttribute('maxy')].join(',');
            // 层级
            opts.minZoom = parseInt(tileData.querySelector('TopLevel').textContent, 10);
            opts.maxZoom = parseInt(tileData.querySelector('BottomLevel').textContent, 10);
            // 金字塔顶级范围
            var topTile = tileData.querySelector('Pyramid').querySelector('TopTile');
            opts.topTileExtent = [Number(topTile.getAttribute('FromX')), Number(topTile.getAttribute('ToY')), Number(topTile.getAttribute('ToX')), Number(topTile.getAttribute('FromY'))];
          }
        } catch (e) {
          console.error(errorMsg);
          reject();
        }
        resolve(opts);
      },
      failure: function failure() {
        console.error(errorMsg);
        reject();
      }
    });
  });
};

/**
 * 解析GeoTile类型的服务地址获取图层样式信息
 */
/**
 * @文件说明: Services.GeoTile 吉奥瓦片服务
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-25 12:13:14
 */

var fetchGeoTileLayerStyles = function fetchGeoTileLayerStyles(own, id, url, layerOptions, options) {
  var isProxyUrl = (0, _methodsUtil.isBooleanFlase)(options.proxy) ? false : true;
  // 构建数据源
  var source = (0, _index.getServicesLayerSource)(options);
  source.tileSize = options.tileSize || layerOptions.tileSize || source.defaultTileSize;
  // 拼接数据源瓦片地址
  var urlParams = url.indexOf('?') !== -1 ? url.split('?')[1] : '';
  var tileUrl = url;
  if (tileUrl.indexOf('/services/tile') !== -1) {
    tileUrl = tileUrl.replace(/\/services\/tile/g, '/DataServer');
  }
  tileUrl += '?T=tile&X={x}&Y={y}&L={z}';
  urlParams && (tileUrl += '&' + urlParams);
  isProxyUrl && (tileUrl = '' + own.proxyURL + tileUrl);
  source.tiles = [tileUrl];
  // 构建图层
  var layer = (0, _index.getServicesBaseLayer)(options);
  layer.id = id;
  layer.source = source;
  layer.minzoom = !layer.minzoom || layer.minzoom < layerOptions.minZoom ? layerOptions.minZoom : layer.minzoom;
  layer.maxzoom = !layer.maxzoom || layer.maxzoom > layerOptions.maxZoom + 1 ? layerOptions.maxZoom + 1 : layer.maxzoom;
  layer.metadata = (0, _assign2['default'])({}, layer.metadata, (0, _extends3['default'])({
    serviceType: 'GeoTile',
    serviceName: options.name || ''
  }, layerOptions));
  if (layer.metadata.tileBBox) {
    var bbox = layer.metadata.tileBBox.split(',');
    layer.metadata.tileBBox = [[Number(bbox[0]), Number(bbox[1])], [Number(bbox[2]), Number(bbox[3])]];
  }
  return layer;
};

var GeoTile = function () {
  function GeoTile(iMapApi) {
    (0, _classCallCheck3['default'])(this, GeoTile);

    this.iMapApi = iMapApi;
    this.exports = iMapApi && iMapApi.exports ? iMapApi.exports : {};
    this.proxyURL = iMapApi.proxyURL || '';
    this.options = (0, _extends3['default'])({}, _index.defaultServicesSourceOptions, _index.defaultServicesLayerOptions);
  }

  /**
   * 获取解析GeoTile服务的图层对象
   * @param {String} id 图层Id名称
   * @param {String} name 图层Name名
   * @param {String} url 服务地址
   * @param {Object} options 解析服务的参数选项
   */


  (0, _createClass3['default'])(GeoTile, [{
    key: 'getLayer',
    value: function () {
      var _ref = (0, _asyncToGenerator3['default'])( /*#__PURE__*/_regenerator2['default'].mark(function _callee(id) {
        var name = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';
        var url = arguments[2];
        var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
        var opts, layerOptions;
        return _regenerator2['default'].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                opts = (0, _assign2['default'])(this.options, options);
                // 获取服务信息

                _context.next = 3;
                return fetchGeoTileCapabilities(this, url, opts);

              case 3:
                layerOptions = _context.sent;
                return _context.abrupt('return', fetchGeoTileLayerStyles(this, id, url, layerOptions, (0, _extends3['default'])({}, opts, { name: name })));

              case 5:
              case 'end':
                return _context.stop();
            }
          }
        }, _callee, this);
      }));

      function getLayer(_x3) {
        return _ref.apply(this, arguments);
      }

      return getLayer;
    }()
  }]);
  return GeoTile;
}();

exports['default'] = GeoTile;