"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _react = _interopRequireWildcard(require("react"));

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

var _antd = require("antd");

var _all = require("react-icons/all");

var _axios = _interopRequireDefault(require("axios"));

var _VideoPoseEstimator = _interopRequireDefault(require("../components/VideoPoseEstimator"));

var _TemplateBottom = _interopRequireDefault(require("../components/TemplateBottom"));

var _TemplateTop = _interopRequireDefault(require("../components/TemplateTop"));

var _PreAssessment = _interopRequireDefault(require("./PreAssessment"));

var _WeeklyAssessment = _interopRequireDefault(require("./WeeklyAssessment"));

var _ScreenGallery = _interopRequireDefault(require("./ScreenGallery"));

var _TemplateProgressBar = _interopRequireDefault(require("../components/TemplateProgressBar"));

var _Menu = _interopRequireDefault(require("./Menu"));

var _Main = _interopRequireDefault(require("./Main"));

require("./style.css");

var _main = require("../store/rom/main");

function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }

function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }

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

function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

var App = function App(props) {
  var _currentExercise$labe;

  var _useState = (0, _react.useState)(false),
      isFullscreen = _useState[0],
      setFullscreen = _useState[1];

  var fullscreenRef = (0, _react.useRef)(null);
  var dispatch = (0, _reactRedux.useDispatch)();

  var _useSelector = (0, _reactRedux.useSelector)(function (state) {
    return state.rom.main;
  }),
      assessment = _useSelector.assessment,
      gallery = _useSelector.gallery,
      weeklyAssessment = _useSelector.weeklyAssessment,
      progress = _useSelector.progress,
      currentExercise = _useSelector.currentExercise,
      currentTransition = _useSelector.currentTransition,
      pose = _useSelector.pose,
      paused = _useSelector.paused,
      loading = _useSelector.loading,
      aspectArea = _useSelector.aspectArea,
      selfieMode = _useSelector.selfieMode,
      facingMode = _useSelector.facingMode;

  (0, _react.useEffect)(function () {
    onResizeWindow();
  }, []);

  var onResizeWindowByHeight = function onResizeWindowByHeight() {
    var width = 16 / 9 * window.innerHeight;
    var height = window.innerHeight;
    dispatch((0, _main.updateAspectArea)({
      width: width,
      height: height
    }));
  };

  var onResizeWindowByWidth = function onResizeWindowByWidth() {
    var width = window.innerWidth;
    var height = 9 / 16 * window.innerWidth;
    dispatch((0, _main.updateAspectArea)({
      width: width,
      height: height
    }));
  };

  var onResizeWindow = function onResizeWindow() {
    var width = window.innerWidth;
    var height = window.innerHeight;

    if (width / height >= 1.77) {
      onResizeWindowByHeight();
    } else {
      onResizeWindowByWidth();
    }
  };

  (0, _react.useEffect)(function () {
    _axios["default"].defaults.baseURL = props.baseURL;
    dispatch((0, _main.updateUser)(props.user));
    dispatch((0, _main.fetchExercises)());
    window.onresize = onResizeWindow;
  }, []);

  var onTogglePlayPause = function onTogglePlayPause() {
    dispatch((0, _main.togglePlayPause)());
  };

  var onLoading = (0, _react.useCallback)(function () {
    dispatch((0, _main.toggleLoading)(false));
  }, [loading]);

  var onExerciseValue = function onExerciseValue(value) {
    dispatch((0, _main.exerciseValue)(value));
  };

  var onBodyPartNotVisible = function onBodyPartNotVisible(value) {
    dispatch((0, _main.getBodyPartsNotVisible)(value));
  };

  var onShowIncorrectSide = function onShowIncorrectSide(value) {
    dispatch((0, _main.setIncorrectSide)(value));
  };

  var toogleFacingMode = function toogleFacingMode() {
    dispatch((0, _main.setFacingMode)());
  };

  var onFullscreen = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
      return _regenerator["default"].wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              if (!(document.fullscreenElement !== null)) {
                _context.next = 5;
                break;
              }

              _context.next = 3;
              return document.exitFullscreen();

            case 3:
              _context.next = 7;
              break;

            case 5:
              _context.next = 7;
              return fullscreenRef.current.requestFullscreen();

            case 7:
              setFullscreen(document.fullscreenElement !== null);

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }));

    return function onFullscreen() {
      return _ref.apply(this, arguments);
    };
  }();

  var conditionalTerm = currentTransition === 0 || currentTransition === 1 || currentTransition === 2 && (currentExercise === null || currentExercise === void 0 ? void 0 : currentExercise.label);
  var bottomTitle = conditionalTerm ? currentExercise === null || currentExercise === void 0 ? void 0 : (_currentExercise$labe = currentExercise.label) === null || _currentExercise$labe === void 0 ? void 0 : _currentExercise$labe.toUpperCase() : null;
  var bottomResult = conditionalTerm ? Math.floor(pose.angleResult) : null;
  return /*#__PURE__*/_react["default"].createElement("div", {
    ref: fullscreenRef,
    className: "estimator-template " + (isFullscreen ? 'fullscreen' : ''),
    style: {
      width: aspectArea.width + "px",
      height: aspectArea.height + "px"
    }
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "coach-container " + (isFullscreen ? 'fullscreen' : '')
  }, assessment.isOpened && /*#__PURE__*/_react["default"].createElement(_PreAssessment["default"], null), weeklyAssessment.isOpened && /*#__PURE__*/_react["default"].createElement(_WeeklyAssessment["default"], null), gallery.isOpened && /*#__PURE__*/_react["default"].createElement(_ScreenGallery["default"], null), /*#__PURE__*/_react["default"].createElement(_VideoPoseEstimator["default"], {
    loading: loading,
    currentExercise: currentExercise,
    onLoading: onLoading,
    onExerciseValue: onExerciseValue,
    onBodyPartNotVisible: onBodyPartNotVisible,
    selfieMode: selfieMode,
    onShowIncorrectSide: onShowIncorrectSide,
    facingMode: facingMode
  }), /*#__PURE__*/_react["default"].createElement(_Main["default"], null), /*#__PURE__*/_react["default"].createElement("div", {
    className: "overlay-container"
  }, /*#__PURE__*/_react["default"].createElement("div", {
    className: "overlay-coach"
  }, /*#__PURE__*/_react["default"].createElement(_TemplateTop["default"], null, /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    className: "btn-invert-cam",
    type: "primary",
    shape: "circle",
    icon: /*#__PURE__*/_react["default"].createElement(_all.MdFlipCameraAndroid, {
      size: "22px"
    }),
    size: "large",
    onClick: toogleFacingMode
  }), /*#__PURE__*/_react["default"].createElement(_antd.Button, {
    className: "btn-fullscreen",
    type: "primary",
    shape: "circle",
    icon: document.fullscreenElement ? /*#__PURE__*/_react["default"].createElement(_all.MdFullscreenExit, {
      size: "22px"
    }) : /*#__PURE__*/_react["default"].createElement(_all.MdFullscreen, {
      size: "22px"
    }),
    size: "large",
    onClick: onFullscreen
  }), /*#__PURE__*/_react["default"].createElement(_TemplateProgressBar["default"], {
    progress: progress
  })), /*#__PURE__*/_react["default"].createElement(_TemplateBottom["default"] // onTogglePlayPause={onTogglePlayPause}
  , {
    paused: paused,
    title: bottomTitle,
    result: bottomResult
  }), /*#__PURE__*/_react["default"].createElement(_Menu["default"], null)))));
};

var _default = (0, _react.memo)(App);

exports["default"] = _default;
module.exports = exports.default;