"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.GenericPhase = GenericPhase;
require("core-js/modules/web.dom-collections.iterator.js");
var _react = require("react");
var _greenTick = require("../assets/green-tick.svg");
var _stylesModule = _interopRequireDefault(require("./styles.module.css"));
var _PhaserGame = require("../tutorial-game/PhaserGame");
var _EventBus = require("../tutorial-game/EventBus");
var _jsxRuntime = require("react/jsx-runtime");
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
function GenericPhase(_ref) {
  let {
    nextPhase,
    BlocksConnectedIcon,
    initialBlock
  } = _ref;
  const [gameContainerDimensions, setGameContainerDimensions] = (0, _react.useState)({
    width: "40vw",
    height: "30vh",
    visibility: "hidden"
  });
  const [tickIconState, setTickIconState] = (0, _react.useState)("hidden");
  const [blocksIconState, setBlocksIconState] = (0, _react.useState)("visible");
  const phaserGameRef = (0, _react.useRef)(null);
  const cogClickedCounter = (0, _react.useRef)(0);
  const cogJumpedCounter = (0, _react.useRef)(0);
  const onCogButtonClick = (0, _react.useCallback)(() => {
    cogClickedCounter.current++;
    if (cogClickedCounter.current === 3) {
      var _phaserGameRef$curren;
      setBlocksIconState("animation");
      (_phaserGameRef$curren = phaserGameRef.current) === null || _phaserGameRef$curren === void 0 || (_phaserGameRef$curren = _phaserGameRef$curren.game) === null || _phaserGameRef$curren === void 0 || _phaserGameRef$curren.destroy(true);
    }
  }, []);

  // useEffect(() => {
  //     const connectedCog = getConnectedCog();
  //     if (connectedCog) {
  //         const eventName = (connectedCog as Cog).publishedDataAnalyser.eventsMap.buttonClick.release;
  //         (connectedCog as Cog).publishedDataAnalyser.addListener(eventName, onCogButtonClick);

  //         return () => {
  //             (connectedCog as Cog).publishedDataAnalyser.removeListener(eventName, onCogButtonClick);
  //         };
  //     }
  // }, []);

  (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",
        visibility: "visible"
      });
      if (sky.scene) {
        sky.scene.currentlySectedBlock = initialBlock;
      }
    });
    _EventBus.EventBus.once('player-off-screen', () => {
      var _phaserGameRef$curren2;
      setBlocksIconState("animation");
      (_phaserGameRef$curren2 = phaserGameRef.current) === null || _phaserGameRef$curren2 === void 0 || (_phaserGameRef$curren2 = _phaserGameRef$curren2.game) === null || _phaserGameRef$curren2 === void 0 || _phaserGameRef$curren2.destroy(true);
    });
    _EventBus.EventBus.on('player-jumped', () => {
      cogJumpedCounter.current++;
      if (cogJumpedCounter.current === 3) {
        setTimeout(() => {
          var _phaserGameRef$curren3;
          setBlocksIconState("animation");
          (_phaserGameRef$curren3 = phaserGameRef.current) === null || _phaserGameRef$curren3 === void 0 || (_phaserGameRef$curren3 = _phaserGameRef$curren3.game) === null || _phaserGameRef$curren3 === void 0 || _phaserGameRef$curren3.destroy(true);
        }, 1500);
      }
    });
    return () => {
      _EventBus.EventBus.removeListener('sky-ready');
      _EventBus.EventBus.removeListener('player-off-screen');
      _EventBus.EventBus.removeListener('player-jumped');
    };
  }, [initialBlock]);
  const onBlocksAnimationEnd = (0, _react.useCallback)(() => {
    setBlocksIconState("hidden");
    setTickIconState("animation");
  }, []);
  const onTickIconAnimationEnd = (0, _react.useCallback)(() => {
    nextPhase();
  }, []);
  const blocksIconContainerClasses = [_stylesModule.default.doubleColumnContainer, blocksIconState === "animation" ? _stylesModule.default.fadeOutAnimation : "", blocksIconState === "hidden" ? _stylesModule.default.hide : ""].join(" ");
  const tickIconClassNames = [_stylesModule.default.tickIconFullScreen, tickIconState === "animation" ? _stylesModule.default.fadeInAnimation : "", tickIconState === "hidden" ? _stylesModule.default.hide : "", tickIconState === "visible" ? _stylesModule.default.show : ""].join(" ");
  return /*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
    className: _stylesModule.default.singleColumnContainer,
    children: [/*#__PURE__*/(0, _jsxRuntime.jsxs)("div", {
      className: blocksIconContainerClasses,
      onAnimationEnd: onBlocksAnimationEnd,
      children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(BlocksConnectedIcon, {
        className: _stylesModule.default.blocksConnectedIcon
      }), /*#__PURE__*/(0, _jsxRuntime.jsx)("div", {
        className: _stylesModule.default.gameContainer,
        style: gameContainerDimensions,
        children: /*#__PURE__*/(0, _jsxRuntime.jsx)(_PhaserGame.PhaserGame, {
          ref: phaserGameRef
        })
      })]
    }), /*#__PURE__*/(0, _jsxRuntime.jsx)(_greenTick.ReactComponent, {
      className: tickIconClassNames,
      onAnimationEnd: onTickIconAnimationEnd
    })]
  });
}