var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
    if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
    return cooked;
};
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    }
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
import React from 'react';
import styled, { css } from 'react-emotion';
import * as Three from 'three';
import NavigationBoxModel from '../../assets/models/navigation_box.glb';
import { loadGLTFFromUrl } from '../../lib/object3dts/loaders';
var HALF_PI = Math.PI / 2;
var QUARTER_PI = Math.PI / 4;
var WIDTH = 150;
var HEIGHT = 150;
var Container = styled.div(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n  position: absolute;\n  top: 0px;\n  left: 0px;\n  width: ", "px;\n  height: ", "px;\n\n  ", "\n"], ["\n  position: absolute;\n  top: 0px;\n  left: 0px;\n  width: ", "px;\n  height: ", "px;\n\n  ",
    "\n"])), WIDTH, HEIGHT, function (props) { return props.hovering && css(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n    cursor: pointer;\n  "], ["\n    cursor: pointer;\n  "]))); });
var clickBoxes = [
    // TOP
    {
        position: [0, 0, 1],
        size: [1.5, 1.5, 0.001],
        cameraAngle: [-HALF_PI, 0],
    },
    // BOTTOM
    {
        position: [0, 0, -1],
        size: [1.5, 1.5, 0.001],
        cameraAngle: [-HALF_PI, Math.PI],
    },
    // FRONT
    {
        position: [0, -1, 0],
        size: [1.5, 0.001, 1.5],
        cameraAngle: [-HALF_PI, HALF_PI],
    },
    // BACK
    {
        position: [0, 1, 0],
        size: [1.5, 0.001, 1.5],
        cameraAngle: [HALF_PI, HALF_PI],
    },
    // RIGHT
    {
        position: [1, 0, 0],
        size: [0.001, 1.5, 1.5],
        cameraAngle: [0, HALF_PI],
    },
    // LEFT
    {
        position: [-1, 0, 0],
        size: [0.001, 1.5, 1.5],
        cameraAngle: [-Math.PI, HALF_PI],
    },
    // TOP-FRONT
    {
        position: [0, -0.876, 0.876],
        size: [1.5, 0.25, 0.25],
        cameraAngle: [-HALF_PI, QUARTER_PI],
    },
    // TOP-LEFT
    {
        position: [-0.876, 0, 0.876],
        size: [0.25, 1.5, 0.25],
        cameraAngle: [-Math.PI, QUARTER_PI],
    },
    // TOP-BACK
    {
        position: [0, 0.876, 0.876],
        size: [1.5, 0.25, 0.25],
        cameraAngle: [HALF_PI, QUARTER_PI],
    },
    // TOP-RIGHT
    {
        position: [0.876, 0, 0.876],
        size: [0.25, 1.5, 0.25],
        cameraAngle: [0, QUARTER_PI],
    },
    // BOTTOM-FRONT
    {
        position: [0, -0.876, -0.876],
        size: [1.5, 0.25, 0.25],
        cameraAngle: [-HALF_PI, 3 * QUARTER_PI],
    },
    // BOTTOM-LEFT
    {
        position: [-0.876, 0, -0.876],
        size: [0.25, 1.5, 0.25],
        cameraAngle: [-Math.PI, 3 * QUARTER_PI],
    },
    // BOTTOM-BACK
    {
        position: [0, 0.876, -0.876],
        size: [1.5, 0.25, 0.25],
        cameraAngle: [HALF_PI, 3 * QUARTER_PI],
    },
    // BOTTOM-RIGHT
    {
        position: [0.876, 0, -0.876],
        size: [0.25, 1.5, 0.25],
        cameraAngle: [0, 3 * QUARTER_PI],
    },
    // FRONT-LEFT
    {
        position: [-0.876, -0.876, 0],
        size: [0.25, 0.25, 1.5],
        cameraAngle: [-3 * QUARTER_PI, HALF_PI],
    },
    // FRONT-RIGHT
    {
        position: [0.876, -0.876, 0],
        size: [0.25, 0.25, 1.5],
        cameraAngle: [-QUARTER_PI, HALF_PI],
    },
    // BACK-LEFT
    {
        position: [-0.876, 0.876, 0],
        size: [0.25, 0.25, 1.5],
        cameraAngle: [3 * QUARTER_PI, HALF_PI],
    },
    // BACK-RIGHT
    {
        position: [0.876, 0.876, 0],
        size: [0.25, 0.25, 1.5],
        cameraAngle: [QUARTER_PI, HALF_PI],
    },
    // TOP-FRONT-LEFT
    {
        position: [-0.876, -0.876, 0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [-3 * QUARTER_PI, QUARTER_PI],
    },
    // TOP-FRONT-RIGHT
    {
        position: [0.876, -0.876, 0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [-QUARTER_PI, QUARTER_PI],
    },
    // TOP-BACK-LEFT
    {
        position: [-0.876, 0.876, 0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [3 * QUARTER_PI, QUARTER_PI],
    },
    // TOP-BACK-RIGHT
    {
        position: [0.876, 0.876, 0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [QUARTER_PI, QUARTER_PI],
    },
    // BOTTOM-FRONT-LEFT
    {
        position: [-0.876, -0.876, -0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [-3 * QUARTER_PI, 3 * QUARTER_PI],
    },
    // BOTTOM-FRONT-RIGHT
    {
        position: [0.876, -0.876, -0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [-QUARTER_PI, 3 * QUARTER_PI],
    },
    // BOTTOM-BACK-LEFT
    {
        position: [-0.876, 0.876, -0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [3 * QUARTER_PI, 3 * QUARTER_PI],
    },
    // BOTTOM-BACK-RIGHT
    {
        position: [0.876, 0.876, -0.876],
        size: [0.25, 0.25, 0.25],
        cameraAngle: [QUARTER_PI, 3 * QUARTER_PI],
    },
];
var ThreeDNavigationBox = /** @class */ (function (_super) {
    __extends(ThreeDNavigationBox, _super);
    function ThreeDNavigationBox() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.container = React.createRef();
        _this.containerRect = { left: 0, top: 0, width: 0, height: 0 };
        _this.renderer = new Three.WebGLRenderer({ alpha: true, antialias: true });
        _this.scene = new Three.Scene();
        _this.camera = new Three.PerspectiveCamera(50, 1, 0.1, 1000);
        _this.raycaster = new Three.Raycaster();
        _this.boxes = new Three.Group();
        _this.mousePosition = new Three.Vector2();
        _this.state = {
            hoverBox: null
        };
        _this.onClick = function (e) {
            var onChangeCameraAngle = _this.props.onChangeCameraAngle;
            var box = _this.getBoxFromPosition(e.clientX, e.clientY);
            if (box) {
                onChangeCameraAngle.apply(void 0, box.cameraAngle);
            }
        };
        _this.onMouseMove = function (e) {
            var box = _this.getBoxFromPosition(e.clientX, e.clientY);
            var needsRender = false;
            if (_this.state.hoverBox) {
                _this.state.hoverBox.material.opacity = 0;
                _this.setState({ hoverBox: null });
                needsRender = true;
            }
            if (box) {
                box.material.opacity = 0.45;
                _this.setState({ hoverBox: box });
                needsRender = true;
            }
            if (needsRender) {
                _this.renderer.render(_this.scene, _this.camera);
            }
        };
        _this.getBoxFromPosition = function (x, y) {
            var _a = _this.containerRect, left = _a.left, top = _a.top, width = _a.width, height = _a.height;
            _this.mousePosition.x = ((x - left) / width) * 2 - 1;
            _this.mousePosition.y = -((y - top) / height) * 2 + 1;
            _this.raycaster.setFromCamera(_this.mousePosition, _this.camera);
            var intersects = _this.raycaster.intersectObjects(_this.boxes.children, true);
            if (intersects.length > 0) {
                return intersects[0].object;
            }
        };
        return _this;
    }
    ThreeDNavigationBox.prototype.componentDidMount = function () {
        this.containerRect = this.container.current.getBoundingClientRect();
        this.setupScene();
    };
    ThreeDNavigationBox.prototype.setupScene = function () {
        return __awaiter(this, void 0, void 0, function () {
            var gltf;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.scene.add(new Three.AmbientLight(0xeeeeee));
                        clickBoxes.forEach(function (clickBox) {
                            var _a, _b;
                            var geometry = new ((_a = Three.BoxGeometry).bind.apply(_a, [void 0].concat(clickBox.size)))();
                            var material = new Three.MeshBasicMaterial({
                                color: 0x40a0f0,
                                transparent: true,
                                opacity: 0,
                            });
                            var box = new Three.Mesh(geometry, material);
                            (_b = box.position).set.apply(_b, clickBox.position);
                            box.cameraAngle = clickBox.cameraAngle;
                            _this.boxes.add(box);
                        });
                        this.camera.aspect = WIDTH / HEIGHT;
                        this.camera.updateProjectionMatrix();
                        this.camera.up.set(0, 0, 1);
                        this.camera.lookAt(this.scene.position);
                        this.container.current.appendChild(this.renderer.domElement);
                        this.renderer.setSize(WIDTH, HEIGHT);
                        this.scene.add(this.boxes);
                        return [4 /*yield*/, loadGLTFFromUrl(NavigationBoxModel)];
                    case 1:
                        gltf = _a.sent();
                        gltf.scene.rotateX(HALF_PI);
                        gltf.scene.rotateY(-HALF_PI);
                        this.scene.add(gltf.scene);
                        this.renderer.render(this.scene, this.camera);
                        return [2 /*return*/];
                }
            });
        });
    };
    ThreeDNavigationBox.prototype.updateCamera = function (x, y, z) {
        this.camera.position.set(x * 5, y * 5, z * 5);
        this.camera.lookAt(this.scene.position);
        this.renderer.render(this.scene, this.camera);
    };
    ThreeDNavigationBox.prototype.render = function () {
        return (React.createElement(Container, { innerRef: this.container, onClick: this.onClick, onMouseMove: this.onMouseMove, hovering: this.state.hoverBox }));
    };
    return ThreeDNavigationBox;
}(React.Component));
export default ThreeDNavigationBox;
var templateObject_1, templateObject_2;
//# sourceMappingURL=ThreeDNavigationBox.js.map