import DataUtils from '../utils/DataUtils';
import VerifyUtils from '../utils/VerifyUtils';
import LayerProvider from '../geog/LayerProvider';
import GeoJsonProvider from '../geog/GeoJsonProvider';
import AnnotationLayer from '../common/AnnotationLayer';
import SimpleMarkerLayer from '../common/SimpleMarkerLayer';
import LabelLayer from './LabelLayer';

/**
 * 定义检查井图层
 *
 * @alias ManholeLayer
 *
 * @summary <用于加载检查井点位图层, 同一个数据源>
 */
export default class ManholeLayer {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

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

    /**
     * 是否缓存的
     * @type {Boolean}
     */
    this._cached = DataUtils.default(options.cached, false);

    /**
     * 图标
     * @type {String}
     */
    this._icon = DataUtils.default(options.icon, 'well');

    /**
     * 图标比例
     * @type {Number}
     */
    this._size = DataUtils.default(options.size, 1.0);

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

    /**
     * 是否分级显示
     * @type {Boolean}
     */
    this._showByLevel = DataUtils.default(options.showByLevel, true);

    /**
     * 是否显示标注
     * @type {Boolean}
     */
    this._showLabel = DataUtils.default(options.showLabel, false);

    /**
     * 字段名
     * @type {String}
     */
    this._fieldName = DataUtils.default(options.field, 'nodeid');

    /**
     * 文本大小
     * @type {Number}
     */
    this._textSize = DataUtils.default(options.textSize, 15);

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

    /**
     * 默认图层ID
     * @type {String}
     */
    this._defaultLayerId = DataUtils.default(options.layerId, this._defaultSourceId);

    // 数据源ID
    this._sourceId = this._showByLevel ? (this._defaultSourceId + '-lvl1') : this._defaultSourceId;
    this._lvl2_sourceId = this._showByLevel ? (this._defaultSourceId + '-lvl2') : undefined;
    this._lvl3_sourceId = this._showByLevel ? (this._defaultSourceId + '-lvl3') : undefined;

    // 图层ID
    this._layerId = this._showByLevel ? (this._defaultLayerId + '-lvl1') : this._defaultLayerId;
    this._lvl2_layerId = this._defaultLayerId + '-lvl2';
    this._lvl3_layerId = this._defaultLayerId + '-lvl3';

    // 标注图层ID
    this._labelLayerId = this._layerId + '-label';
    this._lvl2_labelLayerId = this._lvl2_layerId + '-label';
    this._lvl3_labelLayerId = this._lvl3_layerId + '-label';

    /* 图层对象 */
    this._wellLayer = null;      // 一级检查井
    this._lvl2_wellLayer = null; // 二级检查井
    this._lvl3_wellLayer = null; // 三级检查井

    this._labelLayer = null; // 标注
    this._lvl2_labelLayer = null;
    this._lvl3_labelLayer = null;

    // 初始化图层
    this._initLayer();

    if (this._showLabel) {
      // 初始化标注图层
      this._initLabelLayer();
    }
  }

  /** 公有方法 **/
  /**
   * 设置数据
   * @param {Object|String} data -数据/过滤条件
   */
  setData(data = null) {
    // 加载图层
    this.loadLayer();

    if (data) {
      this._wellLayer.setData(data);

      if (this._showLabel) {
        this._labelLayer.setData(data);
      }
    }
  }

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

    if (filter && VerifyUtils.isArray(filter)) {
      // 设置筛选器
      this._wellLayer.setFilter(filter);
      if (this._showLabel) {
        this._labelLayer.setFilter(filter);
      }

      if (this._showByLevel) {
        this._lvl2_wellLayer.setFilter(filter);
        this._lvl3_wellLayer.setFilter(filter);

        if (this._showLabel) {
          this._lvl2_labelLayer.setFilter(filter);
          this._lvl3_labelLayer.setFilter(filter);
        }
      }
    }
  }

  /**
   * 设置布局属性
   * @param {String} name -属性名称
   * @param {String|Array} value -属性值/表达式
   */
  setLayoutProperty(name, value) {
    this._wellLayer.setLayoutProperty(name, value);
    if (this._showLabel) {
      this._labelLayer.setLayoutProperty(name, value);
    }

    if (this._showByLevel) {
      this._lvl2_wellLayer.setLayoutProperty(name, value);
      this._lvl3_wellLayer.setLayoutProperty(name, value);

      if (this._showLabel) {
        this._lvl2_labelLayer.setLayoutProperty(name, value);
        this._lvl3_labelLayer.setLayoutProperty(name, value);
      }
    }
  }

  /**
   * 加载图层
   */
  loadLayer() {
    if (!this.hasLayer()) {
      // 一级检查井
      this._wellLayer.loadLayer();
      if (this._showLabel) {
        this._labelLayer.loadLayer();
      }

      if (this._showByLevel) {
        // 二级检查井
        this._lvl2_wellLayer.loadLayer();
        // 三级检查井
        this._lvl3_wellLayer.loadLayer();

        if (this._showLabel) {
          this._lvl2_labelLayer.loadLayer();
          this._lvl3_labelLayer.loadLayer();
        }
      }
    }

    // 显示图层
    this.showLayer(true);
  }

  /**
   * 加载标注图层
   */
  loadLabelLayer() {
    this._showLabel = true;

    if (!this._labelLayer) {
      this._initLabelLayer();
    }

    this._labelLayer.loadLayer();
    if (this._showByLevel) {
      this._lvl2_labelLayer.loadLayer();
      this._lvl3_labelLayer.loadLayer();
    }

    // 显示图层
    this._labelLayer.showLayer(true);
    if (this._showByLevel) {
      this._lvl2_labelLayer.showLayer(true);
      this._lvl3_labelLayer.showLayer(true);
    }
  }

  /**
   * 显示/隐藏图层
   * @param {Boolean} isShow -是否显示图层
   */
  showLayer(isShow) {
    // 一级检查井
    this._wellLayer.showLayer(isShow);
    if (this._showLabel) {
      this._labelLayer.showLayer(isShow);
    }

    if (this._showByLevel) {
      // 二级检查井
      this._lvl2_wellLayer.showLayer(isShow);
      // 三级检查井
      this._lvl3_wellLayer.showLayer(isShow);

      if (this._showLabel) {
        this._lvl2_labelLayer.showLayer(isShow);
        this._lvl3_labelLayer.showLayer(isShow);
      }
    }
  }

  /**
   * 图层是否可见
   *
   * @returns {Boolean}
   */
  isVisible() {
    return (
      LayerProvider.hasLayer(this._map, this._layerId) &&
      LayerProvider.isLayerVisible(this._map, this._layerId)
    );
  }

  /**
   * 判断图层是否已加载
   *
   * @returns {Boolean}
   */
  hasLayer() {
    let has = this._wellLayer.hasLayer();

    if (this._showByLevel) {
      has = (
        this._lvl2_wellLayer.hasLayer() &&
        this._lvl3_wellLayer.hasLayer()
      );
    }

    return has;
  }

  /**
   * 移除图层
   */
  removeLayer() {
    if (this._cached) return;

    if (this._showByLevel) {
      if (this._showLabel) {
        // 标注
        this._lvl2_labelLayer.removeLayer();
        this._lvl3_labelLayer.removeLayer();
      }

      // 二级检查井
      this._lvl2_wellLayer.removeLayer();
      // 三级检查井
      this._lvl3_wellLayer.removeLayer();
    }

    // 一级流向
    if (this._showLabel) {
      this._labelLayer.removeLayer();
    }
    // 一级检查井
    this._wellLayer.removeLayer();
  }

  /**
   * 移除标注图层
   */
  removeLabelLayer() {
    this._showLabel = false;

    if (!this._labelLayer) return;

    if (this._cached) {
      this._labelLayer.showLayer(false);
      if (this._showByLevel) {
        this._lvl2_labelLayer.showLayer(false);
        this._lvl3_labelLayer.showLayer(false);
      }
    } else {
      this._labelLayer.removeLayer();
      if (this._showByLevel) {
        this._lvl2_labelLayer.removeLayer();
        this._lvl3_labelLayer.removeLayer();
      }
    }
  }

  /**
   * 移除数据源
   */
  removeSource() {
    if (this._cached) return;

    // 清空数据源
    this._clearSourceData();

    const map = this._map;

    // 更新数据源
    LayerProvider.updateSource(map, this._sourceId, GeoJsonProvider.getEmptyFC());
    if (this._showByLevel) {
      LayerProvider.updateSource(map, this._lvl2_sourceId, GeoJsonProvider.getEmptyFC());
      LayerProvider.updateSource(map, this._lvl3_sourceId, GeoJsonProvider.getEmptyFC());
    }
    // 移除数据源
    LayerProvider.removeSource(map, this._sourceId);
    if (this._showByLevel) {
      LayerProvider.removeSource(map, this._lvl2_sourceId);
      LayerProvider.removeSource(map, this._lvl3_sourceId);
    }
  }

  /**
   * 绑定单击事件
   * @param {Function} event -事件函数
   */
  onClick(event) {
    // 一级检查井
    this._wellLayer.onClick(event);

    if (this._showByLevel) {
      // 二级检查井
      this._lvl2_wellLayer.onClick(event);
      // 三级检查井
      this._lvl3_wellLayer.onClick(event);
    }
  }

  /**
   * 绑定事件
   * @param {String} name -事件名称
   * @param {Function} event -事件函数
   */
  onEvent(name, event) {
    // 一级检查井
    this._wellLayer.onEvent(name, event);

    if (this._showByLevel) {
      // 二级检查井
      this._lvl2_wellLayer.onEvent(name, event);
      // 三级检查井
      this._lvl3_wellLayer.onEvent(name, event);
    }
  }

  /** 私有方法 **/
  /**
   * 初始化图层
   */
  _initLayer() {
    const map = this._map;
    const minZoom = this._minzoom;
    const cached = this._cached;

    const filter = this._filter;

    if (!this._showByLevel) {
      // 检查井
      this._wellLayer = new SimpleMarkerLayer({
        map: map,
        minzoom: minZoom,
        icon: this._icon,
        size: this._size,
        filter: filter,
        sourceId: this._sourceId,
        layerId: this._layerId
      });
    } else {
      // 一级检查井
      this._wellLayer = new SimpleMarkerLayer({
        map: map,
        minzoom: minZoom,
        icon: this._icon,
        size: this._size,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '1']] : filter,
        sourceId: this._sourceId,
        layerId: this._layerId
      });

      // 二级检查井
      this._lvl2_wellLayer = new SimpleMarkerLayer({
        map: map,
        minzoom: minZoom + 1.5,
        icon: this._icon,
        size: this._size,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '2']] : filter,
        sourceId: this._lvl2_sourceId,
        layerId: this._lvl2_layerId
      });

      // 三级检查井
      this._lvl3_wellLayer = new SimpleMarkerLayer({
        map: map,
        minzoom: minZoom + 3,
        icon: this._icon,
        size: this._size,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '3']] : filter,
        sourceId: this._lvl3_sourceId,
        layerId: this._lvl3_layerId
      });
    }

    // 检查井瓦片，需初始化默认鼠标事件
    if (this._cached) {
      this._wellLayer._onEvent();

      if (this._showByLevel) {
        this._lvl2_wellLayer._onEvent();
        this._lvl3_wellLayer._onEvent();
      }
    }
  }

  /**
   * 初始化标注图层
   */
  _initLabelLayer() {
    const map = this._map;
    const minZoom = this._minzoom;
    const cached = this._cached;

    const filter = this._filter;

    if (!this._showByLevel) {
      // 标注
      this._labelLayer = new LabelLayer({
        map: map,
        minzoom: minZoom,
        field: this._fieldName,
        textSize: this._textSize,
        radialOffset: 0.75,
        filter: filter,
        sourceId: this._sourceId,
        layerId: this._labelLayerId
      });
    } else {
      // 一级标注
      this._labelLayer = new LabelLayer({
        map: map,
        minzoom: minZoom,
        field: this._fieldName,
        textSize: this._textSize,
        radialOffset: 0.75,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '1']] : filter,
        sourceId: this._sourceId,
        layerId: this._labelLayerId
      });

      // 二级标注
      this._lvl2_labelLayer = new LabelLayer({
        map: map,
        minzoom: minZoom + 1.5,
        field: this._fieldName,
        textSize: this._textSize,
        radialOffset: 0.75,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '2']] : filter,
        sourceId: this._lvl2_sourceId,
        layerId: this._lvl2_labelLayerId
      });

      // 三级标注
      this._lvl3_labelLayer = new LabelLayer({
        map: map,
        minzoom: minZoom + 3,
        field: this._fieldName,
        textSize: this._textSize,
        radialOffset: 0.75,
        filter: (filter && cached) ? ['all', filter, ['==', 'show_level', '3']] : filter,
        sourceId: this._lvl3_sourceId,
        layerId: this._lvl3_labelLayerId
      });
    }
  }

  /**
   * 清空数据源数据
   */
  _clearSourceData() {
    if (this._cached) return;

    const map = this._map;

    // 一级检查井
    let source = map.getSource(this._sourceId);
    if (source && source._data && VerifyUtils.isGeoJsonFC(source._data)) {
      source._data.features = [];
    }
    if (this._showByLevel) {
      // 二级检查井
      source = map.getSource(this._lvl2_sourceId);
      if (source && source._data && VerifyUtils.isGeoJsonFC(source._data)) {
        source._data.features = [];
      }

      // 三级检查井
      source = map.getSource(this._lvl3_sourceId);
      if (source && source._data && VerifyUtils.isGeoJsonFC(source._data)) {
        source._data.features = [];
      }
    }
  }
}
