// Use this for initialization
    void Start()
    {
        // -----------------------------------------
        // ------------ INITIALIZATIONS ------------
        // -----------------------------------------

        CanvasHandCursor = GameObject.Find("CanvasHandCursor");

        // Disable handcursor in execution mode TODO all disable in testmode, enable in production
        bodyManager = GameObject.Find("BodyManager");

        KinectManager = GameObject.Find("KinectManager");
        if (KinectManager == null)
        {
            return;
        }

        _kinectManager      = KinectManager.GetComponent <KinectManager>();
        _interactionManager = _kinectManager.GetComponent <InteractionManager>();

        if (CanvasHandCursor.activeSelf)
        {
            _interactionManager.enabled = false;
            CanvasHandCursor.gameObject.SetActive(false);
        }

        Debug.Log("IsUserDetected: " + _kinectManager.IsUserDetected());


        _exerciseExecutionValidationManager = ExerciseExecutionValidationManager.GetComponent <ExerciseExecutionValidationManager>();

        // Reference to exercise data of current user
//		_currentExerciseData = UserDataObject.currentUser.exerciseData[PlayerPrefs.GetInt("CurrentExerciseId")];
        _currentExerciseData = UserDataObject.GetCurrentExercise();
        _lastExercise        = UserDataObject.GetLastTierExercise();

        // Duration manager
        _durationManager = durationManager.GetComponent <DurationManager>();

        _minTimeAlreadyReached = false;
        _repsIterator          = 0;

        _methodCheckedArray = new bool[UserDataObject.GetCurrentChecksArray().Length];

        // -----------------------------------------
        // ------------- UI COMPONENTS -------------
        // -----------------------------------------

        // Exercise name
        titleText.text       = UserDataObject.GetCurrentExerciseName().ToUpper();
        standingLegText.text = UserDataObject.GetCurrentSide().direction;
        successPanel         = successPanel.GetComponent <CanvasGroup>(); // TODO implement success animation for rep

        foreach (var check in UserDataObject.GetCurrentChecksArray())
        {
            GameObject gameObjectCheckItem = Instantiate(checkItem);
            CheckItem  currentCheckItem    = gameObjectCheckItem.GetComponent <CheckItem>();

            currentCheckItem.methodName       = check.methodName;
            currentCheckItem.description.text = check.description;
            checkItemList.Add(currentCheckItem);

            gameObjectCheckItem.transform.SetParent(checkSpacer, false);
        }

        // Array of Toggles
        _toggleArray = new Toggle[UserDataObject.GetCurrentRepetitionsArray().Length];

        // Create and check toggles for each rep of current exercise
        foreach (var repetition in UserDataObject.GetCurrentRepetitionsArray())
        {
            GameObject gameObjectToggle = Instantiate(toggle);
            Toggle     currentToggle    = gameObjectToggle.GetComponent <Toggle>();

            _toggleArray[Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), repetition)] = currentToggle;

            // Check if exercise not already accomplished
            if (!_currentExerciseData.accomplished)
            {
                // look for accomplished reps, check regarding toggles and set current rep
                if (repetition.accomplished)
                {
                    _repsIterator += 1;
                    currentToggle.GetComponent <Toggle>().isOn = true;
                }
                else if (_currentRepetition == null)
                {
                    _currentRepetition = repetition;
                }
            }
            else             // If exercise already accomplished
            {
                // Set first rep as current rep
                if (_currentRepetition == null)
                {
                    repetition.attempts   = 0;
                    repetition.confidence = 0.0f;
                    repetition.userTime   = 0.0f;

                    _currentRepetition = repetition;
                }
            }
            // Append GO to group
            gameObjectToggle.transform.SetParent(toggleGroup, false);
        }
        textReps.text = "Reps " + _repsIterator + "/" + UserDataObject.GetCurrentRepetitionsArray().Length;

        // Set ID of current repetition
        PlayerPrefs.SetInt("CurrentRepetitionId", Array.IndexOf(UserDataObject.GetCurrentRepetitionsArray(), _currentRepetition));

        // -----------------------------------------
        // ---------------- KINECT -----------------
        // -----------------------------------------

        _bodyManager = bodyManager.GetComponent <BodyManager>();
        if (_bodyManager == null)
        {
            return;
        }

        _kinectSensor = _bodyManager.GetSensor();

        _bodies = _bodyManager.GetBodies();
        Debug.Log(_bodyManager + " | " + _bodies);

        // Initialize gesture detector object
        _gestureDetectorList = new List <GestureDetector>();
        for (int bodyIndex = 0; bodyIndex < _bodies.Length; bodyIndex++)
        {
            _gestureDetectorList.Add(new GestureDetector(_kinectSensor));
        }

        // Foot joints for getting positions
        _jointFootRight           = KinectInterop.JointType.FootRight;
        _jointFootLeft            = KinectInterop.JointType.FootLeft;
        _startingHeightDifference = UserDataObject.GetCurrentExerciseStartingHeightDifference();
        heightTestIterator        = 0;

        // Initial foot position
        if (_kinectManager.IsUserDetected())
        {
            long userId = _kinectManager.GetPrimaryUserID();

            if (_kinectManager.IsJointTracked(userId, (int)_jointFootRight) &&
                _kinectManager.IsJointTracked(userId, (int)_jointFootLeft))
            {
                _initialStartingHeightLeft  = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootLeft).y;
                _initialStartingHeightRight = _kinectManager.GetJointKinectPosition(userId, (int)_jointFootRight).y;

                initialLeftFootText.text  += _initialStartingHeightLeft.ToString();
                initialRightFootText.text += _initialStartingHeightRight.ToString();
            }
        }
        _bothFeetUp         = false;
        _inStartingPosition = false;


        if (UserDataObject.GetCurrentExerciseFileName() == "WalkForward")
        {
            _progressMinConfidence = 0.8f;
        }
        if (UserDataObject.GetCurrentExerciseFileName() == "BobOne")
        {
            _progressMinConfidence = 0.4f;
        }
        Debug.Log("_progressMinConfidence: " + _progressMinConfidence);
    }
    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;
            }
//			}
        }
    }