private void CopyViewBagBodyExerciseToViewModel(TrainingExercisesViewModel viewModel, List<BodyExercise> bodyExerciseList)
 {
     viewModel.BodyExerciseList = new List<SelectBodyExercise>();
     if (bodyExerciseList != null)
     {
         foreach (BodyExercise bodyExercise in bodyExerciseList)
         {
             viewModel.BodyExerciseList.Add(new SelectBodyExercise() { Id = bodyExercise.Id, Name = bodyExercise.Name });
         }
     }
 }
        public IActionResult AddTrainingExercises(TrainingExercisesViewModel viewModel, string buttonType)
        {
            if (IncorrectHttpData(viewModel.UserId, viewModel.Year, viewModel.WeekOfYear, viewModel.DayOfWeek, viewModel.TrainingDayId))
                return View(viewModel);

            bool displayMessage = buttonType == "submit";

            int currentMuscularGroupId = 0, currentMuscleId = 0;
            if (viewModel != null)
            {
                currentMuscularGroupId = viewModel.MuscularGroupId;
                currentMuscleId = viewModel.MuscleId;
            }

            List<BodyExercise> bodyExerciseList;
            InsertViewBagOnEditTrainingExercise(out bodyExerciseList, currentMuscularGroupId, currentMuscleId);
            ModelState.Clear();

            if (ModelState.IsValid)
            {
                if(viewModel.BodyExerciseList != null)
                { // filter selected data with existing exercise
                    if (bodyExerciseList == null)
                        viewModel.BodyExerciseList = null;
                    else
                    {
                        var deleteList = new List<SelectBodyExercise>();
                        foreach(var selectBodyExercise in viewModel.BodyExerciseList)
                        {
                            if(bodyExerciseList.FirstOrDefault(c => c.Id == selectBodyExercise.Id) == null)
                            {
                                deleteList.Add(selectBodyExercise);
                            }
                        }

                        foreach (var selectBodyExercise in deleteList)
                        {
                            viewModel.BodyExerciseList.Remove(selectBodyExercise);
                        }
                    }
                }

                if (viewModel.BodyExerciseList == null || viewModel.BodyExerciseList.Count(b => b.Selected == true) == 0)
                {
                    CopyViewBagBodyExerciseToViewModel(viewModel, bodyExerciseList);
                    if (displayMessage)
                        ModelState.AddModelError(string.Empty, string.Format(Translation.THE_P0_FIELD_IS_REQUIRED, Translation.BODY_EXERCISES));
                    return View(viewModel);
                }

                var trainingDayKey = new TrainingDayKey() { UserId = viewModel.UserId, Year = viewModel.Year, WeekOfYear = viewModel.WeekOfYear, DayOfWeek = viewModel.DayOfWeek, TrainingDayId = viewModel.TrainingDayId };
                var trainingDayScenario = new TrainingDayScenario() { ManageExercise = true };
                var trainingDay = _trainingDaysService.GetTrainingDay(trainingDayKey, trainingDayScenario);

                if(trainingDay == null)
                {
                    CopyViewBagBodyExerciseToViewModel(viewModel, bodyExerciseList);
                    ModelState.AddModelError(string.Empty, string.Format(Translation.P0_NOT_EXIST, Translation.TRAINING_DAY));
                    return View(viewModel);
                }

                if (trainingDay.TrainingExercises == null)
                    trainingDay.TrainingExercises = new List<TrainingExercise>();

                int bodyExerciseCount = trainingDay.TrainingExercises.Count;
                int maxId = 1;
                if (bodyExerciseCount > 0)
                    maxId = trainingDay.TrainingExercises.Max(t => t.Id) + 1;
                TrainingExercise trainingExercise;
                var bodyExerciseSelectedList = viewModel.BodyExerciseList.Where(b => b.Selected == true);
                foreach (var bodyExercise in bodyExerciseSelectedList)
                {
                    //Only manage add in this page
                    trainingExercise = new TrainingExercise()
                    {
                        UserId = viewModel.UserId,
                        Year = viewModel.Year,
                        WeekOfYear = viewModel.WeekOfYear,
                        DayOfWeek = viewModel.DayOfWeek,
                        TrainingDayId = viewModel.TrainingDayId,
                        Id = maxId,
                        BodyExerciseId = bodyExercise.Id,
                        RestTime = 0,
                        EccentricContractionTempo = 1,
                        StretchPositionTempo = 0,
                        ConcentricContractionTempo = 1,
                        ContractedPositionTempo = 0
                    };
                    trainingDay.TrainingExercises.Add(trainingExercise);
                    maxId++;
                }
                if(bodyExerciseCount != trainingDay.TrainingExercises.Count)
                { //data changed
                    _trainingDaysService.UpdateTrainingDay(trainingDay, trainingDayScenario);
                }

                return GetViewActionResult(viewModel.UserId, viewModel.Year, viewModel.WeekOfYear, viewModel.DayOfWeek);
            }

            CopyViewBagBodyExerciseToViewModel(viewModel, bodyExerciseList);
            return View(viewModel);
        }
        public IActionResult AddTrainingExercises(string userId, int year, int weekOfYear, int dayOfWeek, int trainingDayId)
        {
            if (IncorrectHttpData(userId, year, weekOfYear, dayOfWeek, trainingDayId))
                return RedirectToAction("Index");

            List<BodyExercise> bodyExerciseList;
            InsertViewBagOnEditTrainingExercise(out bodyExerciseList);

            var viewModel = new TrainingExercisesViewModel();
            CopyViewBagBodyExerciseToViewModel(viewModel, bodyExerciseList);
            return View(viewModel);
        }