import _regeneratorRuntime from "@babel/runtime/regenerator";

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); }); }; }

import React, { useEffect, memo, useCallback, useState, useRef } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Button } from 'antd';
import { MdFlipCameraAndroid, MdFullscreen, MdFullscreenExit } from 'react-icons/all';
import axios from 'axios';
import VideoPoseEstimator from "../components/VideoPoseEstimator";
import Bottom from "../components/TemplateBottom";
import Top from "../components/TemplateTop";
import PreAssessment from "./PreAssessment";
import WeeklyAssessment from "./WeeklyAssessment";
import ScreenGallery from "./ScreenGallery";
import TemplateProgressBar from "../components/TemplateProgressBar";
import Menu from "./Menu";
import Main from "./Main";
import "./style.css";
import { fetchExercises, updateUser, togglePlayPause, exerciseValue, toggleLoading, getBodyPartsNotVisible, updateAspectArea, setIncorrectSide, setFacingMode, resetResultsExercises } from "../store/rom/main";

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

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

  var fullscreenRef = useRef(null);
  var dispatch = useDispatch();

  var _useSelector = 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;

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

  var onResizeWindowByWidth = function onResizeWindowByWidth() {
    var width = window.innerWidth;
    var height = 9 / 16 * window.innerWidth;
    dispatch(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();
    }
  };

  useEffect(function () {
    axios.defaults.baseURL = props.baseURL;
    window.onresize = onResizeWindow;
    onResizeWindow();
    dispatch(resetResultsExercises());
    dispatch(updateUser(props.user));
    dispatch(fetchExercises());
  }, []);

  var onTogglePlayPause = function onTogglePlayPause() {
    dispatch(togglePlayPause());
  };

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

  var onExerciseValue = function onExerciseValue(value) {
    dispatch(exerciseValue(value));
  };

  var onBodyPartNotVisible = function onBodyPartNotVisible(value) {
    dispatch(getBodyPartsNotVisible(value));
  };

  var onShowIncorrectSide = function onShowIncorrectSide(value) {
    dispatch(setIncorrectSide(value));
  };

  var toogleFacingMode = function toogleFacingMode() {
    dispatch(setFacingMode());
  };

  var onFullscreen = /*#__PURE__*/function () {
    var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.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.createElement("div", {
    ref: fullscreenRef,
    className: "estimator-template " + (isFullscreen ? 'fullscreen' : ''),
    style: {
      width: aspectArea.width + "px",
      height: aspectArea.height + "px"
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "coach-container " + (isFullscreen ? 'fullscreen' : '')
  }, assessment.isOpened && /*#__PURE__*/React.createElement(PreAssessment, null), weeklyAssessment.isOpened && /*#__PURE__*/React.createElement(WeeklyAssessment, null), gallery.isOpened && /*#__PURE__*/React.createElement(ScreenGallery, null), /*#__PURE__*/React.createElement(VideoPoseEstimator, {
    loading: loading,
    currentExercise: currentExercise,
    onLoading: onLoading,
    onExerciseValue: onExerciseValue,
    onBodyPartNotVisible: onBodyPartNotVisible,
    selfieMode: selfieMode,
    onShowIncorrectSide: onShowIncorrectSide,
    facingMode: facingMode
  }), /*#__PURE__*/React.createElement(Main, null), /*#__PURE__*/React.createElement("div", {
    className: "overlay-container"
  }, /*#__PURE__*/React.createElement("div", {
    className: "overlay-coach"
  }, /*#__PURE__*/React.createElement(Top, null, /*#__PURE__*/React.createElement(Button, {
    className: "btn-invert-cam",
    type: "primary",
    shape: "circle",
    icon: /*#__PURE__*/React.createElement(MdFlipCameraAndroid, {
      size: "22px"
    }),
    size: "large",
    onClick: toogleFacingMode
  }), /*#__PURE__*/React.createElement(Button, {
    className: "btn-fullscreen",
    type: "primary",
    shape: "circle",
    icon: document.fullscreenElement ? /*#__PURE__*/React.createElement(MdFullscreenExit, {
      size: "22px"
    }) : /*#__PURE__*/React.createElement(MdFullscreen, {
      size: "22px"
    }),
    size: "large",
    onClick: onFullscreen
  }), /*#__PURE__*/React.createElement(TemplateProgressBar, {
    progress: progress
  })), /*#__PURE__*/React.createElement(Bottom // onTogglePlayPause={onTogglePlayPause}
  , {
    paused: paused,
    title: bottomTitle,
    result: bottomResult
  }), /*#__PURE__*/React.createElement(Menu, null)))));
};

export default memo(App);