'use strict';

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

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

var _extends3 = _interopRequireDefault(_extends2);

var _antdv = require('../../../_util/antdv');

var _baseProps = require('../baseProps');

var _baseProps2 = _interopRequireDefault(_baseProps);

var _baseMixin = require('../baseMixin');

var _baseMixin2 = _interopRequireDefault(_baseMixin);

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

// 默认Scale控件的位置
var defaultScalePosition = exports.defaultScalePosition = 'bottom-left'; /**
                                                                          * @文件说明: Controls.Scale - 比例尺控件
                                                                          * @创建人: pjtools@vip.qq.com
                                                                          * @创建时间: 2020-05-08 16:18:15
                                                                          */

var Scale = {
  name: 'PjMap.Controls.Scale',
  inheritAttrs: false,
  mixins: [_baseMixin2['default']],
  props: (0, _antdv.initDefaultProps)((0, _extends3['default'])({}, (0, _baseProps2['default'])(), {
    // 比例尺最大宽度
    maxWidth: _antdv.PropTypes.number,
    // 比例尺显示单位
    unit: _antdv.PropTypes.oneOf(['imperial', 'metric', 'nautical'])
  }), {
    position: defaultScalePosition,
    offset: [10, 5],
    maxWidth: 80,
    unit: 'metric'
  }),
  data: function data() {
    return {
      // 当前比例尺的数值
      currentValue: 0,
      // 当前比例尺的单位
      currentUnit: 'm',
      // 当前比例尺的样式
      scaleStyle: {}
    };
  },

  methods: {
    // 执行地图Move事件执驱动比例尺重新计算
    handleMapScaleMove: function handleMapScaleMove() {
      this.updateScaleValue();
    },
    getDecimalRoundNum: function getDecimalRoundNum(d) {
      var multiplier = Math.pow(10, Math.ceil(-Math.log(d) / Math.LN10));
      return Math.round(d * multiplier) / multiplier;
    },
    getRoundNum: function getRoundNum(num) {
      var pow10 = Math.pow(10, ('' + Math.floor(num)).length - 1);
      var d = num / pow10;
      d = d >= 10 ? 10 : d >= 5 ? 5 : d >= 3 ? 3 : d >= 2 ? 2 : d >= 1 ? 1 : this.getDecimalRoundNum(d);
      return pow10 * d;
    },
    distanceTo: function distanceTo(from, to) {
      var rad = Math.PI / 180;
      var lat1 = from[1] * rad;
      var lat2 = to[1] * rad;
      var maxMeters = 6371008.8 * Math.acos(Math.min(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos((to[0] - from[0]) * rad), 1));
      return maxMeters;
    },


    // 计算比例尺
    calculationScale: function calculationScale(maxWidth, maxDistance, unit) {
      var distance = this.getRoundNum(maxDistance) || 0;
      var ratio = maxDistance === 0 ? 1 : distance / maxDistance;
      this.currentValue = distance;
      this.currentUnit = unit;
      this.scaleStyle = {
        width: maxWidth * ratio + 16 + 'px'
      };
    },


    // 更新比例尺的参考距离比值
    updateScaleValue: function updateScaleValue() {
      var iMapApi = this.iMapApi;
      var map = iMapApi && iMapApi.map;
      var maxWidth = this.maxWidth;
      // 计算屏幕坐标的距离
      var y = map._container.clientHeight / 2;
      var left = map.unproject([0, y]).toArray();
      var right = map.unproject([maxWidth, y]).toArray();
      var from = iMapApi.toWGS84(left);
      var to = iMapApi.toWGS84(right);
      var maxMeters = this.distanceTo(from, to);
      // 根据单位类型计算转换
      switch (this.unit) {
        case 'imperial':
          {
            var maxFeet = 3.2808 * maxMeters;
            if (maxFeet > 5280) {
              var maxMiles = maxFeet / 5280;
              this.calculationScale(maxWidth, maxMiles, '英里');
            } else {
              this.calculationScale(maxWidth, maxFeet, '英尺');
            }
            break;
          }
        case 'nautical':
          {
            var maxNauticals = maxMeters / 1852;
            this.calculationScale(maxWidth, maxNauticals, '海里');
            break;
          }
        default:
          {
            if (maxMeters >= 1000) {
              this.calculationScale(maxWidth, maxMeters / 1000, '公里');
            } else {
              this.calculationScale(maxWidth, maxMeters, '米');
            }
            break;
          }
      }
    },


    // 设置比例尺的单位
    setUnit: function setUnit(unit) {
      var _this = this;

      this.$emit('update:unit', unit);
      this.$nextTick(function () {
        _this.updateScaleValue();
      });
    }
  },
  mounted: function mounted() {
    // 联动地图move事件驱动更新比例尺
    this.iMapApi.on('pjmap.controls.scale.move', 'move', this.handleMapScaleMove);
    this.handleMapScaleMove();
  },
  beforeDestroy: function beforeDestroy() {
    this.iMapApi.off('pjmap.controls.scale.move');
  },
  render: function render() {
    var h = arguments[0];
    var id = this.id,
        classes = this.classes,
        ctrlStyles = this.ctrlStyles,
        currentValue = this.currentValue,
        currentUnit = this.currentUnit,
        scaleStyle = this.scaleStyle;


    return h(
      'div',
      {
        attrs: { 'data-id': id },
        'class': classes, style: ctrlStyles },
      [h(
        'div',
        { 'class': 'mapboxgl-ctrl mapboxgl-ctrl-scale', style: scaleStyle },
        [currentValue + ' ' + currentUnit]
      )]
    );
  }
};

exports['default'] = Scale;