'use strict';

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

var _class, _temp2;

var _reactBootstrap = require('react-bootstrap');

var _reactRedux = require('react-redux');

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _propTypes = require('prop-types');

var _propTypes2 = _interopRequireDefault(_propTypes);

var _lodash = require('lodash');

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

const { config, i18n } = window;
const __ = i18n.setting.__.bind(i18n.setting);

function _ref(i) {
  return _react2.default.createElement(
    'option',
    { key: i, value: i * 400 + 400 },
    i * 50 + 50,
    '%'
  );
}

const ResolutionConfig = (0, _reactRedux.connect)((state, props) => ({
  webview: state.layout.webview
}))((_temp2 = _class = class ResolutionConfig extends _react.Component {
  constructor(...args) {
    var _temp;

    return _temp = super(...args), this.state = {
      width: this.props.webview.width
    }, this.handleSetWebviewWidthWithDebounce = (value, isDebounced) => {
      this.setState({
        width: parseInt(value)
      });
      if (!this.handleSetWebviewWidthDebounced) {
        this.handleSetWebviewWidthDebounced = (0, _lodash.debounce)(this.handleSetWebviewWidth, 1000, { leading: false, trailing: true });
      }
      if (isDebounced) {
        this.handleSetWebviewWidthDebounced(value * devicePixelRatio);
      } else {
        this.handleSetWebviewWidth(value * devicePixelRatio);
      }
    }, this.handleSetWebviewWidth = value => {
      const useFixedResolution = this.props.webview.useFixedResolution;
      const width = parseInt(value);
      if (isNaN(width) || width < 0 || !useFixedResolution) {
        return;
      }
      config.set('poi.webview.width', width);
    }, this.handleSetFixedResolution = e => {
      if (this.props.webview.useFixedResolution) {
        config.set('poi.webview.width', -1);
      } else {
        const { devicePixelRatio } = window;
        config.set('poi.webview.width', Math.round(this.props.webview.width * devicePixelRatio));
      }
    }, this.componentWillReceiveProps = nextProps => {
      if (this.state.width !== nextProps.webview.width) {
        this.setState({
          width: nextProps.webview.width
        });
      }
    }, _temp;
  }

  render() {
    return _react2.default.createElement(
      _reactBootstrap.Grid,
      null,
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 8 },
        _react2.default.createElement(
          _reactBootstrap.Checkbox,
          {
            checked: !this.props.webview.useFixedResolution,
            onChange: this.handleSetFixedResolution },
          __('Adaptive resolution based on the window')
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { xs: 4 },
        _react2.default.createElement(
          _reactBootstrap.FormControl,
          { componentClass: 'select',
            value: this.state.width,
            onChange: e => this.handleSetWebviewWidthWithDebounce(e.target.value, false),
            disabled: !this.props.webview.useFixedResolution },
          _react2.default.createElement(
            'option',
            { key: -1, value: this.state.width, hidden: true },
            Math.round(this.props.webview.width / 800 * 100),
            '%'
          ),
          [0, 1, 2, 3].map(_ref)
        )
      ),
      _react2.default.createElement(
        _reactBootstrap.Col,
        { id: 'poi-resolution-config', xs: 12, style: { display: 'flex', alignItems: 'center' } },
        _react2.default.createElement(
          'div',
          { style: { flex: 1 } },
          _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number',
            value: this.state.width,
            onChange: e => this.handleSetWebviewWidthWithDebounce(e.target.value, true),
            readOnly: !this.props.webview.useFixedResolution })
        ),
        _react2.default.createElement(
          'div',
          { style: { flex: 'none', width: 15, paddingLeft: 5 } },
          'x'
        ),
        _react2.default.createElement(
          'div',
          { style: { flex: 1 } },
          _react2.default.createElement(_reactBootstrap.FormControl, { type: 'number', value: Math.round(this.props.webview.height), readOnly: true })
        ),
        _react2.default.createElement(
          'div',
          { style: { flex: 'none', width: 15, paddingLeft: 5 } },
          'px'
        )
      )
    );
  }
}, _class.propTypes = {
  webview: _propTypes2.default.object
}, _temp2));

exports.default = ResolutionConfig;
module.exports = exports['default'];