import _regeneratorRuntime from "@babel/runtime/regenerator";
var _excluded = ["redirectTo"];

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

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, useRef, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Button, Modal } from 'antd';
import { MdFlipCameraAndroid, MdFullscreen, MdFullscreenExit, AiOutlineExclamationCircle } from 'react-icons/all';
import { useIdleTimer } from 'react-idle-timer';
import axios from 'axios';
import Menu from "./Menu";
import Main from "./Main";
import Bottom from "../components/TemplateBottom";
import Top from "../components/TemplateTop";
import TemplateProgressBar from "../components/TemplateProgressBar";
import { fetchExercises, updateProps, fetchExercisesEffort, updateAspectArea, setFacingMode, initSequence, recoverySession } from "../store/rehab/main";
import "./style.css";
var baseHeight = 130;
var baseWidth = 231.11;

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

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

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

  var _useSelector = useSelector(function (state) {
    return state.rehab.main;
  }),
      shouldRedirect = _useSelector.shouldRedirect,
      currentExercise = _useSelector.currentExercise,
      progress = _useSelector.progress,
      aspectArea = _useSelector.aspectArea,
      isPhysioterapist = _useSelector.isPhysioterapist,
      continueWhereLeftOff = _useSelector.continueWhereLeftOff,
      recordConsult = _useSelector.recordConsult;

  var handleOnIdle = function handleOnIdle(event) {
    if (!props.isPhysioterapist) {
      Modal.destroyAll();
      Modal.confirm({
        title: 'Confirm',
        icon: /*#__PURE__*/React.createElement("div", {
          className: 'iconModalInativity'
        }, /*#__PURE__*/React.createElement(AiOutlineExclamationCircle, {
          size: 24,
          color: "#faad14"
        })),
        content: 'Would you like to continue?',
        okText: 'Yes',
        cancelText: 'No',
        zIndex: 1020,
        onCancel: function onCancel() {
          if (props === null || props === void 0 ? void 0 : props.redirectTo) props.redirectTo();
        }
      });
    }
  };

  var _ = useIdleTimer({
    timeout: 1000 * 60 * 5,
    onIdle: handleOnIdle,
    debounce: 500
  });

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

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


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

  useEffect(function () {
    axios.defaults.baseURL = props.baseURL;

    var redirectTo = props.redirectTo,
        rest = _objectWithoutPropertiesLoose(props, _excluded);

    dispatch(updateProps(rest));

    if (!props.isPhysioterapist) {
      dispatch(fetchExercises(props.user));
      dispatch(fetchExercisesEffort());
      dispatch(recoverySession(props.user));
    }

    window.onresize = onResizeWindow;
    onResizeWindow();
  }, []);
  useEffect(function () {
    if (shouldRedirect && (props === null || props === void 0 ? void 0 : props.redirectTo)) {
      props.redirectTo();
      dispatch(initSequence());
    }
  }, [shouldRedirect]);
  useEffect(function () {
    if (continueWhereLeftOff && !props.isPhysioterapist) {
      Modal.destroyAll();
      Modal.confirm({
        title: 'Confirm',
        icon: /*#__PURE__*/React.createElement("div", {
          className: 'iconModalInativity'
        }, /*#__PURE__*/React.createElement(AiOutlineExclamationCircle, {
          size: 24,
          color: "#faad14"
        })),
        content: 'Continue where you left off?',
        okText: 'Yes',
        cancelText: 'No',
        onCancel: function onCancel() {
          dispatch(initSequence());
        }
      });
    }
  }, [continueWhereLeftOff]); // console.log('RECORD CONSULT:', recordConsult);

  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' : '')
  }, /*#__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
  }), !isPhysioterapist && /*#__PURE__*/React.createElement(TemplateProgressBar, {
    progress: progress
  })), /*#__PURE__*/React.createElement(Bottom, {
    title: (currentExercise === null || currentExercise === void 0 ? void 0 : currentExercise.rehabExerciseLibraryId) ? currentExercise === null || currentExercise === void 0 ? void 0 : (_currentExercise$reha = currentExercise.rehabExerciseLibraryId) === null || _currentExercise$reha === void 0 ? void 0 : _currentExercise$reha.title : (currentExercise === null || currentExercise === void 0 ? void 0 : currentExercise.title) ? currentExercise === null || currentExercise === void 0 ? void 0 : currentExercise.title : recordConsult.title
  }), /*#__PURE__*/React.createElement(Menu, null))), /*#__PURE__*/React.createElement(Main, {
    rehabAction: props.rehabAction
  })));
};

export default memo(App);