public SignupResult SaveNewSignup(SignupInfo signupInfo,bool requireValidEmail = true, bool requireSitterParentPhone = false)
        {
            var result = new SignupResult() ;
            ValidateSignup(signupInfo, result, requireValidEmail, requireSitterParentPhone);
            if (result.Error != null)
                return result;

            signupInfo.User.Email = signupInfo.User.Email.ToLower();

            _appUserDal.InsertUser(signupInfo.User);
            var userPass = new AppUserPass
            {
                Id = signupInfo.User.Id,
                PasswordHash = StringHasher.GetHashString(signupInfo.Pass),
                Token = Guid.NewGuid().ToString(),
                Email = signupInfo.User.Email.ToLower(),
                MobilePhone =  signupInfo.User.MobilePhone
            };

            _userPassDal.InsertUserPass(userPass);

            if (signupInfo.User.UserRole == UserRole.Parent)
            {
                var parent = new Parent {Id = signupInfo.User.Id};
                _parentDal.Insert(parent);
            }
            else if (signupInfo.User.UserRole == UserRole.Sitter)
            {
                var sitter = new Sitter
                {
                    Id = signupInfo.User.Id,
                    ParentEmail = signupInfo.SitterSignupInfo.ParentEmail,
                    ParentMobile = signupInfo.SitterSignupInfo.ParentMobile,
                    Age = signupInfo.SitterSignupInfo.Age
                };
                _sitterDal.Insert(sitter);
                result.NewId = sitter.Id;
            }
            else if (signupInfo.User.UserRole == UserRole.Admin)
            {                
            }
            else
            {
                throw new AppException(string.Format("userRole '{0}' not valid", signupInfo.User.UserRole));
            }

            string userName = signupInfo.User.Email.ToLower() != null ? signupInfo.User.Email.ToLower() : signupInfo.User.MobilePhone;
            result.newUserData =_authDal.AuthenticateUserByName(userName);
            result.IsSuccess = true;
            return result;
        }
        private void ValidateSignup(SignupInfo signupInfo, SignupResult result, bool requireValidEmail,bool requireSitterParentPhone)
        {
            if (signupInfo == null)
            {
                result.Error = "signupInfo is null";
                return;
            }
            
            if (signupInfo.User == null)
            {
                result.Error = "user is null";
                return;
            }

            if (string.IsNullOrWhiteSpace(signupInfo.User.FirstName ))
            {
                result.Error = "First Name is required";
                return;
            }

            if (requireValidEmail)
            {
                try
                {
                    MailAddress email = new MailAddress(signupInfo.User.Email); //FutureDev: use Regex so that 1) we don't have to reference System.Net assembly, and 2) don't have to catch an exception which is expensive.
                }
                catch (FormatException)
                {
                    result.Error = "Invalid email";
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(signupInfo.Pass))
            {
                result.Error = "Password is required";
                return;
            }

            if (!PhoneUtil.IsValidPhoneNumber(signupInfo.User.MobilePhone))
            {
                result.Error = "Mobile Phone is invalid";
                return;
            }

            signupInfo.User.MobilePhone = PhoneUtil.CleanAndEnsureCountryCode(signupInfo.User.MobilePhone);
            if (_appUserDal.GetByMobile(signupInfo.User.MobilePhone) != null)
            {
                result.Error = "Mobile Phone is already associated with an account.";
                return;
            }

            if (requireSitterParentPhone)
            {
                if (signupInfo.User.UserRole == UserRole.Sitter)
                {
                    if (signupInfo.SitterSignupInfo == null)
                    {
                        result.Error = "Signup is sitter, but SitterSignupInfo is null";
                        return;
                    }
                    if (signupInfo.SitterSignupInfo.Age < 18 && string.IsNullOrEmpty(signupInfo.SitterSignupInfo.ParentMobile))
                    {
                        result.Error = "Signup is sitter and is less than Age 18, parent mobile is required.";
                        return;
                    }
                }
            }
        }
        public void ProcessResponseToSignupInvite(TxtMsgInbound msgInbound, TxtMsgAwaitingResponse awaiting, AppUser user)
        {
            MessageAffirmativeType affirmativeType = MessageUtility.ParseAffirmativeType(msgInbound);
            string feedback = null;
            Parent parent = _parentRepository.GetById(awaiting.ParentId, true);
            if (parent == null)
                throw new Exception("Error while processing inbound message. Parent not found with ID" + awaiting.ParentId);
            // If user answers 'yes'.
            if (affirmativeType == MessageAffirmativeType.yes)
            {
                // If user already exists we simply add hib to parent's sitterhub.
                if (user != null)
                {
                    if (user.UserRole == UserRole.Sitter)
                    {
                        // ..sure if this user is a sitter.
                        AddSitterToParentMySitters(user.Id, parent, msgInbound.MobilePhone);
                        feedback = MessageTemplates.FormatUserAddedToSitters(parent);
                    }
                    else
                    {
                        // Otherwise we cannot add user because he is not a sitter.
                        feedback = MessageTemplates.FormatInvitedUserIsNotASitter();
                    }
                }
                    // If user doesn't exist we need to create new one.
                else
                {
                    // Create new (default) sitter profile.
                    var newSitter = new SignupInfo
                    {
                        User = new AppUser
                        {
                            FirstName = "New Sitter",
                            LastName = "",
                            Email = "",
                            MobilePhone = msgInbound.MobilePhone,
                            UserRole = UserRole.Sitter,
                        },
                        Pass = GeneratePassForNewUser() //FutureDev: text this password to sitter as their new temp password.
                    };
                    newSitter.SitterSignupInfo = new SitterSignupInfo {ParentEmail = "", ParentMobile = ""};

                    SignupResult result = new SignupRepository().SaveNewSignup(newSitter, false, false);
                    if (!result.IsSuccess)
                        throw new Exception("Failed to create new profile for sitter during parent invite." + result.Error);
                    // If there were no errors we can continue.
                    // We need to add newly created user into parent's sitterhub.
                    AddSitterToParentMySitters(result.NewId, parent, msgInbound.MobilePhone);
                    // Send message about new account created and user successfully added to parent's hub.
                    feedback = MessageTemplates.FormatUserAddedToSittersAndProfileCreated(parent, newSitter.Pass);
                }
            }
                // If user answered 'no'.
            else if (affirmativeType == MessageAffirmativeType.no)
            {
                // TODO: separate failure message.
                // We send message to sitter that we didn't added him to parent's hub.
                feedback = MessageTemplates.FormatDeclineInvitationToSitterhub(parent);
                _parentRepository.CancelInviteSitter(parent.Id, msgInbound.MobilePhone, true);
            }
                // If user's answer can't be parsed.
            else
            {
                feedback = MessageTemplates.FormatSitterInvalidResponseToJobOrSignup(false);
            }
            // Update state of message.
            _txtMsgInboundDal.UpdateState(msgInbound.Id, TxtMsgProcessState.Processed);
            // If sitter's answer was successfully parsed we can mark this message as processed.
            if (affirmativeType != MessageAffirmativeType.Unknown)
            {
                _txtMsgAwaitingResponseDal.DeleteAwaiting(awaiting.Id);
            }
            // TODO: where we send messages to parent? Seems like we can do it here.
            if (feedback != null)
            {
                _omm.SendFeedbackToInboundMessage(msgInbound, feedback, user.Id);
            }
        }
        public void ProcessSelfSignupConversation_Step1(TxtMsgInbound msgInbound, TxtMsgAwaitingResponse awaiting, AppUser user)
        {
            bool deleteAwaiting = true;
            if (MessageUtility.IsCancelRequested(msgInbound))
            {
                _omm.SendFeedbackToInboundMessage(msgInbound, "Ok, signup cancelled", user.Id);
            }
            else if (msgInbound.Message == "sitter" || msgInbound.Message == "parent" || msgInbound.Message == "babysitter")
            {
                bool isSitter = (msgInbound.Message == "sitter" || msgInbound.Message == "babysitter");
                // STEP - Create new AppUser
                var newUser = new SignupInfo
                {
                    User = new AppUser
                    {
                        FirstName = "New Self Signup",
                        LastName = "",
                        Email = "",
                        MobilePhone = msgInbound.MobilePhone,
                        UserRole = isSitter ? UserRole.Sitter : UserRole.Parent,
                    },
                    Pass = GeneratePassForNewUser()
                };
                if (isSitter)
                {
                    newUser.SitterSignupInfo = new SitterSignupInfo {ParentEmail = "", ParentMobile = ""};
                }

                SignupResult result = new SignupRepository().SaveNewSignup(newUser, false);
                if (result.IsSuccess)
                {
                    _omm.SendFeedbackToInboundMessage(msgInbound, string.Format("Ok, you have signed up for mysitterhub.com, you can login at mysitterhub.com with your mobile number and password: {0}.", newUser.Pass), user.Id);
                    //_omm.SendFeedbackToInboundMessage(msgInbound, string.Format("You can finish filling out your profile. What is your name? Or say 'cancel'."));
                    //_txtMsgAwaitingResponseDal.Insert(new TxtMsgAwaitingResponse(outboundTxtMsgId, 0, msgInbound.MobilePhone, InboundMessageType.SelfSignup_Step2_Name, 0)); //FutureDev
                }
                else
                {
                    _omm.SendFeedbackToInboundMessage(msgInbound, string.Format("Error while signing up:" + result.Error), user.Id);
                    new LogUtil().LogMessage("signup unsuccessful " + result.Error);
                    deleteAwaiting = false;
                }
            }
            else
            {
                _omm.SendFeedbackToInboundMessage(msgInbound, string.Format("Invalid response, are you a 'parent' or a 'sitter'? Or say 'cancel' to quit the signup process."), user.Id);
                deleteAwaiting = false;
            }

            if (deleteAwaiting)
                _txtMsgAwaitingResponseDal.DeleteAwaiting(awaiting.Id);
        }
 private void SaveSignup(SignupInfo signup)
 {
     SignupResult ret = _signupRepo.SaveNewSignup(signup);
     if (!ret.IsSuccess)
         throw new AppException("Invalid Signup:" + ret.Error);
 }
        public void InsertExampleData()
        {
            var infoParent1 = new SignupInfo
            {
                User = new AppUser
                {
                    FirstName = "Sarah",
                    LastName = "Fluckiger",
                    Email = "*****@*****.**",
                    MobilePhone = "+15127514094",
                    UserRole = UserRole.Parent,
                    TimezoneOffset = -5,
                    HasProfilePic = true
                },
                Pass = "******"
            };
            SaveSignup(infoParent1);

            var infoParent2 = new SignupInfo
            {
                User = new AppUser
                {
                    FirstName = "Parent2",
                    LastName = "Jones",
                    Email = "*****@*****.**",
                    MobilePhone = "+15125552102",
                    UserRole = UserRole.Parent,
                    TimezoneOffset = -5,
                    HasProfilePic = true
                },
                Pass = "******"
            };
            SaveSignup(infoParent2);


            var infoSitter1 = new SignupInfo
            {
                User = new AppUser
                {
                    FirstName = "Joseph",
                    LastName = "Fluckiger",
                    Email = "*****@*****.**",
                    MobilePhone = "+15129219530",
                    UserRole = UserRole.Sitter,
                    TimezoneOffset = -5
                },
                SitterSignupInfo = new SitterSignupInfo
                {
                    Age = 12,
                    ParentEmail = "*****@*****.**",
                    ParentMobile = "+1512555301"
                },
                Pass = "******"
            };
            SaveSignup(infoSitter1);

            var infoSMike = new SignupInfo
            {
                User = new AppUser
                {
                    UserRole = UserRole.Sitter,
                    FirstName = "Michael-Sitter",
                    LastName = "Fluckiger",
                    Email = "*****@*****.**",
                    MobilePhone = "+15125219854",
                    TimezoneOffset = -5
                },
                SitterSignupInfo = new SitterSignupInfo
                {
                    Age = 14,
                    ParentEmail = "*****@*****.**",
                    ParentMobile = "+15125554444"
                },
                Pass = "******"
            };
            // Michael As Parent
            infoSMike.User.UserRole = UserRole.Parent;
            infoSMike.User.FirstName = "Michael-Parent";

            SaveSignup(infoSMike);

            var infoSitterArtem = new SignupInfo
            {
                User = new AppUser
                {
                    UserRole = UserRole.Sitter,
                    FirstName = "Artem",
                    LastName = "Shelkov",
                    Email = "*****@*****.**",
                    MobilePhone = "+79137110364",
                    TimezoneOffset = +7
                },
                SitterSignupInfo = new SitterSignupInfo
                {
                    Age = 23
                },
                Pass = "******"
            };
            SaveSignup(infoSitterArtem);

            // STEP - Assign sitters to Parent.
            Parent parent1 = _parentRepo.GetById(infoParent1.User.Id);
            parent1.Sitters = new List<ParentMySitter>
            {
                new ParentMySitter {SitterId = infoSitter1.User.Id, Rate = (decimal) 6.5, SortOrder = 1},
                new ParentMySitter {SitterId = infoSitterArtem.User.Id, Rate = (decimal) 7.5, SortOrder = 2},
                //new ParentMySitter {SitterId = infoSitter3.User.Id, Rate = (decimal) 8.5, SortOrder = 3},
            };
            parent1.InviteToSignup = new List<InviteToSignup>
            {
                new InviteToSignup
                {
                    MobilePhone = "+15125554545",
                    InviteNickName = "Tom",
                    InviteStatus = InvitationState.InvitePending,
                },
                new InviteToSignup
                {
                    MobilePhone = "+15125554546",
                    InviteNickName = "Jerry",
                    InviteStatus = InvitationState.InvitePending,
                },
            };

            var admin1 = new SignupInfo
            {
                User = new AppUser
                {
                    FirstName = "Admin1",
                    LastName = "Business",
                    Email = "*****@*****.**",
                    MobilePhone = "+15125553345",
                    UserRole = UserRole.Admin,
                    TimezoneOffset = -5
                },
                Pass = "******"
            };
            SaveSignup(admin1);

            _parentRepo.UpdateParent(parent1);

            generateJobs(infoParent1.User.Id,
                new List<AppUser> {infoSitter1.User, infoSMike.User});
        }