import React, { memo, useEffect } from 'react';
import { useSelector, useDispatch } from 'react-redux';
import Results from "../components/TransistionResults";
import SingleResult from "../components/TransistionSingleResult";
import ReadySetGo from "../components/TransistionReadySetGo";
import Openning from "../components/TransistionOpenning";
import Closing from "../components/TransistionClosing";
import InstructionalVideo from "./InstructionalVideo";
import Tutorial from "./Tutorial";
import SplashPage from "./SplashPage";
import Calibration from "../components/TransistionCalibration";
import { nextTransition, resetResultsExercises, nextExercise, savePhysicalAssessmentsMetrics, getPrintScreen, goToWeeklyAssessment, repetingExercise, setPause, setPlay, setResultExerciseValidateValue, setIsRepetingExercise, setMetricsDataValue, toggleSelfieMode } from "../store/rom/main";
var previousValue = false; // let count = 0;

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

  var _useSelector = useSelector(function (state) {
    return state.rom.main;
  }),
      user = _useSelector.user,
      tutorial = _useSelector.tutorial,
      splash = _useSelector.splash,
      currentExercise = _useSelector.currentExercise,
      currentTransition = _useSelector.currentTransition,
      finishedExercises = _useSelector.finishedExercises,
      resultsExercises = _useSelector.resultsExercises,
      exercises = _useSelector.exercises,
      paused = _useSelector.paused,
      pose = _useSelector.pose,
      identifier = _useSelector.identifier,
      gallery = _useSelector.gallery,
      assessment = _useSelector.assessment,
      session = _useSelector.session,
      isRepetingExercise = _useSelector.isRepetingExercise,
      bodyPartsNotVisible = _useSelector.bodyPartsNotVisible,
      resultExerciseValidation = _useSelector.resultExerciseValidation,
      metricsData = _useSelector.metricsData,
      incorrectSide = _useSelector.incorrectSide;

  var onNextTransition = function onNextTransition(value) {
    if (value === void 0) {
      value = null;
    }

    dispatch(nextTransition());
  };

  var onRepeatExercise = function onRepeatExercise(value) {
    dispatch(repetingExercise(value));
  };

  var onResetResultExercises = function onResetResultExercises() {
    dispatch(resetResultsExercises());
  };

  var onSetIsRepetingExercise = function onSetIsRepetingExercise(value) {
    dispatch(setIsRepetingExercise(value));
  }; // const onToogleSelfieMode = () => {
  //   dispatch(toggleSelfieMode());
  // };


  var onSavePhysicalAssessmentsMetricsToState = function onSavePhysicalAssessmentsMetricsToState() {
    var data = {
      session: session.id,
      identifier: identifier,
      user: user,
      anonymous: true,
      metrics: {
        exercise: currentExercise._id,
        value: pose.angleResult,
        printscreen: getPrintScreen()
      }
    };
    dispatch(setMetricsDataValue(data));
    dispatch(setResultExerciseValidateValue(data));
  };

  var onSavePhysicalAssessmentsMetrics = function onSavePhysicalAssessmentsMetrics() {
    dispatch(savePhysicalAssessmentsMetrics(metricsData));
  };

  var onWeeklyAssessment = function onWeeklyAssessment() {
    dispatch(goToWeeklyAssessment());
  };

  useEffect(function () {
    if (currentTransition === 0) {
      previousValue = false;
    }
  }, [currentTransition]);
  useEffect(function () {
    if (bodyPartsNotVisible.part1 && bodyPartsNotVisible.part2 && bodyPartsNotVisible.part3) {
      if (!previousValue) {
        previousValue = true; // dispatch(setPlay());

        if (currentTransition === 0) onNextTransition();
      }
    } else {
      if (previousValue) {
        previousValue = false;
        if (currentTransition != 3 && currentTransition != 4 && currentTransition != 5) onRepeatExercise(true); // dispatch(setPause());
      }
    }
  }, [bodyPartsNotVisible]);
  useEffect(function () {
    if (currentTransition === 4 && (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.result) >= (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.minAcceptableAngle) && (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.result) <= (resultExerciseValidation === null || resultExerciseValidation === void 0 ? void 0 : resultExerciseValidation.acceptableAngle)) {
      onSetIsRepetingExercise(false);
      onSavePhysicalAssessmentsMetrics();
    }

    if (!splash.isOpened && !tutorial.isOpened && !paused && !gallery.isOpened && !assessment.isOpened && exercises.length > 0 && currentTransition === -1) {
      if (!isRepetingExercise) dispatch(nextExercise());
      onNextTransition();
    }
  }, [tutorial, currentTransition, finishedExercises.finished, paused, isRepetingExercise]);
  return /*#__PURE__*/React.createElement(React.Fragment, null, tutorial.isOpened && /*#__PURE__*/React.createElement(Tutorial, null), splash.isOpened && /*#__PURE__*/React.createElement(SplashPage, null), currentTransition === 0 && /*#__PURE__*/React.createElement(Calibration, {
    currentExercise: currentExercise,
    incorrectSide: incorrectSide
  }), (currentTransition === 1 || currentTransition === 2) && /*#__PURE__*/React.createElement(InstructionalVideo, {
    onNextTransition: onNextTransition
  }), currentTransition === 2 && /*#__PURE__*/React.createElement(ReadySetGo, {
    isPaused: paused,
    onNextTransition: onNextTransition,
    onSavePhysicalAssessmentsMetricsToState: onSavePhysicalAssessmentsMetricsToState
  }), currentTransition === 3 && /*#__PURE__*/React.createElement(Closing, {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), currentTransition === 4 && /*#__PURE__*/React.createElement(SingleResult, {
    isPaused: paused,
    onNextTransition: onNextTransition,
    exercise: currentExercise,
    resultsExercises: resultsExercises,
    onRepeatExercise: onRepeatExercise,
    onSetIsRepetingExercise: onSetIsRepetingExercise,
    resultExerciseValidation: resultExerciseValidation,
    onSavePhysicalAssessmentsMetrics: onSavePhysicalAssessmentsMetrics
  }), currentTransition === 5 && /*#__PURE__*/React.createElement(Openning, {
    isPaused: paused,
    onNextTransition: onNextTransition
  }), finishedExercises.isOpened && finishedExercises.finished && /*#__PURE__*/React.createElement(Results, {
    resultsExercises: resultsExercises,
    onResetResultExercises: onResetResultExercises,
    onWeeklyAssessment: onWeeklyAssessment
  }), props.children);
};

export default memo(Main);