// Use this for initialization
    void Start()
    {
        KinectManager = GameObject.Find("KinectManager");

        _kinectManager = KinectManager.GetComponent <KinectManager>();

        if (_kinectManager.displayUserMapSmall)
        {
            _kinectManager.displayUserMapSmall = false;
        }

        Color32 green = MainColors.GreenLight();

        foreach (var side in UserDataObject.GetCurrentExercise().sides)
        {
            if (side.accomplished)
            {
                if (side.direction == "Left")
                {
                    toggleLeft.isOn               = true;
                    imageToggleLeftBG.sprite      = Resources.Load <Sprite>("Images/Toggle");
                    imageToggleLeftBG.color       = MainColors.ToggleIsOn();
                    imageToggleLeftBGShadow.color = MainColors.SideSelectionShadow();
                }
                else if (side.direction == "Right")
                {
                    toggleRight.isOn               = true;
                    imageToggleRightBG.sprite      = Resources.Load <Sprite>("Images/Toggle");
                    imageToggleRightBG.color       = MainColors.ToggleIsOn();
                    imageToggleRightBGShadow.color = MainColors.SideSelectionShadow();
                }
            }
        }

        exerciseLeftButton.onClick.AddListener(() =>
        {
            PlayerPrefs.SetInt("CurrentSideId", 1);
            LoadNextScene("Left");
        });

        exerciseRightButton.onClick.AddListener(() =>
        {
            PlayerPrefs.SetInt("CurrentSideId", 0);
            LoadNextScene("Right");
        });

        exerciseName.text = UserDataObject.GetCurrentExerciseName().ToUpper();
    }
    private void OnGestureDetected(object sender, GestureEventArgs e, int bodyIndex)
    {
        var isDetected = e.IsBodyTrackingIdValid && e.IsGestureDetected;

        if (_kinectManager && _kinectManager.IsInitialized() && _kinectManager.IsUserDetected())
        {
            long userId = _kinectManager.GetPrimaryUserID();

            if (_kinectManager.IsJointTracked(userId, (int)_jointFootRight) &&
                _kinectManager.IsJointTracked(userId, (int)_jointFootLeft))
            {
                float jointPosFootLeftHeight  = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).y;
                float jointPosFootRightHeight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).y;
                float jointPosFootLeftDepth   = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).z;
                float jointPosFootRightDepth  = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).z;

//				if ((jointPosFootLeftDepth > jointPosFootRightDepth - _footDepthTolerance) && (jointPosFootLeftDepth < jointPosFootRightDepth + _footDepthTolerance) &&
//				     (jointPosFootRightDepth > jointPosFootLeftDepth - _footDepthTolerance) && (jointPosFootRightDepth < jointPosFootLeftDepth + _footDepthTolerance))
//				{
//					_inStartingPosition = true;
//				}
//				else if (!_bothFeetUp)
//				{
//					_inStartingPosition = false;
//				}
                _inStartingPosition = true;

                rightFootHeightText.text = "R Height: " + jointPosFootRightHeight.ToString();
                leftFootHeightText.text  = "L Height: " + jointPosFootLeftHeight.ToString();
                rightFootDepthText.text  = "R Depth: " + jointPosFootRightDepth.ToString();
                leftFootDepthText.text   = "L Depth: " + jointPosFootLeftDepth.ToString();

                // Check if in starting position and foot currentFootHeight is between initialFootHeight + difference tolerance
                if (_inStartingPosition &&
                    (jointPosFootLeftHeight > _initialStartingHeightLeft + _startingHeightDifference) &&
                    (jointPosFootRightHeight > _initialStartingHeightRight + _startingHeightDifference))
                {
                    _bothFeetUp = true;
                }
                else
                {
                    _bothFeetUp = false;
                }
            }
        }
        // Check positions of joints of current exercise
        foreach (var check in UserDataObject.GetCurrentChecksArray())
        {
            if (_exerciseExecutionValidationManager.GetMethodToCheck(check))
            {
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkToggle.isOn  = true;
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.sprite = Resources.Load <Sprite>("Images/Toggle");
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.color  = MainColors.ToggleIsOn();
                _methodCheckedArray[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)]             = true;
            }
            else
            {
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkToggle.isOn  = false;
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.sprite = Resources.Load <Sprite>("Images/IconNotChecked");
                checkItemList[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)].checkImage.color  = MainColors.White();
                _methodCheckedArray[Array.IndexOf(UserDataObject.GetCurrentChecksArray(), check)]             = false;
                _checksPassed = false;
            }

            // Debug.Log(_exerciseExecutionValidationManager.GetMethodToCheck(check));
            armsUpText.text = _checksPassed.ToString();
        }

        bothFeetUpText.text = "DIFF: " + _startingHeightDifference + " |UP: " + _bothFeetUp + "| INPOS: " + _inStartingPosition;

        if (_exerciseExecutionValidationManager.LeftFootOnLine() || _exerciseExecutionValidationManager.RightFootOnLine())
        {
            if (CanvasHandCursor.activeSelf)
            {
                _interactionManager.enabled = false;
                CanvasHandCursor.gameObject.SetActive(false);
            }
        }
        // Discrete Gesture tracking
        if ((e.GestureType == GestureType.Discrete))
        {
            _gestureAccuracy = e.DetectionConfidence;

            for (int i = 0; i < _methodCheckedArray.Length; i++)
            {
                if (_methodCheckedArray[i] == false)
                {
                    _gestureAccuracy -= 0.1f;
                }
                testText.text = _gestureAccuracy.ToString();
            }

            if (_gestureAccuracy > 1.0f)
            {
                _gestureAccuracy = 1.0f;
            }
            _durationManager.SetProgress(_gestureAccuracy);

            if (GestureDetected(_gestureAccuracy, 0.4f, 1f) && _bothFeetUp)
            {
                if (CanvasHandCursor.activeSelf)
                {
                    _interactionManager.enabled = false;
                    CanvasHandCursor.gameObject.SetActive(false);
                }

                _durationManager.StartTimer();

                if (MinTimeReached())
                {
                    audioSuccess.Play();

                    if (_currentRepetition == UserDataObject.GetCurrentRepetitionsArray().Last())
                    {
                        Debug.Log("LASTREP");
                        successMainText.text = "Exercise finished!";
                        successSubText.text  = "Preparing next exercise";
                        StartCoroutine("StartLastAnimateSuccess");
                    }
                    else
                    {
                        StartCoroutine("StartAnimateSuccess");
                    }
                }

                confidenceIterator++;
                _currentRepetitionConfidence += _gestureAccuracy * 100;

//				testText.text = "if DISCRETE: " +  e.IsGestureDetected.ToString() + " " + _gestureAccuracy;
            }
            else
            {
                if (_durationManager.IsTimerRunning() && _durationManager.GetlatestTimeInSeconds() <= _currentRepetition.minTime)
                {
                    audioFail.Play();
                    Debug.Log("Activate handcursor");

//					if (!CanvasHandCursor.activeSelf)
//					{
//						CanvasHandCursor.gameObject.SetActive(true);
//						_interactionManager.enabled = true;
//					}
                }

                if (_durationManager.IsTimerRunning())
                {
                    attemptsIterator++;
                }
                _durationManager.StopTimer();

                // if tracked time is greater than given time of the repetition
                if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime)
                {
                    _repsIterator += 1;
                    ToggleAndCheckRepetition();
                }

//				testText.text = "else DISCRETE: " +  e.IsGestureDetected.ToString() + " " + _gestureAccuracy;
            }
//				testText.text = "DISCRETE: " +  e.IsGestureDetected.ToString() + " " + e.DetectionConfidence;
        }
        else if ((e.GestureType == GestureType.Continuous))
        {
            _gestureAccuracy = e.Progress;

            for (int i = 0; i < _methodCheckedArray.Length; i++)
            {
                if (_methodCheckedArray[i] == false)
                {
                    _gestureAccuracy -= 0.1f;
                }
                testText.text = _gestureAccuracy.ToString();
            }

            if (_gestureAccuracy > 1.0f)
            {
                _gestureAccuracy = 1.0f;
            }
            _durationManager.SetProgress(_gestureAccuracy);

//			if (_thirdCheckpoint)
//			{
//				_durationManager.StartTimer();
//
//				confidenceIterator++;
//				_currentRepetitionConfidence += e.Progress * 100;
//
//				if (e.Progress <= 0.4f)
//				{
//					Debug.Log(e.Progress);
//					if (_durationManager.IsTimerRunning())
//					{
//						_currentRepetition.attempts++;
//						Debug.Log("CURRENT REPETITION ATTEMPT: " + _currentRepetition.attempts);
//
//					}
//
//					_durationManager.StopTimer();
//
//					_thirdCheckpoint = false;
//
//					// if tracked time is greater than given time of the repetition
//					if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime)
//					{
//						ToggleAndCheckRepetition();
//					}
//				}
//			}
//			else
//			{
//				if (_secondCheckpoint && GestureDetected(e.Progress, 0.7f, 1f))
//				{
//					_thirdCheckpoint = true;
//				}
//				else if (_firstCheckpoint && GestureDetected(e.Progress, 0.4f, 0.7f))

//				Debug.Log(_bothFeetUp +  " | " + _gestureAccuracy + " | " + e.Progress + " | "+ GestureDetected(_gestureAccuracy, 0.7f, 1f) + " | " + GestureDetected(e.Progress, 0.7f, 1f));

            if (GestureDetected(_gestureAccuracy, _progressMinConfidence, 1f) && _bothFeetUp)
            {
                if (CanvasHandCursor.activeSelf)
                {
                    _interactionManager.enabled = false;
                    CanvasHandCursor.gameObject.SetActive(false);
                }
                _durationManager.StartTimer();

                if (MinTimeReached())
                {
                    audioSuccess.Play();

                    if (_currentRepetition == UserDataObject.GetCurrentRepetitionsArray().Last())
                    {
                        Debug.Log("LASTREP");

                        successMainText.text = "Exercise finished!";
                        successSubText.text  = "Preparing next exercise";
                        StartCoroutine("StartLastAnimateSuccess");
                    }
                    else
                    {
                        StartCoroutine("StartAnimateSuccess");
                    }
                }

                confidenceIterator++;
                _currentRepetitionConfidence += _gestureAccuracy * 100;

//					testText.text = "if CONTINUOUS: " + _gestureAccuracy;
            }
            else
            {
                if (_durationManager.IsTimerRunning() && _durationManager.GetlatestTimeInSeconds() <= _currentRepetition.minTime)
                {
                    audioFail.Play();
                    Debug.Log("Activate handcursor");
//						if (!CanvasHandCursor.activeSelf)
//						{
//							CanvasHandCursor.gameObject.SetActive(true);
//							_interactionManager.enabled = true;
//						}
                }

                if (_durationManager.IsTimerRunning())
                {
                    attemptsIterator++;
                    Debug.Log("CURRENT REPETITION ATTEMPT: " + attemptsIterator);
                    _durationManager.StopTimer();
                }

                // if tracked time is greater than given time of the repetition
                if (_durationManager.GetlatestTimeInSeconds() >= _currentRepetition.minTime)
                {
                    _repsIterator += 1;
                    ToggleAndCheckRepetition();
                }

//					testText.text = "else CONTINUOUS: " + _gestureAccuracy;
            }
//			}
        }
    }