import React, { useEffect, memo } 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 } from "../store/rom/main";

var App = function App(props) {
  var dispatch = useDispatch();

  var _useSelector = useSelector(function (state) {
    return {
      assessment: state.rom.main.assessment,
      gallery: state.rom.main.gallery,
      weeklyAssessment: state.rom.main.weeklyAssessment,
      progress: state.rom.main.progress,
      currentExercise: state.rom.main.currentExercise,
      currentTransition: state.rom.main.currentTransition,
      pose: state.rom.main.pose,
      paused: state.rom.main.paused,
      loading: state.rom.main.loading
    };
  }),
      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;

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

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

  var onLoading = function onLoading() {
    dispatch(toggleLoading(false));
  };

  var onExerciseValue = function onExerciseValue(value) {
    dispatch(exerciseValue(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"
  }, /*#__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
  }), /*#__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);