import DataUtils from '../utils/DataUtils';
import VerifyUtils from '../utils/VerifyUtils';
import MapProvider from '../geog/MapProvider';
import LayerProvider from '../geog/LayerProvider';
import GeoJsonProvider from '../geog/GeoJsonProvider';

/**
 * 定义简单圆图层
 * @alias SimpleCircleLayer
 *
 * @summary <用于点位图层加载>
 */
export default class SimpleCircleLayer {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    // 默认数据源ID
    this._defaultSourceId = 'circle_simple';
    // 数据源是否存在
    this._hasSource = true;

    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

    /**
     * 最小缩放层级
     * @type {Number}
     */
    this._minzoom = DataUtils.default(options.minzoom, 10);

    /**
     * 圆颜色
     * @type {String}
     */
    this._circleColor = DataUtils.default(options.color, 'green');

    /**
     * 圆半径
     * @type {Number}
     */
    this._circleRadius = DataUtils.default(options.radius, 2);

    /**
     * 最大圆半径
     * @type {Number}
     */
    this._maxCircleRadius = DataUtils.default(options.maxRadius, 8);

    /**
     * 填充不透明度
     * @type {Number}
     */
    this._circleOpacity = DataUtils.default(options.opacity, 0.75);

    /**
     * 是否有边框
     * @type {Boolean}
     */
    this._hasOutline = DataUtils.default(options.outline, false);

    /**
     * 线颜色
     * @type {String}
     */
    this._lineColor = DataUtils.default(options.lineColor, 'green');

    /**
     * 线宽
     * @type {Number}
     */
    this._lineWidth = DataUtils.default(options.lineWidth, 1.5);

    /**
     * 线不透明度
     * @type {Number}
     */
    this._lineOpacity = DataUtils.default(options.lineOpacity, 1.0);

    /**
     * 过滤条件数组
     * @type {Array}
     */
    this._filter = DataUtils.default(options.filter, undefined);

    /**
     * 图层服务地址
     * @type {String}
     */
    this._dataUrl = DataUtils.default(options.dataUrl, undefined);

    /**
     * 数据源ID
     * @type {String}
     */
    this._sourceId = DataUtils.default(options.sourceId, this._defaultSourceId);

    /**
     * 图层ID
     * @type {String}
     */
    this._layerId = DataUtils.default(options.layerId, this._sourceId);

    // 样式属性数组
    this._styleProperties = [];

    // 事件
    this._enterEvent = null;
    this._leaveEvent = null;
    this._clickEvent = null; // 外部实现单击事件
    // 初始化鼠标事件
    this._initEvent();
  }

  /** 公有方法 **/
  /**
   * 设置数据
   * @param {Object|String|Array} data -GeoJSON要素集/查询语句/过滤条件(点位)数组
   *
   * @summary <data参数：
   *   Object - GeoJson要素/要素集
   *   String - cql_filter查询语句
   *   Array - 图层过滤条件数组(如：['==', field, value]) or 点位数组(包含longitude, latitude字段)
   * >
   */
  setData(data) {
    // 加载图层
    if (!this.hasLayer()) {
      this.loadLayer();
    }
    // 隐藏图层
    this.showLayer(false);

    let fc = null;
    if (VerifyUtils.isGeoJsonFC(data)) {
      // GeoJSON要素集合
      fc = data;
    } else if (VerifyUtils.isGeoJsonFeature(data)) {
      // GeoJSON要素
      fc = GeoJsonProvider.getEmptyFC();
      fc.features = [data];
    } else if (VerifyUtils.isArray(data)) {
      if (data.length > 1 && VerifyUtils.isString(data[0])) {
        // 设置筛选器
        this._map.setFilter(this._layerId, data);
      } else {
        // 数组转GeoJSON对象
        fc = GeoJsonProvider.data2PointFC(data);
      }
    } else if (VerifyUtils.isString(data)) {
      // 查询语句
      if (this._hasSource) {
        const source = this._map.getSource(this._sourceId);
        fc = source._data + (data ? "&cql_filter=" + data : '');
      }
    }

    fc = fc || GeoJsonProvider.getEmptyFC();

    // 更新数据源
    this._map.getSource(this._sourceId).setData(fc);
    // 显示图层
    this.showLayer(true);
  }

  /**
   * 设置筛选器
   * @param {Array} filter -筛选器
   */
  setFilter(filter) {
    if (!this.hasLayer()) return;

    // 空数组
    if (VerifyUtils.isArray(filter) && filter.length === 0) {
      filter = null;
    }

    let newFilter = null;
    /*
    if (this._filter) {
      newFilter = ['all', this._filter];
    }
    */
    if (this._filter) {
      if (this._filter.length === 1) {
        newFilter = ['all', this._filter];
      } else {
        newFilter = [].concat(this._filter);
      }
    }

    if (filter) {
      if (!newFilter) {
        newFilter = filter;
      } else {
        newFilter.push(filter);
      }
    }

    // 设置筛选器
    this._map.setFilter(this._layerId, newFilter);
  }

  /**
   * 设置绘制属性
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setPaintProperty(name, value) {
    if (!this.hasLayer()) {
      this._styleProperties.push({ style: 'paint', name: name, value: value });
      return;
    }

    this._map.setPaintProperty(this._layerId, name, value);
  }

  /**
   * 设置布局属性
   * @param {String} name -属性名称
   * @param {String|Object} value -属性值
   */
  setLayoutProperty(name, value) {
    if (!this.hasLayer()) {
      this._styleProperties.push({ style: 'layout', name: name, value: value });
      return;
    }

    this._map.setLayoutProperty(this._layerId, name, value);
  }

  /**
   * 加载图层
   */
  loadLayer() {
    if (this.hasLayer()) return;

    const map = this._map;
    const minZoom = this._minzoom;
    const maxZoom = map.getMaxZoom() - 1;

    const sourceId = this._sourceId;
    const layerId = this._layerId;
    // 判断数据源是否存在
    this._hasSource = LayerProvider.hasSource(map, sourceId);
    if (!this._hasSource) {
      // 添加数据源
      map.addSource(sourceId, {
        type: "geojson",
        data: this._dataUrl || GeoJsonProvider.getEmptyFC()
      });
    }

    const radius = this._circleRadius;
    const maxRadius = this._maxCircleRadius;
    // 添加点位圆图层
    if (this._hasOutline) {
      // 有边框线
      map.addLayer({
        type: "circle",
        id: layerId,
        source: sourceId,
        minzoom: minZoom,
        layout: {
          visibility: "none",
        },
        paint: {
          "circle-color": this._circleColor,
          /*
          "circle-radius": {
            base: 4,
            stops: [
              [minZoom, radius],
              [maxZoom, maxRadius]
            ],
          },
          */
          "circle-radius": [
            "interpolate",
            ["linear"],
            ["zoom"],
            minZoom, radius,
            maxZoom, maxRadius
          ],
          "circle-opacity": this._circleOpacity,
          "circle-stroke-color": this._lineColor,
          "circle-stroke-width": this._lineWidth,
          "circle-stroke-opacity": this._lineOpacity
        }
      }, map._build3dLayerId);
    } else {
      // 无边框线
      map.addLayer({
        type: "circle",
        id: layerId,
        source: sourceId,
        minzoom: minZoom,
        layout: {
          visibility: "none",
        },
        paint: {
          "circle-color": this._circleColor,
          "circle-radius": {
            base: 4,
            stops: [
              [minZoom, radius],
              [maxZoom, maxRadius]
            ],
          },
          "circle-opacity": this._circleOpacity
        }
      }, map._build3dLayerId);
    }

    // 设置自定义样式
    if (this._styleProperties.length > 0) {
      this._styleProperties.forEach(prop => {
        switch (prop.style) {
          case 'paint':
            map.setPaintProperty(layerId, prop.name, prop.value);
            break;
          case 'layout':
            map.setLayoutProperty(layerId, prop.name, prop.value);
            break;
          default:
            break;
        }
      });
    }

    // 设置筛选器
    if (this._filter) {
      map.setFilter(this._layerId, this._filter);
    }

    // 绑定事件
    this._onEvent();
  }

  /**
   * 显示/隐藏图层
   * @param {Boolean} show -是否显示
   */
  showLayer(show) {
    LayerProvider.showLayer(this._map, this._layerId, show);
  }

  /**
   * 判断图层是否已加载
   *
   * @returns {Boolean}
   */
  hasLayer() {
    return LayerProvider.hasLayer(this._map, this._layerId);
  }

  /**
   * 移除图层
   */
  removeLayer() {
    // 解除事件绑定
    this.offEvent();

    LayerProvider.removeLayer(this._map, this._layerId);
  }

  /**
   * 移除数据源
   */
  removeSource() {
    LayerProvider.removeSource(this._map, this._sourceId);
  }

  /**
   * 绑定单击事件
   * @param {Function} event -事件函数
   */
  onClick(event) {
    this._clickEvent = event;
    if (this.hasLayer()) {
      LayerProvider.onEvent(this._map, this._layerId, 'click', event);
    }
  }

  /**
   * 绑定事件
   * @param {String} name -事件名称
   * @param {Function} event -事件函数
   *
   * @summary <type参数取值：mouseenter-鼠标移入; mouseleave-鼠标移除; click-鼠标单击>
   */
  onEvent(name, event) {
    switch (name) {
      case 'mouseenter': {
        this._enterEvent = event;
        break;
      }
      case 'mouseleave': {
        this._leaveEvent = event;
        break;
      }
      case 'click': {
        this._clickEvent = event;
        break;
      }
      default:
        return;
    }

    if (this.hasLayer()) {
      LayerProvider.onEvent(this._map, this._layerId, name, event);
    }
  }

  /**
   * 解除事件绑定
   */
  offEvent() {
    if (!this.hasLayer()) return;

    const map = this._map;
    let layerId = this._layerId;

    LayerProvider.offEvent(map, layerId, 'mouseenter', this._enterEvent);
    LayerProvider.offEvent(map, layerId, 'mouseleave', this._leaveEvent);
    if (this._clickEvent) {
      LayerProvider.offEvent(map, layerId, 'click', this._clickEvent);
    }
  }

  /** 私有方法 **/
  /**
   * 初始化事件
   */
  _initEvent() {
    const map = this._map;

    // 移入事件
    this._enterEvent = (e) => {
      MapProvider.setCursor(map, 'pointer');
    }
    // 移出事件
    this._leaveEvent = (e) => {
      MapProvider.setCursor(map);
    }
  }

  /**
   * 绑定事件
   */
  _onEvent() {
    const map = this._map;
    let layerId = this._layerId;

    LayerProvider.onEvent(map, layerId, 'mouseenter', this._enterEvent);
    LayerProvider.onEvent(map, layerId, 'mouseenter', this._leaveEvent);
    if (this._clickEvent) {
      LayerProvider.onEvent(map, layerId, 'click', this._clickEvent);
    }
  }
}
