/// <summary>
        /// What happens before we go next
        /// </summary>
        /// <returns>The validation before going to the next fragment</returns>
        public override bool BeforeGoNext()
        {
            bool savedProspect = AsyncHelper.RunSync(
                async() =>
            {
                ProspectsController prospectsController = new ProspectsController();
                this.PersonRegistrationInfo.Groups      =
                    JsonConvert.SerializeObject(this.PersonRegistrationInfo.GroupInfo);
                await prospectsController.SaveAsync(this.PersonRegistrationInfo);
                return(this.PersonRegistrationInfo.Id != default(Guid));
            });

            return(savedProspect);
        }
        private async Task <bool> ExitIfConverted()
        {
            Prospect prosp = await ProspectsController.GetByIdAsync(_originalProspect.SearchResult.Id);

            if (prosp == null || prosp.Converted)
            {
                RunOnUiThread
                (
                    () =>
                {
                    Toast.MakeText(this, Resource.String.already_converted, ToastLength.Long).Show();
                    Finish();
                }
                );
                return(true);
            }

            return(false);
        }
        public async void onActionSelected(int selection)
        {
            var trans = GetFragmentManager().BeginTransaction();

            switch (selection)
            {
            case 0:
                this.Finish();
                break;

            case 1:
                RefreshUi();
                InitializeUi();
                break;

            case 2:
                Prospect props = _originalProspect.SearchResult.CastTo <Prospect>();
                SaveResponse <Prospect> saveResult = await ProspectsController.SaveAsync(props);

                if (saveResult.SavedModel == null || saveResult.SavedModel.Id == default(Guid))
                {
                    throw new Exception("Could not save prospect");
                }
                break;

            case 3:
                // App trackking
                GoogleAnalyticService.Instance.TrackEvent(GetString(Resource.String.prospect_details), GetString(Resource.String.prospect_details), "Convert Prospect");

                Dictionary <string, object> bundledItems = new Dictionary <string, object>
                {
                    { FragmentBasicInfo.KeyProspectIdBundled, _originalProspect.SearchResult }
                };

                // if we accessed prospect details from a reminder, then return to home after the conversion

                IntentStartPointTracker.IntentStartPoint startPoint = IntentStartPointTracker.IntentStartPoint.ProspectConversion;

                if (this._origin == Enums.ProspectDetailsOrigin.ProspectReminderClick)
                {
                    startPoint = IntentStartPointTracker.IntentStartPoint.WelcomeScreen;
                }

                WizardLauncher.Launch
                (
                    this,
                    WizardTypes.CustomerRegistration,
                    startPoint,
                    bundledItems
                );
                Finish();
                break;

            case 10:
                CreatingReminder = true;
                trans.Hide(_prospectDetailsMainFragment);
                trans.Show(_customDatePickerFragment);
                trans.Commit();
                SetTitle(Resource.String.create_reminder);

                break;

            case 11:
                CreatingReminder = true;
                SetTitle(Resource.String.edit_reminder);
                trans.Hide(_prospectDetailsMainFragment);
                trans.Show(_customDatePickerFragment);
                trans.Commit();
                break;
            }
        }
        /// <summary>
        /// Called to save a customer to the device
        /// </summary>
        /// <param name="e">The event arguments</param>
        /// <param name="smsSendAttempts">Number of attempts</param>
        /// <param name="smsAttemptLimit">The Limit</param>
        /// <returns>An empty task</returns>
        private async Task SaveCustomerToDevice(CustomerService.RegistrationStatusChangedEventArgs e, int smsSendAttempts, int smsAttemptLimit)
        {
            bool succeeded = e.SmsRegistrationSucceeded || e.Response.RegistrationSuccessful;

            if (!succeeded)
            {
                this.Logger.Debug(string.Format("Sms send attempts = {0} and sms send limts = {1}", smsSendAttempts, smsAttemptLimit));

                succeeded = e.Channel == DataChannel.Fallback && smsAttemptLimit == smsSendAttempts;
                if (!succeeded)
                {
                    return;
                }
            }

            this.Logger.Debug("Attempting to save customer to device");
            if (e.Response == null || e.Response.Customer == null)
            {
                return;
            }

            ProspectsController prospectsController = new ProspectsController();

            Mkopa.Core.BL.Models.People.Prospect prospect = await prospectsController
                                                            .GetByPhoneNumberAsync(e.Response.Customer.Phone);

            await Resolver.Instance.Get <ISQLiteDB>().Connection.RunInTransactionAsync(
                async(SQLiteConnection connTran) =>
            {
                try
                {
                    this.Logger.Debug("Trying to save customer");
                    CustomersController customersController = new CustomersController();
                    e.Response.Customer.Channel             = e.Channel;
                    string customerPhone = e.Response.Customer.Phone;
                    Guid custGuid;

                    Mkopa.Core.BL.Models.People.Customer customer =
                        await customersController.GetSingleByCriteria(CriteriaBuilder.New()
                                                                      .Add("Phone", customerPhone));

                    // customer to have more than one product
                    if (customer == null || customer.Id == default(Guid))
                    {
                        SaveResponse <Mkopa.Core.BL.Models.People.Customer> saveResponse =
                            await customersController.SaveAsync(connTran, e.Response.Customer, true);

                        if (saveResponse.SavedModel == null || saveResponse.SavedModel.Id == default(Guid))
                        {
                            new ReusableScreens(this.Activity)
                            .ShowInfo(
                                Resource.String.customer_save_err_actionbar,
                                Resource.String.customer_save_err_title,
                                Resource.String.customer_save_err_message,
                                Resource.String.customer_save_err_button);
                            return;
                        }
                        custGuid = saveResponse.SavedModel.Id;
                    }
                    else
                    {
                        custGuid = customer.Id;
                    }

                    await this.SaveCustomerProductToDevice(custGuid, connTran);

                    if (prospect != null)
                    {
                        this.Logger.Debug("There was a prospect with the same number so we convert to customer");
                        prospect.Converted = true;
                        await prospectsController.SaveAsync(connTran, prospect);
                    }

                    RecordStatus syncStatus = e.Channel == DataChannel.Fallback
                                ? (e.SmsRegistrationSucceeded
                                    ? RecordStatus.FallbackSent
                                    : RecordStatus.Pending)
                                : RecordStatus.Synced;

                    SyncingController syncController = new SyncingController();

                    SyncRecord syncRec = await syncController.GetSyncRecordAsync(e.Response.Customer.RequestId);
                    this.Logger.Debug("Fetching sync record for customer");

                    if (syncRec == null)
                    {
                        this.Logger.Debug("The sync record is null so we generate one");
                        syncRec = new SyncRecord
                        {
                            ModelId   = e.Response.Customer.Id,
                            ModelType = e.Response.Customer.TableName,
                            Status    = syncStatus,
                            RequestId = e.Response.Customer.RequestId
                        };
                        this.Logger.Debug("Saving generated sync record");
                        SaveResponse <SyncRecord> syncSaveResponse = await syncController.SaveAsync(
                            connTran,
                            syncRec);

                        this.Logger.Debug("Sync record was saved correctly");
                    }
                    else
                    {
                        syncRec.Status = syncStatus;
                    }

                    await syncController.SaveAsync(connTran, syncRec);
                }
                catch (Exception exception)
                {
                    this.Logger.Error(exception);
                }
            });
        }