private Template GetTemplate(TrainerLevelType level, DaysType days, string templateType) { Template template = null; TemplateFactory templateFactory = new TemplateFactory(level); if (templateType != "DecideForMe") { template = templateFactory.CreateBasicTemplate(Enum.Parse <TemplateType>(templateType)); } else { Random random; if (days <= DaysType.TwoDays) { //FBW template = templateFactory.CreateBasicTemplate(TemplateType.FBW); } if (days == DaysType.ThreeDays) { if (level == TrainerLevelType.Novice) { //FBW template = templateFactory.CreateBasicTemplate(TemplateType.FBW); } if (level == TrainerLevelType.Intermediate) { //FBW/AB (random) var list = new TemplateType[] { TemplateType.AB }; random = new Random(); template = templateFactory.CreateBasicTemplate(list[random.Next(list.Length)]); } if (level >= TrainerLevelType.Advanced) { //AB template = templateFactory.CreateBasicTemplate(TemplateType.AB); } } if (days == DaysType.FourDays) { if (level <= TrainerLevelType.Intermediate) { //AB template = templateFactory.CreateBasicTemplate(TemplateType.AB); } if (level >= TrainerLevelType.Advanced) { // AB, ABC, ABCD (random) var list = new TemplateType[] { TemplateType.ABCD, TemplateType.AB, TemplateType.ABC }; random = new Random(); template = templateFactory.CreateBasicTemplate(list[random.Next(list.Length)]); } } if (days == DaysType.FiveDays) { if (level <= TrainerLevelType.Advanced) { //ABC template = templateFactory.CreateBasicTemplate(TemplateType.ABC); } else { //ABC, ABCD, ABCDE var list = new TemplateType[] { TemplateType.ABCD, TemplateType.ABC }; random = new Random(list.Length); template = templateFactory.CreateBasicTemplate(list[random.Next(list.Length)]); } } if (days == DaysType.SixDays || days == DaysType.SevenDays) { if (level <= TrainerLevelType.Advanced) { //ABC template = templateFactory.CreateBasicTemplate(TemplateType.ABC); } else { //ABC, ABCD, ABCDE, ABCDEF var list = new[] { TemplateType.AB, TemplateType.ABC, TemplateType.ABCD }; random = new Random(list.Length); template = templateFactory.CreateBasicTemplate(list[random.Next(list.Length)]); } } if (template == null) { throw new Exception("could not instantiate template"); } } template.DaysType = days; template.TrainerLevelType = level; foreach (Workout templateWorkout in template.Workouts) { foreach (MuscleExercises templateWorkoutMuscleExercise in templateWorkout.WorkoutHistories.First() .MuscleExercises) { var exercisesOfMuscle = _db.Exercises.Where(x => x.MuscleType == templateWorkoutMuscleExercise.MuscleType).ToList(); for (int i = 0; i < templateWorkoutMuscleExercise.Exercises.Count; i++) { Random r = new Random(); var exerciseSetting = _programService.PickExerciseSetting( _programService.GetRelevantExerciseData(i, level, templateWorkoutMuscleExercise.MuscleType)); var workoutExercise = templateWorkoutMuscleExercise.Exercises[i]; var exerciseChose = _programService.PickExercise(i, exerciseSetting, exercisesOfMuscle); workoutExercise.Name = exerciseChose.Name; var numOfSets = exerciseSetting.Sets[r.Next(exerciseSetting.Sets.Length)]; var reps = _programService.PickReps(exerciseSetting); var rest = _programService.PickRest(exerciseSetting); for (int j = 0; j < numOfSets; j++) { var set = new Set() { Reps = reps, Rest = rest }; workoutExercise.Sets.Add(set); } exercisesOfMuscle.Remove(exerciseChose); } } } return(template); }