'use strict';

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

var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');

var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);

var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');

var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);

var _get2 = require('babel-runtime/helpers/get');

var _get3 = _interopRequireDefault(_get2);

var _createClass2 = require('babel-runtime/helpers/createClass');

var _createClass3 = _interopRequireDefault(_createClass2);

var _inherits2 = require('babel-runtime/helpers/inherits');

var _inherits3 = _interopRequireDefault(_inherits2);

var _extends2 = require('babel-runtime/helpers/extends');

var _extends3 = _interopRequireDefault(_extends2);

var _basicLayer = require('./basicLayer');

var _basicLayer2 = _interopRequireDefault(_basicLayer);

var _deepmerge = require('deepmerge');

var _deepmerge2 = _interopRequireDefault(_deepmerge);

var _hat = require('hat');

var _hat2 = _interopRequireDefault(_hat);

var _cloneDeep = require('lodash/cloneDeep');

var _cloneDeep2 = _interopRequireDefault(_cloneDeep);

var _lineLayer = require('./lineLayer');

var _lineLayer2 = _interopRequireDefault(_lineLayer);

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

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

/**
 * @文件说明: Layers.PolygonLayer 矢量面图层
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-25 11:04:47
 */

var defaultLayerOptions = (0, _deepmerge2['default'])(_basicLayer.defaultVectorLayerOptions, {
  paint: {
    'fill-color': 'rgba(255, 156, 110, 0.45)',
    'fill-opacity': 1,
    'fill-antialias': true,
    'fill-outline-color': 'rgba(255, 255, 255, 0)',
    'line-color': 'rgba(250, 84, 28, 0.8)'
  },
  metadata: {
    layerTypeName: 'PolygonLayer'
  }
});

var defaultOptions = (0, _extends3['default'])({}, _basicLayer.defaultVectorBasicOptions, {
  // 是否开启外边线模式
  outline: false
});

var _outlineLayer = Symbol('outlineLayer');

var PolygonLayer = function (_BasicLayerClass) {
  (0, _inherits3['default'])(PolygonLayer, _BasicLayerClass);
  (0, _createClass3['default'])(PolygonLayer, [{
    key: 'outlineLayer',

    // 外边线图层实现对象
    get: function get() {
      return this[_outlineLayer] || null;
    }
  }]);

  function PolygonLayer(iMapApi, id) {
    var layerOptions = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
    var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
    (0, _classCallCheck3['default'])(this, PolygonLayer);

    var opts = _deepmerge2['default'].all([{}, defaultOptions, options || {}]);
    opts.outline = !!(0, _methodsUtil.isBooleanTrue)(opts.outline);
    opts.opacityPaints = ['fill-opacity'];
    // 合并原生Layer图层属性
    !opts.outline && (defaultLayerOptions.paint['fill-color'] = defaultLayerOptions.paint['line-color']);
    var layer = _deepmerge2['default'].all([{}, defaultLayerOptions, layerOptions || {}]);
    layer.type = 'fill';
    // 继承矢量图层基类
    !id && (id = (0, _hat2['default'])());
    var layerGroupId = null;
    if (opts.outline) {
      layerGroupId = opts.layerGroupId || id + '.polygon_layergroup';
      opts.layerGroupId = layerGroupId;
    }

    // 判断是否渲染外边线，则添加线图层
    var _this = (0, _possibleConstructorReturn3['default'])(this, (PolygonLayer.__proto__ || Object.getPrototypeOf(PolygonLayer)).call(this, iMapApi, id, layer, opts));

    if (opts.outline) {
      var outlineLayerOptions = (0, _cloneDeep2['default'])(layer);
      outlineLayerOptions.metadata.layerTypeName && delete outlineLayerOptions.metadata.layerTypeName;
      outlineLayerOptions.type = 'line';
      // 获取填充面图层的数据源
      var fillLayer = _this.iMapApi.getLayer(id, opts.layerGroupId);
      fillLayer && fillLayer.source && (outlineLayerOptions.source = fillLayer.source);
      // 转换外边线图层的参数选项
      var outlineOptions = (0, _cloneDeep2['default'])(opts);
      outlineOptions.opacityPaints && delete outlineOptions.opacityPaints;
      outlineOptions.data && delete outlineOptions.data;
      var outlineLayer = new _lineLayer2['default'](iMapApi, id + '.outline', outlineLayerOptions, outlineOptions);
      _this[_outlineLayer] = outlineLayer;
    }

    // 绑定图层实例对象
    _this.mapLayer && (_this.mapLayer._instance = _this);
    return _this;
  }

  /**
   * 移除当前图层
   */


  (0, _createClass3['default'])(PolygonLayer, [{
    key: 'remove',
    value: function remove() {
      // 判断是否有外边线
      if (this.outlineLayer) {
        this.outlineLayer.remove();
        this[_outlineLayer] = null;
      }
      (0, _get3['default'])(PolygonLayer.prototype.__proto__ || Object.getPrototypeOf(PolygonLayer.prototype), 'remove', this).call(this);
    }

    /**
     * 设置图层的整体透明度
     * @param {Number} opacity 待设定的透明度
     */

  }, {
    key: 'setOpacity',
    value: function setOpacity(opacity) {
      (0, _get3['default'])(PolygonLayer.prototype.__proto__ || Object.getPrototypeOf(PolygonLayer.prototype), 'setOpacity', this).call(this, opacity, ['fill-opacity']);
      // 判断是否有外边线
      this.outlineLayer && this.outlineLayer.setOpacity(opacity);
    }

    /**
     * 获取矢量面的填充颜色属性
     */

  }, {
    key: 'getColor',
    value: function getColor() {
      return this.getPaint('fill-color');
    }

    /**
     * 设置矢量面的填充颜色属性
     * @param {String} color 待设定的颜色值
     */

  }, {
    key: 'setColor',
    value: function setColor(color) {
      color && this.setPaint('fill-color', color);
    }

    /**
     * 获取矢量面的外边线颜色属性
     */

  }, {
    key: 'getOutlineColor',
    value: function getOutlineColor() {
      return this.outlineLayer ? this.outlineLayer.getColor() : null;
    }

    /**
     * 设置矢量面的外边线颜色属性
     * @param {String} color 待设定的颜色值
     */

  }, {
    key: 'setOutlineColor',
    value: function setOutlineColor(color) {
      this.outlineLayer && this.outlineLayer.setColor(color);
    }

    /**
     * 获取矢量面的外边线宽度
     */

  }, {
    key: 'getOutlineWidth',
    value: function getOutlineWidth() {
      return this.outlineLayer ? this.outlineLayer.getLineWidth() : null;
    }

    /**
     * 设置矢量面的外边线宽度
     * @param {Number} width 待设定的线宽度
     */

  }, {
    key: 'setOutlineWidth',
    value: function setOutlineWidth(width) {
      this.outlineLayer && this.outlineLayer.setLineWidth(width);
    }
  }]);
  return PolygonLayer;
}(_basicLayer2['default']);

exports['default'] = PolygonLayer;