import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
/**
 * @文件说明: Providers.GDMap 高德Map数据源
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-23 19:41:09
 */

import hat from 'hat';
import assign from 'lodash/assign';
import { isBooleanFlase } from '../../_util/methods-util';

// GDMap地图图层服务源的类型
export var gdLayersTypes = ['vec', 'img'];

// GDMap地图的限制地图Map属性
export var gdMapOptions = {
  minZoom: 2,
  maxZoom: 17
};

// GDMap地图瓦片服务地址
var gdTilesUrls = {
  vec: [1, 2, 3, 4].map(function (key) {
    return 'http://webrd0' + key + '.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=1&style=8';
  }),
  img: {
    'default': [1, 2, 3, 4].map(function (key) {
      return 'https://wprd0' + key + '.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scl=2&style=6&ltype=11';
    }),
    labels: [1, 2, 3, 4].map(function (key) {
      return 'http://webst0' + key + '.is.autonavi.com/appmaptile?x={x}&y={y}&z={z}&lang=zh_cn&size=1&scale=1&style=8';
    })
  }
};

// 默认服务数据源的参数选项
var DEFAULT_OPTIONS = {
  // 是否配置地名文本标注
  labels: true
};

/**
 * 根据服务源的规则生成图层的数据源对象
 * @param {String} type 服务源类型
 * @param {Object} options 服务源的参数选项
 * @param {String} key 数据源的二级Key名
 */
var getGDSource = function getGDSource(type, options, key) {
  var source = {
    type: 'raster',
    tileSize: 256,
    minzoom: gdMapOptions.minZoom,
    maxzoom: gdMapOptions.maxZoom + 1
  };
  // 添加数据源地址
  switch (type) {
    case 'vec':
      {
        source.tiles = gdTilesUrls[type];
        break;
      }
    case 'img':
      {
        source.tiles = gdTilesUrls[type][key || 'default'];
        break;
      }
  }
  return source;
};

/**
 * 根据服务图层源生成对应的MapboxGL支持的Layer图层对象
 * @param {String} id 图层id
 * @param {String} layerName 图层名称
 * @param {Object} source 图层服务源
 */
var getGDLayer = function getGDLayer(id, layerName, source) {
  var prefix = 'providers_gdmap';
  return {
    id: prefix + '_' + layerName + '_' + id,
    type: 'raster',
    source: source,
    minzoom: gdMapOptions.minZoom,
    maxzoom: gdMapOptions.maxZoom + 1,
    metadata: {
      serviceType: 'XYZTile'
    }
  };
};

var GDMap = function () {
  function GDMap(iMapApi) {
    _classCallCheck(this, GDMap);

    this.iMapApi = iMapApi;
    this.id = hat();
  }

  /**
   * 获取GDMap地图服务源的图层对象
   * @param {Object} options 服务源的参数选项
   */


  _createClass(GDMap, [{
    key: 'getLayers',
    value: function getLayers() {
      var _this = this;

      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};

      var opts = assign({}, DEFAULT_OPTIONS, options);
      var layers = {};
      // 生成高德对应类型的图层集合
      gdLayersTypes.map(function (key) {
        layers[key] = [];
        // 基础底图
        var baseSource = getGDSource(key, opts);
        baseSource && layers[key].push(getGDLayer(_this.id, key, baseSource));
        // 中文标注瓦片
        if (key !== 'vec' && !isBooleanFlase(opts.labels)) {
          var labelsSource = getGDSource(key, opts, 'labels');
          labelsSource && layers[key].push(getGDLayer(_this.id, key + '_labels', labelsSource));
        }
      });
      return layers;
    }
  }]);

  return GDMap;
}();

export default GDMap;