public UserExerciseGroupViewModel (UserExerciseGroup userExerciseGroup)
		{
			this.ExerciseGroupName = userExerciseGroup.TemplateName;
			this.ID = userExerciseGroup.Id;
			this.ServerID = userExerciseGroup.UserWorkoutGroupID;
			this.ExerciseGroupDescription = userExerciseGroup.TemplateDescription;
			this.UserExerciseGroup = userExerciseGroup;

		}
		public async static Task AddModify(UserExerciseGroup exerciseGroup)
		{


			var db = DependencyService.Get<ISQLite>().GetAsyncConnection();
			await db.CreateTableAsync<UserExerciseGroup>();
			if (exerciseGroup.Id == 0)
			{
				await db.InsertAsync(exerciseGroup);
			}
			else {
				await db.UpdateAsync(exerciseGroup);
			}

		}
		//private static readonly AsyncLock Mutex = new AsyncLock ();
		public async static Task<UserExerciseGroup> CreateUserExerciseGroup(int profileID, int gymID, int serverExerciseGroupID, bool isDeleted, String exerciseGroupName, String exerciseGroupDescription)
		{
			UserExerciseGroup group = new UserExerciseGroup();
			group = new UserExerciseGroup();
			group.TemplateName = exerciseGroupName;
			group.TemplateDescription = exerciseGroupDescription;
			group.ProfileID = profileID;
			group.GymID = gymID;
			group.UserWorkoutGroupID = serverExerciseGroupID;
			group.IsDeleted = isDeleted;


			var db = DependencyService.Get<ISQLite>().GetAsyncConnection();
			await db.CreateTableAsync<UserExerciseGroup>();
			await db.InsertAsync(group);

			return group;
		}
		public async Task WriteToRepository(string userExerciseGroupName, string userExerciseGroupDescription)
		{
			//Update local database with new userExerciseGroup
			UserExerciseGroup userExerciseGroup = new UserExerciseGroup();
			userExerciseGroup.TemplateName = userExerciseGroupName;
			userExerciseGroup.TemplateDescription = userExerciseGroupDescription;
			userExerciseGroup.ProfileID = this.ProfileID;
			userExerciseGroup.GymID = App.WorkoutCreatorContext.StaffMember.GymID;

			await UserExerciseGroupDAL.AddModify(userExerciseGroup);

			await UserExerciseGroupDAL.SyncLocalUserExerciseGroupsAndUserWorkoutTemplateMappingsWithServer(userExerciseGroup.GymID);


			Insights.Track("Assign user workout", new Dictionary<string, string>() {
				{ "workout group name", userExerciseGroupName },
				{ "workout group description", userExerciseGroupDescription },
				{ "Profile id", this.ProfileID.ToString () }
			});

			MessagingCenter.Send<ModalAddUserExerciseGroupPage>(Application.Current.MainPage.Navigation.ModalStack.Last() as ModalAddUserExerciseGroupPage, "WorkoutAssigned");

			Device.BeginInvokeOnMainThread(async () =>
			{
				await Application.Current.MainPage.Navigation.PopModalAsync(false);

				WorkoutReviewPage workoutReviewPage = new WorkoutReviewPage(userExerciseGroup.ProfileID, userExerciseGroup.Id);
				await Application.Current.MainPage.Navigation.PushAsync(workoutReviewPage, false);

				//Manage Xamarin messaging center memory leaks
				workoutReviewPage.ViewModel.AddedPageOrModalToNav = true;

				await Application.Current.MainPage.Navigation.PushAsync(new AddExerciseContainerPage());
			});




		}
		UserExerciseGroup CreateUserExerciseGroup (WorkoutTemplate workoutTemplate)
		{
			UserExerciseGroup userExerciseGroup = new UserExerciseGroup ();
			userExerciseGroup.TemplateName = workoutTemplate.TemplateName;
			userExerciseGroup.TemplateDescription = workoutTemplate.TemplateDescription;
			userExerciseGroup.LastUpdatedTime = DateTime.UtcNow;
			userExerciseGroup.ProfileID = _selectedUser.ProfileID;
			userExerciseGroup.GymID = workoutTemplate.GymID;
			return userExerciseGroup;
		}
		private async void ExecuteAssignTemplateCommand (TemplateViewModel selectedViewModel)
		{

			if (IsBusy)
				return;

			IsBusy = true;

			//Assign member template in the local database
			bool successfulAssignment = await UserDAL.AssignUserTemplate (SelectedUser.ProfileID, selectedViewModel.Id);

			if (successfulAssignment) {

				int gymID = App.WorkoutCreatorContext.StaffMember.GymID;

				//Retrieve WorkoutTemplate serverTemplateID
				WorkoutTemplate workoutTemplate = await WorkoutTemplateDAL.GetWorkoutTemplateByID (gymID, selectedViewModel.Id);
				int serverTemplateID = workoutTemplate.WorkoutTemplateID;

				UserExerciseGroup userExerciseGroup = new UserExerciseGroup ();

				if (CrossConnectivity.Current.IsConnected) {
					//Attempt to assign workout template to the member on the server
					userExerciseGroup = await UserDAL.AddServerTemplateIDLocalAndServer (gymID, SelectedUser.ProfileID, serverTemplateID, false);
				}

				if (userExerciseGroup.Id == 0) {
					//Still need to map userexercisegroups (and exercises) to the user if the API cannot be accessed or the application is not connected to a network
					userExerciseGroup.TemplateDescription = selectedViewModel.TemplateDescription;
					userExerciseGroup.TemplateName = selectedViewModel.TemplateName;
					userExerciseGroup.ExerisesDirty = true;
					userExerciseGroup.LastUpdatedTime = DateTime.UtcNow;
					userExerciseGroup.ProfileID = SelectedUser.ProfileID;
					userExerciseGroup.UserWorkoutGroupID = 0;
					userExerciseGroup.GymID = SelectedUser.GymID;
					userExerciseGroup.LocalTemplateID = selectedViewModel.Id;

					await UserExerciseGroupDAL.AddModify (userExerciseGroup);
				}



				//Create the user exercise mapping in the local database
				//Assumes template exercise mappings were already made locally
				await UserWorkoutTemplateMappingDAL.CreateUserWorkoutTemplateMappingByTemplateID (SelectedUser.ProfileID, gymID, selectedViewModel.Id, userExerciseGroup.Id, selectedViewModel.TemplateName, selectedViewModel.TemplateDescription);

				//Uses session tracking
				Insights.Track ("Assign member", new Dictionary<string, string> () {
					{ "template", selectedViewModel.TemplateName },
					{ "template id", selectedViewModel.LocalUserExerciseGroupID.ToString () },
					{ "Member name", SelectedUser.FName + " " + SelectedUser.LName },
					{ "Profile id", SelectedUser.ProfileID.ToString () }
				});

				MessagingCenter.Send<TemplateListPage> (Application.Current.MainPage.Navigation.NavigationStack.Last () as TemplateListPage, "WorkoutAssigned");


				await Application.Current.MainPage.Navigation.PopAsync (true);



			} else {
				DependencyService.Get<ICustomDialog> ().Display ("A problem occurred during assignment of the workout. Please try again.", "OK");
			}

			IsBusy = false;

		

		}
		public async static Task<UserExerciseGroup> GetUserExerciseGroupByServerExerciseGroupID(int serverExerciseGroupID)
		{
			UserExerciseGroup group = new UserExerciseGroup();


			var db = DependencyService.Get<ISQLite>().GetAsyncConnection();
			await db.CreateTableAsync<UserExerciseGroup>();
			group = await db.Table<UserExerciseGroup>().Where(row => row.UserWorkoutGroupID == serverExerciseGroupID).FirstOrDefaultAsync();

			return group;
		}
		public async static Task<UserExerciseGroup> GetUserExerciseGroupByProfileIDNameDescription(int profileID, string groupName, string groupDescription)
		{
			UserExerciseGroup group = new UserExerciseGroup();


			var db = DependencyService.Get<ISQLite>().GetAsyncConnection();
			await db.CreateTableAsync<UserExerciseGroup>();
			group = await db.Table<UserExerciseGroup>().Where(row => row.ProfileID == profileID && row.TemplateName == groupName && row.TemplateDescription == groupDescription).FirstOrDefaultAsync();

			return group;
		}
		private async Task WriteToRepository ()
		{
			activityIndicator.IsRunning = true;
			activityIndicator.IsVisible = true;
			submitButton.IsEnabled = false;
			cancel2Button.IsEnabled = false;

			//Assign member template in the local database
			bool successfulAssignment = await UserDAL.AssignUserTemplate (selectedUser.ProfileID, _viewModel.Id);

			if (successfulAssignment) {

				int gymID = App.WorkoutCreatorContext.StaffMember.GymID;

				//Retrieve WorkoutTemplate serverTemplateID
				WorkoutTemplate workoutTemplate = await WorkoutTemplateDAL.GetWorkoutTemplateByID (gymID, _viewModel.Id);
				int serverTemplateID = workoutTemplate.WorkoutTemplateID;

				UserExerciseGroup userExerciseGroup = new UserExerciseGroup ();

				if (CrossConnectivity.Current.IsConnected) {
					//Attempt to assign workout template to the member on the server
					userExerciseGroup = await UserDAL.AddServerTemplateIDLocalAndServer (gymID, selectedUser.ProfileID, serverTemplateID, false);
				}

				if (userExerciseGroup.Id == 0) {
					//Still need to map userexercisegroups (and exercises) to the user if the API cannot be accessed or the application is not connected to a network
					userExerciseGroup.TemplateDescription = _viewModel.TemplateDescription;
					userExerciseGroup.TemplateName = _viewModel.TemplateName;
					userExerciseGroup.ExerisesDirty = true;
					userExerciseGroup.LastUpdatedTime = DateTime.UtcNow;
					userExerciseGroup.ProfileID = selectedUser.ProfileID;
					userExerciseGroup.UserWorkoutGroupID = 0;
					userExerciseGroup.GymID = selectedUser.GymID;

					await UserExerciseGroupDAL.AddModify (userExerciseGroup);
				}



				//Create the user exercise mapping in the local database
				//Assumes template exercise mappings were already made locally
				await UserWorkoutTemplateMappingDAL.CreateUserWorkoutTemplateMappingByTemplateID (selectedUser.ProfileID, gymID, _viewModel.Id, userExerciseGroup.Id, _viewModel.TemplateName, _viewModel.TemplateDescription);

				//Uses session tracking
				Insights.Track ("Assign member", new Dictionary<string, string> () {
					{ "template", _viewModel.TemplateName },
					{ "template id", _viewModel.LocalUserExerciseGroupID.ToString () },
					{ "Member name", selectedUser.FName + " " + selectedUser.LName },
					{ "Profile id", selectedUser.ProfileID.ToString () }
				});

				//assign profileid to viewmodel
				_viewModel.ProfileID = selectedUser.ProfileID;


				MessagingCenter.Send<ModalAssignMemberWorkoutPage> (Application.Current.MainPage.Navigation.ModalStack.Last () as ModalAssignMemberWorkoutPage, "WorkoutAssigned");

				Device.BeginInvokeOnMainThread (async() => {
					//this.UserExerciseGroupViewModelList.Add (userExerciseGroupViewModel);
					await Application.Current.MainPage.Navigation.PopModalAsync (false);
				});

			} else {
				DependencyService.Get<ICustomDialog> ().Display ("A problem occurred during assignment of the workout. Please try again.", "OK");
			}
		}