'use strict';

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

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _react = require('react');

var _react2 = _interopRequireDefault(_react);

var _reactThreeRenderer = require('react-three-renderer');

var _reactThreeRenderer2 = _interopRequireDefault(_reactThreeRenderer);

var _three = require('three');

var THREE = _interopRequireWildcard(_three);

var _path = require('path');

var _path2 = _interopRequireDefault(_path);

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }

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

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }

function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }

var Scene = function (_React$Component) {
  _inherits(Scene, _React$Component);

  function Scene(props, context) {
    _classCallCheck(this, Scene);

    // construct the position vector here, because if we use 'new' within render,
    // React will think that things have changed when they have not.
    var _this = _possibleConstructorReturn(this, (Scene.__proto__ || Object.getPrototypeOf(Scene)).call(this, props, context));

    _this.scrollHandler = function (e) {
      e.preventDefault();
    };

    _this.componentDidMount = function () {
      window.scroll(0, 0);
      window.onwheel = _this.scrollHandler;
    };

    _this.componentWillUnmount = function () {
      window.onwheel = null;
    };

    _this.cameraPosition = new THREE.Vector3(0, 0, 2);
    _this.directionalLightPosition = new THREE.Vector3(0, 0, 1);
    _this.scenePosition = new THREE.Vector3(0, 0, 0);

    _this.state = {
      cubeRotation: new THREE.Euler()
    };

    _this._onAnimate = function () {
      _this.setState({
        cubeRotation: new THREE.Euler(_this.state.cubeRotation.x + 0.002, _this.state.cubeRotation.y + 0.0002, 0)
      });
    };
    return _this;
  }

  _createClass(Scene, [{
    key: 'render',
    value: function render() {
      var width = window.innerWidth; // canvas width
      var height = window.innerHeight - 40; // canvas height


      return _react2.default.createElement(
        _reactThreeRenderer2.default,
        {
          mainCamera: 'camera',
          width: width,
          height: height,
          onAnimate: this._onAnimate },
        _react2.default.createElement(
          'resources',
          null,
          _react2.default.createElement('texture', {
            resourceId: 'texture',
            url: _path2.default.join(this.props.texture.basepath, this.props.texture.path),
            wrapS: THREE.RepeatWrapping,
            wrapT: THREE.RepeatWrapping,
            anisotropy: 16,
            repeat: new THREE.Vector2(4, 4)
          }),
          _react2.default.createElement(
            'meshLambertMaterial',
            {
              resourceId: 'material',
              side: THREE.DoubleSide
            },
            _react2.default.createElement('textureResource', {
              resourceId: 'texture'
            })
          )
        ),
        _react2.default.createElement(
          'scene',
          null,
          _react2.default.createElement('perspectiveCamera', {
            name: 'camera',
            fov: 75,
            aspect: width / height,
            near: 0.1,
            far: 1000,

            position: this.cameraPosition }),
          _react2.default.createElement('ambientLight', {
            color: 0x222222
          }),
          _react2.default.createElement('directionalLight', {
            color: 0xffffff,
            position: this.directionalLightPosition,
            lookAt: this.scenePosition
          }),
          _react2.default.createElement(
            'mesh',
            { rotation: this.state.cubeRotation },
            _react2.default.createElement('boxGeometry', {
              width: 1,
              height: 1,
              depth: 1
            }),
            _react2.default.createElement('materialResource', {
              resourceId: 'material'
            })
          )
        )
      );
    }
  }]);

  return Scene;
}(_react2.default.Component);

exports.default = Scene;