'use strict';

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

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

var _assign2 = _interopRequireDefault(_assign);

var _find = require('lodash/find');

var _find2 = _interopRequireDefault(_find);

var _remove = require('lodash/remove');

var _remove2 = _interopRequireDefault(_remove);

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

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

/**
 * @文件说明: 构建Map地图的“图层组”管理函数方法
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-03-21 14:42:52
 */

var layerGroup = {
  /**
   * 判断指定的图层(组)Id名称检查是否为图层组
   * @param {String} id 待检测的图层组的Id名称
   */
  isLayerGroup: function isLayerGroup(id) {
    var layerGroup = this.getLayer(id);
    return !!(layerGroup && (0, _methodsUtil.isBooleanTrue)(layerGroup.isLayerGroup));
  },


  /**
   * 获取指定图层组对象的图层Id集合
   * @param {String} id 待检测的图层组的Id名称
   */
  getLayerGroupByIds: function getLayerGroupByIds(id) {
    if (this.isLayerGroup(id)) {
      var _layerGroup = this.getLayer(id);
      return _layerGroup.layers.map(function (layer) {
        return layer.id;
      });
    }
    return null;
  },


  /**
   * 将MapboxGL Layer图层集合添加到指定的图层组对象，并追加到地图Map中
   * @param {String} id 图层组的Id名称
   * @param {Object} layer 图层对象
   * @param {String} beforeId 待添加到指定图层Id之前
   */
  addLayerToGroup: function addLayerToGroup(id, layer) {
    var _this = this;

    var beforeId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

    var map = this.map;

    // 特殊处理矢量瓦片VTS服务图层
    if (layer.type && layer.type === 'VTS') {
      if (layer.layers && layer.layers.length) {
        this.addSource(layer.id, layer.source);
        // 添加矢量瓦片VTS图层Sprite属性
        this.loadSprite(layer.layers[0].metadata.sprite);
        // 添加矢量瓦片VTS图层Glyphs属性
        this.loadGlyphs(layer.layers[0].metadata.glyphs);
        return this.addLayersToGroup(id, layer.layers, beforeId);
      }
      return null;
    } else {
      // 添加图层
      var addMapLayer = function addMapLayer(currentLayer, currentBeforeId) {
        // 添加数据源
        var currentSource = null;
        if (currentLayer.source) {
          if (typeof currentLayer.source !== 'string') {
            var currentSourceId = currentLayer.source && currentLayer.source.id ? currentLayer.source.id : currentLayer.id;
            currentSource = _this.addSource(currentSourceId, currentLayer.source);
            currentLayer.source = currentSource.id;
          } else {
            currentSource = _this.getSource(currentLayer.source);
          }
        }
        // 添加图层
        if (currentBeforeId) {
          map.addLayer(currentLayer, currentBeforeId);
        } else {
          map.addLayer(currentLayer);
        }
        if (currentSource) {
          // 存储数据源的所属图层Id
          !currentSource._layersIds && (currentSource._layersIds = []);
          currentSource._layersIds.indexOf(currentLayer.id) === -1 && currentSource._layersIds.push(currentLayer.id);
        }
        return map.getLayer(currentLayer.id);
      };

      var groupLayer = (0, _assign2['default'])({}, layer, {
        metadata: (0, _assign2['default'])({}, layer.metadata || {}, { group: id })
      });
      var _layerGroup2 = this.getLayer(id);
      // 判断是否未创建图层组对象集合
      if (!_layerGroup2) {
        var currentLayer = addMapLayer(groupLayer, beforeId);
        var currentLayersGroup = {
          id: id,
          isLayerGroup: true,
          layers: [currentLayer]
        };
        // 存储当前图层对象
        this._mapLayers[id] = currentLayersGroup;
        this._mapLayersIds.push({
          layerGroupId: id,
          layersIds: [currentLayer.id]
        });
        return this._mapLayers[id];
      } else {
        // 判断待追加的图层是否还未创建到地图Map对象中，则追加到图层组
        if (!this.getLayer(groupLayer.id, id) && !map.getLayer(groupLayer.id)) {
          // 判断指定图层(组)Id之前是否为该图层组的图层Id，否则只能追加图层组最后
          var beforeLayer = this.getLayer(beforeId, id);
          if (beforeLayer) {
            var _currentLayer = addMapLayer(groupLayer, beforeId);
            // 存储当前图层对象
            var mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
            var index = mapLayersIdsItem.layersIds.indexOf(beforeId);
            mapLayersIdsItem.layersIds.splice(index, 0, _currentLayer.id);
            this._mapLayers[id].layers.splice(index, 0, _currentLayer);
          } else {
            // 获取图层组的下一个图层，重新计算beforeId
            var newBeforeId = this.getNextLayerId(id);
            // 添加图层到地图
            var _currentLayer2 = addMapLayer(groupLayer, newBeforeId);
            // 存储当前图层对象
            this._mapLayers[id].layers.push(_currentLayer2);
            var _mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
            _mapLayersIdsItem.layersIds.push(_currentLayer2.id);
          }
          return this._mapLayers[id];
        } else {
          console.error('\u56FE\u5C42[' + layer.id + ']\u5DF2\u5B58\u5728\uFF0C\u65E0\u6CD5\u8FFD\u52A0\u5230\u56FE\u5C42\u7EC4\u5BF9\u8C61\u96C6\u5408.');
          return null;
        }
      }
    }
  },


  /**
   * 将MapboxGL Layer图层对象添加到指定的图层组对象，并追加到地图Map中
   * @param {String} id 图层组的Id名称
   * @param {Array} layers 图层数组集合
   * @param {String} beforeId 待添加到指定图层Id之前
   */
  addLayersToGroup: function addLayersToGroup(id, layers) {
    var _this2 = this;

    var beforeId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;

    if (!(0, _methodsUtil.isNotEmptyArray)(layers)) {
      console.error('待添加的图层集合不是一个有效的数组对象.');
      return;
    }
    layers.map(function (layer) {
      _this2.addLayerToGroup(id, layer, beforeId);
    });
    return this._mapLayers[id];
  },


  /**
   * 移除图层组内指定Id的图层对象
   * @param {String} id 图层组的Id名称
   * @param {String} layerId 图层Id名称
   */
  removeLayerToGroup: function removeLayerToGroup(id, layerId) {
    if (this.isLayerGroup(id)) {
      var layer = this.getLayer(layerId, id);
      if (layer) {
        // 移除图层的绑定事件
        layer.eventIds && this.off(layer.eventIds);
        // 移除图层和数据源
        this.map.removeLayer(layer.id);
        this.removeSource(layer.source);
        // 移除图层组中的图层对象的缓存
        this._mapLayers[id].layers.splice(this._mapLayers[id].layers.indexOf(layer), 1);
        if (!this._mapLayers[id].layers.length) {
          delete this._mapLayers[id];
        }
        var mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
        mapLayersIdsItem.layersIds.splice(mapLayersIdsItem.layersIds.indexOf(layerId), 1);
        if (!mapLayersIdsItem.layersIds.length) {
          (0, _remove2['default'])(this._mapLayersIds, function (item) {
            return !!(item.layerGroupId && item.layerGroupId === id);
          });
        }
      }
    } else {
      console.error('\u56FE\u5C42\u7EC4Id\uFF1A[' + id + ']\u5BF9\u8C61\u4E0D\u5B58\u5728\u6216\u56FE\u5C42\u5BF9\u8C61\u8BF7\u4F7F\u7528[removeLayer]\u51FD\u6570\u65B9\u6CD5.');
    }
  },


  /**
   * 移除图层组内指定Id数组集合的图层对象
   * @param {String} id 图层组的Id名称
   * @param {Array} layersId 图层Id数组集合
   */
  removeLayersToGroup: function removeLayersToGroup(id, layersId) {
    if ((0, _methodsUtil.isArray)(layersId)) {
      for (var i = layersId.length - 1; i >= 0; i--) {
        this.removeLayerToGroup(id, layersId[i]);
      }
    } else {
      console.log('\u56FE\u5C42Id\u96C6\u5408\u5FC5\u987B\u662F\u4E00\u4E2A\'String[]\'\u5B57\u7B26\u578B\u6570\u7EC4\u683C\u5F0F.');
    }
  },


  /**
   * 获取指定Id的图层组的第一个图层对象
   * @param {String} id 图层组的Id名称
   */
  getGroupFirstLayer: function getGroupFirstLayer(id) {
    if (this.isLayerGroup(id)) {
      var _layerGroup3 = this.getLayer(id);
      if (_layerGroup3 && _layerGroup3.layers && _layerGroup3.layers[0]) {
        return _layerGroup3.layers[0];
      }
    }
    return null;
  },


  /**
   * 获取指定Id的图层组的最后一个图层对象
   * @param {String} id 图层组的Id名称
   */
  getGroupLastLayer: function getGroupLastLayer(id) {
    if (this.isLayerGroup(id)) {
      var _layerGroup4 = this.getLayer(id);
      if (_layerGroup4 && _layerGroup4.layers && _layerGroup4.layers.length) {
        return _layerGroup4.layers[_layerGroup4.layers.length - 1];
      }
    }
    return null;
  },


  /**
   * 在图层组内移动指定Id的图层到指定beforeId图层之前
   * @param {String} id 图层组的Id名称
   * @param {String} layerId 图层Id名称
   * @param {String} beforeId 待移动到指定图层Id之前
   */
  moveLayerToGroup: function moveLayerToGroup(id, layerId, beforeId) {
    // 判断是否指定移动前置图层Id，如果无指定则移动到图层组的最底部
    if (!beforeId) {
      var mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
      var layerIndex = mapLayersIdsItem.layersIds.indexOf(layerId);
      var lastIndex = mapLayersIdsItem.layersIds.length - 1;
      // 查询该图层组的下一个图层Id
      var newBeforeId = this.getNextLayerId(id);
      // 移动图层顺序
      if (newBeforeId) {
        this.map.moveLayer(layerId, newBeforeId);
      } else {
        this.map.moveLayer(layerId);
      }
      // 调整图层组中的图层对象的缓存的顺序
      this._mapLayers[id].layers.splice(lastIndex + 1, 0, this._mapLayers[id].layers[layerIndex]);
      mapLayersIdsItem.layersIds.splice(lastIndex + 1, 0, mapLayersIdsItem.layersIds[layerIndex]);
      this._mapLayers[id].layers.splice(layerIndex, 1);
      mapLayersIdsItem.layersIds.splice(layerIndex, 1);
    } else {
      var beforeLayer = this.getLayer(beforeId, id);
      if (beforeLayer) {
        // 判断是否指定图层待移动图层是否同一个，则直接跳过
        if (layerId !== beforeId) {
          var _mapLayersIdsItem2 = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
          var beforeIndex = _mapLayersIdsItem2.layersIds.indexOf(beforeId);
          var _layerIndex = _mapLayersIdsItem2.layersIds.indexOf(layerId);
          // 移动图层顺序
          this.map.moveLayer(layerId, beforeId);
          // 调整图层组中的图层对象的缓存的顺序
          this._mapLayers[id].layers.splice(beforeIndex, 0, this._mapLayers[id].layers[_layerIndex]);
          _mapLayersIdsItem2.layersIds.splice(beforeIndex, 0, _mapLayersIdsItem2.layersIds[_layerIndex]);
          this._mapLayers[id].layers.splice(_layerIndex < beforeIndex ? _layerIndex : _layerIndex + 1, 1);
          _mapLayersIdsItem2.layersIds.splice(_layerIndex < beforeIndex ? _layerIndex : _layerIndex + 1, 1);
        }
      } else {
        console.error('\u5F85\u79FB\u52A8\u5230\u6307\u5B9A\u7684\u56FE\u5C42beforeId\uFF1A[' + beforeId + ']\u4E0D\u662F\u56FE\u5C42\u7EC4\u5185\u7684\u56FE\u5C42.');
      }
    }
  },


  /**
   * 在图层组内向上移动一层指定Id的图层
   * @param {String} id 图层组的Id名称
   * @param {String} layerId 图层Id名称
   */
  moveUpLayerToGroup: function moveUpLayerToGroup(id, layerId) {
    var layer = this.getLayer(layerId, id);
    if (layer) {
      var firstLayer = this.getGroupFirstLayer(id);
      // 判断当前图层是否不是组内顶级图层
      if (firstLayer.id !== layerId) {
        var mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
        var layerIndex = mapLayersIdsItem.layersIds.indexOf(layerId);
        var beforeLayer = this._mapLayers[id].layers[layerIndex - 1];
        this.moveLayerToGroup(id, layerId, beforeLayer.id);
      }
    } else {
      console.error('\u5F85\u5411\u4E0A\u79FB\u52A8\u7684\u56FE\u5C42id\uFF1A[' + layerId + ']\u4E0D\u662F\u56FE\u5C42\u7EC4\u5185\u7684\u56FE\u5C42.');
    }
  },


  /**
   * 在图层组内向下移动一层指定Id的图层
   * @param {String} id 图层组的Id名称
   * @param {String} layerId 图层Id名称
   */
  moveDownLayerToGroup: function moveDownLayerToGroup(id, layerId) {
    var layer = this.getLayer(layerId, id);
    if (layer) {
      var lastLayer = this.getGroupLastLayer(id);
      // 判断当前图层是否不是组内顶级图层
      if (lastLayer.id !== layerId) {
        var mapLayersIdsItem = (0, _find2['default'])(this._mapLayersIds, { layerGroupId: id });
        var layerIndex = mapLayersIdsItem.layersIds.indexOf(layerId);
        var beforeLayer = this._mapLayers[id].layers[layerIndex + 2] || null;
        this.moveLayerToGroup(id, layerId, beforeLayer ? beforeLayer.id : null);
      }
    } else {
      console.error('\u5F85\u5411\u4E0B\u79FB\u52A8\u7684\u56FE\u5C42id\uFF1A[' + layerId + ']\u4E0D\u662F\u56FE\u5C42\u7EC4\u5185\u7684\u56FE\u5C42.');
    }
  }
};

exports['default'] = layerGroup;