"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;
require("core-js/modules/web.dom-collections.iterator.js");
var _PhaserGame = require("./game/PhaserGame");
require("./styles.css");
var _AppModule = _interopRequireDefault(require("./App.module.css"));
var _react = require("react");
var _EventBus = require("./game/EventBus");
var _starTarget = require("./assets/star-target.svg");
var _raftSubscriptionHelpers = require("./utils/raft-subscription-helpers");
var _raft = require("@robotical/webapp-types/dist-types/src/types/raft");
var _blockSelector = _interopRequireDefault(require("./block-selector"));
var _overlayingTutorial = _interopRequireDefault(require("./overlaying-tutorial"));
var _WonOverlay = _interopRequireDefault(require("./WonOverlay"));
var _star = require("./assets/star.svg");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function JumpingGame() {
  const [gameContainerDimensions, setGameContainerDimensions] = (0, _react.useState)({
    width: "80vw",
    height: "60vh"
  });
  const [collectedStars, setCollectedStars] = (0, _react.useState)(0);
  const [isZooming, setIsZooming] = (0, _react.useState)(false);
  const phaserGameRef = (0, _react.useRef)(null);
  const [connectedCog, setConnectedCog] = (0, _react.useState)(null);
  const [isWinningFlagVisible, setIsWinningFlagVisible] = (0, _react.useState)(false);
  const subHelper = (0, _react.useRef)(null);
  (0, _react.useEffect)(() => {
    var _window$applicationMa;
    /* Checks if we are already connected to a cog */
    const selectedCog = (_window$applicationMa = window.applicationManager) === null || _window$applicationMa === void 0 ? void 0 : _window$applicationMa.getTheCurrentlySelectedDeviceOrFirstOfItsKind(_raft.RaftTypeE.COG);
    if (selectedCog) {
      setConnectedCog(selectedCog);
      subHelper.current = (0, _raftSubscriptionHelpers.raftDisconnectedSubscriptionHelper)(selectedCog);
      subHelper.current.subscribe(() => {
        var _subHelper$current;
        setConnectedCog(null);
        (_subHelper$current = subHelper.current) === null || _subHelper$current === void 0 || _subHelper$current.unsubscribe();
      });
    }
    return () => {
      var _subHelper$current2;
      (_subHelper$current2 = subHelper.current) === null || _subHelper$current2 === void 0 || _subHelper$current2.unsubscribe();
    };
  }, []);
  const handleConnect = () => {
    var _window$applicationMa2;
    (_window$applicationMa2 = window.applicationManager) === null || _window$applicationMa2 === void 0 || _window$applicationMa2.connectGenericCog(raft => {
      setConnectedCog(raft);
      subHelper.current = (0, _raftSubscriptionHelpers.raftDisconnectedSubscriptionHelper)(raft);
      subHelper.current.subscribe(() => {
        var _subHelper$current3;
        setConnectedCog(null);
        (_subHelper$current3 = subHelper.current) === null || _subHelper$current3 === void 0 || _subHelper$current3.unsubscribe();
      });
    });
  };
  const handleDisconnect = () => {
    if (connectedCog) {
      var _window$applicationMa3;
      (_window$applicationMa3 = window.applicationManager) === null || _window$applicationMa3 === void 0 || _window$applicationMa3.disconnectGeneric(connectedCog);
    }
  };
  (0, _react.useEffect)(() => {
    _EventBus.EventBus.on('sky-ready', sky => {
      const skyHeightScaled = sky.height * sky.scaleFactors.height;
      const skyWidthScaled = sky.width * sky.scaleFactors.width;
      setGameContainerDimensions({
        width: skyWidthScaled + "px",
        height: skyHeightScaled + "px"
      });
    });
    _EventBus.EventBus.on('star-collected', scene_instance => {
      setCollectedStars(scene_instance.collectedStars);
    });
    _EventBus.EventBus.on('target-achieved', () => {
      if (!phaserGameRef.current) return;
      if (!phaserGameRef.current.scene) return;
      setIsWinningFlagVisible(true);
      setTimeout(() => {
        setIsWinningFlagVisible(false);
      }, 2000);
    });
    _EventBus.EventBus.on('die', () => {
      setCollectedStars(0);
    });
    return () => {
      _EventBus.EventBus.removeListener('sky-ready');
      _EventBus.EventBus.removeListener('play-scene-updated');
      _EventBus.EventBus.removeListener('die');
      _EventBus.EventBus.removeListener('star-collected');
    };
  }, []);
  (0, _react.useEffect)(() => {
    if (collectedStars > 0) {
      setIsZooming(true);
      setTimeout(() => {
        setIsZooming(false);
      }, 300);
    }
  }, [collectedStars]);
  const onPlayBtnClick = () => {
    // if cog is connected, start the game, otherwise connect a cog
    if (connectedCog) {
      var _phaserGameRef$curren;
      (_phaserGameRef$curren = phaserGameRef.current) === null || _phaserGameRef$curren === void 0 || (_phaserGameRef$curren = _phaserGameRef$curren.scene) === null || _phaserGameRef$curren === void 0 || _phaserGameRef$curren.handleGameStart();
    } else {
      handleConnect();
    }
  };
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: _AppModule.default.App,
    children: [isWinningFlagVisible && /*#__PURE__*/(0, _jsxRuntime.jsx)(_WonOverlay.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_overlayingTutorial.default, {}), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: _AppModule.default.gameContainer,
      style: gameContainerDimensions,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _AppModule.default.blockSelectorContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_blockSelector.default, {
          gameRef: phaserGameRef
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
        className: _AppModule.default.scoreBoard,
        children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_star.ReactComponent, {
          className: _AppModule.default.starIcon
        }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
          className: "".concat(_AppModule.default.score, " ").concat(isZooming ? _AppModule.default.zoom : ''),
          children: collectedStars
        })]
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _AppModule.default.starTargetContainer,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_starTarget.ReactComponent, {
          className: _AppModule.default.starTargetIcon
        })
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_PhaserGame.PhaserGame, {
        ref: phaserGameRef
      })]
    })]
  });
}
var _default = exports.default = JumpingGame;
function zeroPrepender(num, size) {
  // helper function that prepends zeros to a number if it is less than a certain size
  // and also keeps only the first 'size' digits of the number if it is greater than 'size'
  let s = num + "";
  while (s.length < size) s = "0" + s;
  return s.slice(0, size);
}