import React, { useEffect, memo, useState } from 'react';
import { useDispatch, useSelector } from 'react-redux';
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 } from "../store/rom/main";
import { useCallback } from 'react';

var App = function App(props) {
  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;

  useEffect(function () {
    onResizeWindow();
  }, []);

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

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

  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 conditionalTerm = (currentTransition === 0 || currentTransition === 1) && (currentExercise === null || currentExercise === void 0 ? void 0 : currentExercise.label);
  var bottomTitle = conditionalTerm ? currentExercise.label.toUpperCase() : null;
  var bottomResult = conditionalTerm ? Math.floor(pose.angleResult) : null;
  return /*#__PURE__*/React.createElement("div", {
    className: "estimator-template",
    style: {
      width: aspectArea.width + "px",
      height: aspectArea.height + "px"
    }
  }, /*#__PURE__*/React.createElement("div", {
    className: "coach-container"
  }, 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
  }), /*#__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(TemplateProgressBar, {
    progress: progress
  })), /*#__PURE__*/React.createElement(Bottom, {
    onTogglePlayPause: onTogglePlayPause,
    paused: paused,
    title: bottomTitle,
    result: bottomResult
  }), /*#__PURE__*/React.createElement(Menu, null)))));
};

export default memo(App);