import LayerProvider from '../geog/LayerProvider';
import DataUtils from '../utils/DataUtils';
import ManholeLayer from './ManholeLayer';
import PipelineLayer from './PipelineLayer';

/**
 * 定义排水单元内管网图层提供类
 *
 * @alias PipenetProvider
 *
 * @summary <用于加载排水单元内管线、检查井图层>
 */
export default class PipenetInProvider {
  /**
   * 构造函数
   * @param {Object} options -图层选项
   */
  constructor(options) {
    /**
     * 地图对象
     * @type {Object}
     */
    this._map = options.map;

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

    /**
     * 线宽
     * @type {Number}
     */
    this._width = DataUtils.default(options.width, 6.5);

    // 管线数据源ID
    this._pipelineSourceId = DataUtils.default(options.pipelineSourceId, 'pipe_pipeline_in');
    // 检查井数据源ID
    this._manholeSourceId = DataUtils.default(options.manholeSourceId, 'pipe_manhole_in');

    /** 图层ID **/
    // 雨水管线图层
    this._rainLineLayerId = this._pipelineSourceId + '-rai';
    // 污水管线图层
    this._sewageLineLayerId = this._pipelineSourceId + '-sew';

    // 雨水井图层
    this._rainHoleLayerId = this._manholeSourceId + '-rai';
    // 污水井图层
    this._sewageHoleLayerId = this._manholeSourceId + '-sew';

    /** 图层对象 **/
    this._rainLineLayer = null;
    this._sewageLineLayer = null;

    this._rainHoleLayer = null;
    this._sewageHoleLayer = null;

    // 初始化图层
    this._initLayers();
  }

  /** 公有方法 **/
  /**
   * 设置数据
   * @param {String} type -类型
   * @param {Array|Object} data -数据
   */
  setData(type, data) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.setData(data);
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.setData(data);
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.setData(data);
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.setData(data);
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.setData(data);
        this._sewageLineLayer.setData(data);
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.setData(data);
        this._sewageHoleLayer.setData(data);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 设置筛选器
   * @param {String} type -类型
   * @param {Array} filter -筛选器
   */
  setFilter(type, filter) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.setFilter(filter);
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.setFilter(filter);
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.setFilter(filter);
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.setFilter(filter);
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.setFilter(filter);
        this._sewageLineLayer.setFilter(filter);
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.setFilter(filter);
        this._sewageHoleLayer.setFilter(filter);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 加载图层
   * @param {String} type -类型
   */
  loadLayer(type) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.loadLayer();
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.loadLayer();
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.loadLayer();
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.loadLayer();
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.loadLayer();
        this._sewageLineLayer.loadLayer();
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.loadLayer();
        this._sewageHoleLayer.loadLayer();
        break;
      }
      default:
        break;
    }
  }

  /**
   * 显示/隐藏图层
   * @param {String} type -类型
   * @param {Boolean} isShow -是否显示图层
   */
  showLayer(type, isShow) {

    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.showLayer(isShow);
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.showLayer(isShow);
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.showLayer(isShow);
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.showLayer(isShow);
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.showLayer(isShow);
        this._sewageLineLayer.showLayer(isShow);
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.showLayer(isShow);
        this._sewageHoleLayer.showLayer(isShow);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 判断是否已加载图层
   * @param {String} type -类型
   *
   * @returns {Boolean}
   */
  hasLayer(type) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain':
        // 雨水管线
        return this._rainLineLayer.hasLayer();
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage':
        // 污水管线
        return this._sewageLineLayer.hasLayer();
      case 'wellinrain':
      case 'manholeinrain':
        // 雨水井
        return this._rainHoleLayer.hasLayer();
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage':
        // 污水井
        return this._sewageHoleLayer.hasLayer();
      default:
        return false;
    }
  }

  /**
   * 移除图层
   * @param {String} type -类型
   */
  removeLayer(type) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.removeLayer();
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.removeLayer();
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.removeLayer();
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.removeLayer();
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.removeLayer();
        this._sewageLineLayer.removeLayer();
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.removeLayer();
        this._sewageHoleLayer.removeLayer();
        break;
      }
      default:
        break;
    }
  }

  /**
   * 移除所有图层(销毁时调用)
   */
  removeAllLayers() {
    // 雨水管线
    this._rainLineLayer.removeLayer();
    // 污水管线
    this._sewageLineLayer.removeLayer();
    // 移除管线数据源
    this._sewageLineLayer.removeSource();

    // 雨水井
    this._rainHoleLayer.removeLayer();
    // 污水井
    this._sewageHoleLayer.removeLayer();
    // 移除井数据源
    this._sewageLineLayer.removeSource();
  }

  /**
   * 绑定事件
   * @param {String} type -设施类型
   * @param {String} name -事件名称
   * @param {Function} event -事件函数
   */
  bindEvent(type, name, event) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.onEvent(name, event);
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.onEvent(name, event);
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.onEvent(name, event);
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.onEvent(name, event);
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.onEvent(name, event);
        this._sewageLineLayer.onEvent(name, event);
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.onEvent(name, event);
        this._sewageHoleLayer.onEvent(name, event);
        break;
      }
      default:
        break;
    }
  }

  /**
   * 绑定单击事件
   * @param {String} type -设施类型
   * @param {Function} event -事件函数
   */
  bindClickEvent(type, event) {
    switch (type) {
      case 'pipeinrain':
      case 'pipelineinrain': { // 雨水管线
        this._rainLineLayer.onClick(event);
        break;
      }
      case 'pipeinwaste':
      case 'pipeinsewage':
      case 'pipelineinwaste':
      case 'pipelineinsewage': { // 污水管线
        this._sewageLineLayer.onClick(event);
        break;
      }
      case 'wellinrain':
      case 'manholeinrain': { // 雨水井
        this._rainHoleLayer.onClick(event);
        break;
      }
      case 'wellinwaste':
      case 'wellinsewage':
      case 'manholeinwaste':
      case 'manholeinsewage': { // 污水井
        this._sewageHoleLayer.onClick(event);
        break;
      }
      case 'pipelinein': { // 管线
        this._rainLineLayer.onClick(event);
        this._sewageLineLayer.onClick(event);
        break;
      }
      case 'manholein': { // 检查井
        this._rainHoleLayer.onClick(event);
        this._sewageHoleLayer.onClick(event);
        break;
      }
      default:
        break;
    }
  }

  /** 私有方法 **/
  /**
   * 初始化图层
   */
  _initLayers() {
    // 管线
    this._initPipelineLayer();
    // 检查井
    this._initManholeLayer();
  }

  /**
   * 初始化管线图层
   */
  _initPipelineLayer() {
    const map = this._map;
    const minZoom = this._minzoom;

    // 雨水管线
    this._rainLineLayer = new PipelineLayer({
      map: map,
      minzoom: minZoom,
      color: '#13D75C',
      width: this._width,
      filter: ['==', 'category', '1'],
      showByLevel: false,
      showDirection: true,
      sourceId: this._pipelineSourceId,
      layerId: this._rainLineLayerId
    });

    // 污水管线
    this._sewageLineLayer = new PipelineLayer({
      map: map,
      minzoom: minZoom,
      color: '#AF0000',
      width: this._width,
      filter: ['==', 'category', '2'],
      showByLevel: false,
      showDirection: true,
      sourceId: this._pipelineSourceId,
      layerId: this._sewageLineLayerId
    });
  }

  /**
   * 初始化检查井图层
   */
  _initManholeLayer() {
    const map = this._map;
    const minZoom = this._minzoom + 2;

    // liudan
    // 雨水井
    this._rainHoleLayer = new ManholeLayer({
      map: map,
      minzoom: minZoom,
      icon: 'manhole_rai-in',
      filter: ['==', 'category', '1'],
      showByLevel: false,
      sourceId: this._manholeSourceId,
      layerId: this._rainHoleLayerId
    });

    // 污水井
    this._sewageHoleLayer = new ManholeLayer({
      map: map,
      minzoom: minZoom,
      icon: 'well_sew1x1',
      filter: ['==', 'category', '2'],
      showByLevel: false,
      sourceId: this._manholeSourceId,
      layerId: this._sewageHoleLayerId
    });
  }
}
