public IActionResult EditTrainingExercise(TrainingExerciseViewModel viewModel, string buttonType)
        {
            const int MAX_REPS = 10;
            if(viewModel == null)
                return RedirectToAction("Index");

            ViewBag.UserUnit = GetUserUnit(viewModel.UserId);

            if (viewModel.Reps == null || viewModel.Reps.Count == 0) //Security
                viewModel.Reps = new List<int?>() { 8, 8, 8, 8 };

            if (viewModel.Weights == null)
                viewModel.Weights = new List<double?>();

            while (viewModel.Reps.Count > MAX_REPS)
            {
                viewModel.Reps.RemoveAt(viewModel.Reps.Count - 1);
            }

            while (viewModel.Weights.Count > MAX_REPS)
            {
                viewModel.Weights.RemoveAt(viewModel.Weights.Count - 1);
            }

            while (viewModel.Weights.Count < viewModel.Reps.Count)
            {
                viewModel.Weights.Add(0);
            }

            for (int i = 0; i < viewModel.Reps.Count; i++)
            {
                if(viewModel.Reps[i] == null)
                    viewModel.Reps[i] = 0;
                if (viewModel.Weights[i] == null)
                    viewModel.Weights[i] = 0;
            }

            if ("addRep" == buttonType)
            {
                if (viewModel.Reps.Count < MAX_REPS)
                {
                    int newRepValue = 8;
                    if (viewModel.Reps.Count > 0)
                        newRepValue = viewModel.Reps[viewModel.Reps.Count - 1].Value;
                    viewModel.Reps.Add(newRepValue);

                    double newWeightValue = 8;
                    if (viewModel.Weights.Count > 0)
                        newWeightValue = viewModel.Weights[viewModel.Weights.Count - 1].Value;
                    viewModel.Weights.Add(newWeightValue);
                }
                return View(viewModel);
            }
            else if ("delete" == buttonType)
            {
                if (viewModel.Reps.Count > 1)
                    viewModel.Reps.RemoveAt(viewModel.Reps.Count - 1);
                if (viewModel.Weights.Count > 1)
                    viewModel.Weights.RemoveAt(viewModel.Weights.Count - 1);
            }
            else if ("submit" == buttonType && ModelState.IsValid)
            {
                if (string.IsNullOrWhiteSpace(viewModel.UserId) || SessionUserId != viewModel.UserId || viewModel.Year == 0 || viewModel.WeekOfYear == 0 ||
                    viewModel.DayOfWeek < 0 || viewModel.DayOfWeek > 6 || viewModel.TrainingDayId == 0 || viewModel.TrainingExerciseId == 0 ||
                    viewModel.BodyExerciseId == 0 &&
                    (viewModel.Unit == (int)TUnitType.Imperial || viewModel.Unit == (int)TUnitType.Metric))
                    return View(viewModel);

                var key = new TrainingExerciseKey()
                {
                    UserId = viewModel.UserId,
                    Year = viewModel.Year,
                    WeekOfYear = viewModel.WeekOfYear,
                    DayOfWeek = viewModel.DayOfWeek,
                    TrainingDayId = viewModel.TrainingDayId,
                    Id = viewModel.TrainingExerciseId
                };
                var trainingExercise = _trainingExercisesService.GetTrainingExercise(key);
                if (trainingExercise == null)
                {
                    ModelState.AddModelError(string.Empty, string.Format("{0} {1}", Translation.INVALID_INPUT_2P, Translation.TRAINING_EXERCISE));
                    return View(viewModel);
                }

                trainingExercise.BodyExerciseId = viewModel.BodyExerciseId;
                trainingExercise.RestTime = viewModel.RestTime;

                //Tempos
                trainingExercise.EccentricContractionTempo = viewModel.EccentricContractionTempo;
                trainingExercise.StretchPositionTempo = viewModel.StretchPositionTempo;
                trainingExercise.ConcentricContractionTempo = viewModel.ConcentricContractionTempo;
                trainingExercise.ContractedPositionTempo = viewModel.ContractedPositionTempo;

                if (viewModel.Reps != null && viewModel.Reps.Count > 0)
                {
                    //Regroup Reps with Set
                    int nbSet = 0, currentRepValue = 0;
                    var tupleSetRepList = new List<Tuple<int, int, double>>();
                    int repValue;
                    double weightValue, currentWeightValue = 0;
                    for (int i=0; i < viewModel.Reps.Count; i++)
                    {
                        repValue = viewModel.Reps[i].Value;
                        weightValue = viewModel.Weights[i].Value;
                        if (repValue == 0)
                            continue;

                        if (weightValue == currentWeightValue && repValue == currentRepValue)
                            nbSet++;
                        else
                        {
                            if (nbSet != 0)
                                tupleSetRepList.Add(new Tuple<int, int, double>(nbSet, currentRepValue, currentWeightValue));
                            currentRepValue = repValue;
                            currentWeightValue = weightValue;
                            nbSet = 1;
                        }
                    }

                    //last data
                    if (nbSet != 0)
                        tupleSetRepList.Add(new Tuple<int, int, double>(nbSet, currentRepValue, currentWeightValue));

                    trainingExercise.TrainingExerciseSets = new List<TrainingExerciseSet>();
                    int id = 1;
                    var unit = Utils.IntToEnum<TUnitType>(viewModel.Unit);
                    foreach (Tuple<int, int, double> tupleSetRep in tupleSetRepList)
                    {
                        trainingExercise.TrainingExerciseSets.Add(new TrainingExerciseSet()
                        {
                            UserId = viewModel.UserId,
                            Year = viewModel.Year,
                            WeekOfYear = viewModel.WeekOfYear,
                            DayOfWeek = viewModel.DayOfWeek,
                            TrainingDayId = viewModel.TrainingDayId,
                            TrainingExerciseId = viewModel.TrainingExerciseId,
                            Id = id,
                            NumberOfSets = tupleSetRep.Item1,
                            NumberOfReps = tupleSetRep.Item2,
                            Weight = tupleSetRep.Item3,
                            Unit = unit
                        });
                        id++;
                    }

                    _trainingExercisesService.UpdateTrainingExercise(trainingExercise, true);

                    return RedirectToAction("View", "TrainingJournal", new { Area = "User", userId = viewModel.UserId, year = viewModel.Year, weekOfYear = viewModel.WeekOfYear, dayOfWeekSelected = viewModel.DayOfWeek });

                }
            }
            return View(viewModel);
        }
        public IActionResult EditTrainingExercise(string userId, int year, int weekOfYear, int dayOfWeek, int trainingDayId,
            int trainingExerciseId, bool upward=false, bool downward=false)
        {
            if (IncorrectHttpData(userId, year, weekOfYear, dayOfWeek, trainingDayId, trainingExerciseId))
                return RedirectToAction("Index");

            var actionResult = GetViewActionResult(userId, year, weekOfYear, dayOfWeek);

            if (upward || downward)
            {
                var findcriteria = new TrainingExerciseCriteria()
                {
                    UserId = new StringCriteria() { Equal = userId },
                    Year = new IntegerCriteria() { Equal = year },
                    WeekOfYear = new IntegerCriteria() { Equal = weekOfYear },
                    DayOfWeek = new IntegerCriteria() { Equal = dayOfWeek },
                    TrainingDayId = new IntegerCriteria() { Equal = trainingDayId }
                };
                var trainingExercises = _trainingExercisesService.FindTrainingExercise(findcriteria);
                if (trainingExercises == null || trainingExercises.Count == 0)
                    return actionResult;

                trainingExercises = trainingExercises.OrderBy(t => t.Id).ToList();
                int indexOfCurrentExercice = trainingExercises.FindIndex(t => t.Id == trainingExerciseId);
                if (indexOfCurrentExercice == -1)
                    return actionResult;

                foreach (var trainingExerciseTmp in trainingExercises)
                    _trainingExercisesService.DeleteTrainingExercise(trainingExerciseTmp);

                OrderTrainingExercices(trainingExercises, indexOfCurrentExercice, upward == true);

                foreach (var trainingExerciseTmp in trainingExercises)
                    _trainingExercisesService.CreateTrainingExercise(trainingExerciseTmp);

                return actionResult;
            }

            ViewBag.UserUnit = GetUserUnit(userId);

            var key = new TrainingExerciseKey()
            {
                UserId = userId,
                Year = year,
                WeekOfYear = weekOfYear,
                DayOfWeek = dayOfWeek,
                TrainingDayId = trainingDayId,
                Id = trainingExerciseId
            };
            var trainingExercise = _trainingExercisesService.GetTrainingExercise(key);
            if (trainingExercise == null)
                return actionResult;

            var bodyExercise = _bodyExercisesService.GetBodyExercise(new BodyExerciseKey() { Id = trainingExercise.BodyExerciseId });

            var viewModel = new TrainingExerciseViewModel();
            viewModel.UserId = userId;
            viewModel.Year = year;
            viewModel.WeekOfYear = weekOfYear;
            viewModel.DayOfWeek = dayOfWeek;
            viewModel.TrainingDayId = trainingDayId;
            viewModel.TrainingExerciseId = trainingExerciseId;
            viewModel.BodyExerciseId = bodyExercise.Id;
            viewModel.BodyExerciseName = bodyExercise.Name;
            viewModel.BodyExerciseImage = bodyExercise.ImageName;
            viewModel.RestTime = trainingExercise.RestTime;
            viewModel.EccentricContractionTempo = trainingExercise.EccentricContractionTempo;
            viewModel.StretchPositionTempo = trainingExercise.StretchPositionTempo;
            viewModel.ConcentricContractionTempo = trainingExercise.ConcentricContractionTempo;
            viewModel.ContractedPositionTempo = trainingExercise.ContractedPositionTempo;
            viewModel.Unit = (int)GetUserUnit(userId);

            if (trainingExercise.TrainingExerciseSets != null)
            {
                if(trainingExercise.TrainingExerciseSets.Count > 0) //Take unit of first user set if exist
                    viewModel.Unit = (int)trainingExercise.TrainingExerciseSets[0].Unit;

                foreach (var trainingExerciseSet in trainingExercise.TrainingExerciseSets)
                {
                    for(int i=0; i < trainingExerciseSet.NumberOfSets; i++)
                    {
                        viewModel.Reps.Add(trainingExerciseSet.NumberOfReps);
                        viewModel.Weights.Add(trainingExerciseSet.Weight);
                    }
                }
            }

            if(viewModel.Reps == null || viewModel.Reps.Count == 0)
                viewModel.Reps = new List<int?>() { 8 };
            if (viewModel.Weights == null || viewModel.Weights.Count == 0)
                viewModel.Weights = new List<double?>() { 0 };

            return View(viewModel);
        }