import React, { memo, useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { Tutorial, Intro, Results, SingleResult, ReadySetGo, Openning, Closing, Silhouette } from "./transitions/index";
import { watchTutorial, nextTransition, resetResultsExercises, nextExercise, savePhysicalAssessmentsMetrics, getPrintScreen } from "../store/coach";
import SplashPage from "./transitions/SplashPage";

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

  var _useSelector = useSelector(function (state) {
    return {
      user: state.coach.user,
      tutorial: state.coach.tutorial,
      splash: state.coach.splash,
      currentExercise: state.coach.currentExercise,
      currentTransition: state.coach.currentTransition,
      finishedExercises: state.coach.finishedExercises,
      resultsExercises: state.coach.resultsExercises,
      exercises: state.coach.exercises,
      paused: state.coach.paused,
      pose: state.coach.pose,
      identifier: state.coach.identifier,
      session: state.coach.session,
      gallery: state.coach.gallery,
      assessment: state.coach.assessment
    };
  }),
      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;

  var onNextTransition = function onNextTransition() {
    dispatch(nextTransition());
  };

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

  var onSavePhysicalAssessmentsMetrics = function onSavePhysicalAssessmentsMetrics() {
    dispatch(savePhysicalAssessmentsMetrics({
      session: session.id,
      identifier: identifier,
      user: user,
      anonymous: true,
      metrics: {
        exercise: currentExercise._id,
        value: pose.angleResult,
        printscreen: getPrintScreen()
      }
    }));
  }; // useEffect(() => {
  //   if (!tutorial.watched) {
  //     dispatch(watchTutorial(true));
  //   }
  // }, [dispatch, tutorial.watched]);


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

export default memo(Transitions);