import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _createClass from 'babel-runtime/helpers/createClass';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/**
 * @文件说明: Interfaces.Fullscreen 地图全屏对象
 * @创建人: pjtools@vip.qq.com
 * @创建时间: 2020-05-21 10:37:14
 */

import { BasicMapApiEvented } from '../util/basicMapApiClass';
import DOM from '../util/dom';

var Fullscreen = function (_BasicMapApiEvented) {
  _inherits(Fullscreen, _BasicMapApiEvented);

  // 构造函数

  // 全屏区域DOM对象
  function Fullscreen(iMapApi) {
    _classCallCheck(this, Fullscreen);

    // 获取地图的全屏区域DOM对象
    var _this = _possibleConstructorReturn(this, (Fullscreen.__proto__ || Object.getPrototypeOf(Fullscreen)).call(this, iMapApi));

    _this._fullscreen = false;
    var element = iMapApi.getMapViewContainer();
    _this._container = element && element.parentNode;
    // 获取浏览器支持的Change事件类型
    if ('onfullscreenchange' in window.document) {
      _this._fullscreenchange = 'fullscreenchange';
    } else if ('onwebkitfullscreenchange' in window.document) {
      _this._fullscreenchange = 'webkitfullscreenchange';
    } else if ('onmozfullscreenchange' in window.document) {
      _this._fullscreenchange = 'mozfullscreenchange';
    } else if ('onmsfullscreenchange' in window.document) {
      _this._fullscreenchange = 'MSFullscreenChange';
    }
    return _this;
  }

  /**
   * 激活全屏模式
   * 使用注意：出于浏览器安全等原因，需人为手动触发，不能自动触发.
   */

  // 全屏Change事件各浏览器兼容支持的类型

  // 是否全屏状态


  _createClass(Fullscreen, [{
    key: 'enable',
    value: function enable() {
      if (this._checkFullscreenSupport() && !this.isFullscreen()) {
        if (this._container.requestFullscreen) {
          this._container.requestFullscreen();
        } else if (this._container.webkitRequestFullscreen) {
          this._container.webkitRequestFullscreen();
        } else if (this._container.mozRequestFullScreen) {
          this._container.mozRequestFullScreen();
        } else if (this._container.msRequestFullscreen) {
          this._container.msRequestFullscreen();
        }
        this._fullscreen = true;
        // 绑定事件
        this._eventChange = DOM.onEventListener(window.document, this._fullscreenchange, this._handleFullscreenChange.bind(this));
      }
    }

    /**
     * 退出全屏模式
     */

  }, {
    key: 'disable',
    value: function disable() {
      if (this._checkFullscreenSupport() && this.isFullscreen()) {
        if (window.document.exitFullscreen) {
          window.document.exitFullscreen();
        } else if (window.document.webkitCancelFullScreen) {
          window.document.webkitCancelFullScreen();
        } else if (window.document.mozCancelFullScreen) {
          window.document.mozCancelFullScreen();
        } else if (window.document.msExitFullscreen) {
          window.document.msExitFullscreen();
        }
        this._fullscreen = false;
      }
    }

    /**
     * 获取当前禁用/启用状态
     */

  }, {
    key: 'isEnabled',
    value: function isEnabled() {
      return this._fullscreen;
    }

    /**
     * 获取当前活动状态
     */

  }, {
    key: 'isActive',
    value: function isActive() {
      return this._fullscreen;
    }

    /**
     * 判断当前是否为全屏状态
     */

  }, {
    key: 'isFullscreen',
    value: function isFullscreen() {
      return this._fullscreen;
    }

    /**
     * 全屏模式自动切换
     */

  }, {
    key: 'toggle',
    value: function toggle() {
      if (this.isFullscreen()) {
        this.disable();
      } else {
        this.enable();
      }
    }

    // 检查浏览器是否支持全屏接口

  }, {
    key: '_checkFullscreenSupport',
    value: function _checkFullscreenSupport() {
      return !!(window.document.fullscreenEnabled || window.document.mozFullScreenEnabled || window.document.msFullscreenEnabled || window.document.webkitFullscreenEnabled);
    }

    // 执行全屏模式的Change事件

  }, {
    key: '_handleFullscreenChange',
    value: function _handleFullscreenChange() {
      var fullscreenElement = window.document.fullscreenElement || window.document.webkitFullscreenElement || window.document.mozFullScreenElement || window.document.msFullscreenElement;
      // 判断通过“ESC”或“F11”退出全屏时，更新状态
      if (fullscreenElement === this._container !== this._fullscreen) {
        this._fullscreen = !this._fullscreen;
        if (this._eventChange) {
          this._eventChange.remove();
          delete this._eventChange;
        }
      }
      this.fire('change', { status: this._fullscreen });
    }
  }]);

  return Fullscreen;
}(BasicMapApiEvented);

export default Fullscreen;