private static void FillBaseViewModelData(UpdateProfileAuspostViewModel viewModel)
        {
            var privateClientId = ConfigurationManager.AppSettings["PrivateClientId"] == null
                ? 5
                : int.Parse(ConfigurationManager.AppSettings["PrivateClientId"]);

            var streetTypes = new UserService().GetAllStreetTypes(privateClientId);
            var streetNames = new UserService().GetAllStreetNames(privateClientId);
            var suburbs = new UserService().GetAllSuburbs(privateClientId);
            var states = new UserService().GetAllStates(privateClientId);

            viewModel.StreetTypes = new SelectList(streetTypes);
            viewModel.StreetNames = new SelectList(streetNames);
            viewModel.Subrps = new SelectList(suburbs);
            viewModel.States = new SelectList(states);

            //Something not valid, show errors
            viewModel.SupportEmail = ConfigurationManager.AppSettings["SupportEmail"];
        }
        public ActionResult UpdateAuspost()
        {
            LayoutViewModel.ActiveLink = Links.AccountUpdateProfile;

            var profileModel = new ProfileService().GetMyProfile(LayoutViewModel.ProviderUserKey,
                LayoutViewModel.CurrentUserEmail, 1);

            UpdateProfileAuspostViewModel viewModelCompany = new UpdateProfileAuspostViewModel
            {
                FirstName = profileModel.User.FirstName,
                LastName = profileModel.User.LastName,
                PhoneNumber = profileModel.User.PhoneNumber,
                Sex = profileModel.User.Sex,
                Email = LayoutViewModel.CurrentUserEmail,
                CompanyEmail = LayoutViewModel.CurrentUserEmail.Split('@')[0],
                CompanyEmailDomain = string.Format("@{0}", LayoutViewModel.CurrentUserEmail.Split('@')[1]),
                PhotoId = profileModel.User.PhotoID,
                Address = ViewModelHelper.GetUserAddress(profileModel.Address),
                SupportEmail = ConfigurationManager.AppSettings["SupportEmail"],
                PushNotifications = profileModel.User.PushNotifications
            };

            //Private company - auspost
            viewModelCompany.EmploymentType = profileModel.User.EmploymentType;

            if (profileModel.User.DateOfBirth.HasValue)
            {
                DateTime date = profileModel.User.DateOfBirth.Value;
                viewModelCompany.DateOfBirthDay = date.Day;
                viewModelCompany.DateOfBirthMonth = date.Month;
                viewModelCompany.DateOfBirthYear = date.Year;
            }

            var streetTypes = new UserService().GetAllStreetTypes((int)LayoutViewModel.Instance_Id);
            var streetNames = new UserService().GetAllStreetNames((int)LayoutViewModel.Instance_Id);
            var suburbs = new UserService().GetAllSuburbs((int)LayoutViewModel.Instance_Id);
            var states = new UserService().GetAllStates((int)LayoutViewModel.Instance_Id);

            viewModelCompany.StreetTypes = new SelectList(streetTypes);
            viewModelCompany.StreetNames = new SelectList(streetNames);
            viewModelCompany.Subrps = new SelectList(suburbs);
            viewModelCompany.States = new SelectList(states);

            viewModelCompany.StreetType = profileModel.Address.StreetType;
            viewModelCompany.StreetName = profileModel.Address.StreetName;
            viewModelCompany.Subrp = profileModel.Address.Suburb;
            viewModelCompany.State = profileModel.Address.State;

            return View("UpdateProfileAuspost", viewModelCompany);
        }
        public ActionResult UpdateProfileAuspost(UpdateProfileAuspostViewModel viewModel)
        {
            LayoutViewModel.ActiveLink = Links.AccountUpdateProfile;

            var membershipUser = Membership.GetUser();

            if (ModelState.IsValid)
            {
                UpdateProfileModel updateModel = new UpdateProfileModel();

                updateModel.FirstName = viewModel.FirstName;
                updateModel.LastName = viewModel.LastName;
                updateModel.PhoneNumber = viewModel.PhoneNumber;
                updateModel.Sex = viewModel.Sex;
                updateModel.PushNotifications = viewModel.PushNotifications;
                updateModel.EmploymentType = viewModel.EmploymentType;

                if (viewModel.DateOfBirthDay != null && viewModel.DateOfBirthMonth != null && viewModel.DateOfBirthYear != null)
                {
                    updateModel.BirthDate =
                        new DateTime(year: viewModel.DateOfBirthYear.Value,
                            month: viewModel.DateOfBirthMonth.Value,
                            day: viewModel.DateOfBirthDay.Value);
                }

                // find new user's address
                //int? addressId = new UserService().FindMatchingAuspostAddressId(
                //    streetName: viewModel.StreetName,
                //    streetType: viewModel.StreetType,
                //    suburb: viewModel.Subrp,
                //    state: viewModel.State
                //    );

                //if (addressId != null)
                //{
                //    updateModel.AddressId = addressId;
                //}
                //else
                //{
                //    FillBaseViewModelData(viewModel);

                //    ModelState.AddModelError("", "Incomplete workplace details. Please update your details and save.");
                //    return View(viewModel);
                //}

                var success = true;

                // change password
                if (!string.IsNullOrEmpty(viewModel.OldPassword) || !string.IsNullOrEmpty(viewModel.NewPassword)
                    || !string.IsNullOrEmpty(viewModel.ConfirmNewPassword))
                {
                    if (string.IsNullOrEmpty(viewModel.OldPassword))
                    {
                        ModelState.AddModelError("OldPassword", "The Old password field is required.");
                        success = false;
                    }

                    if (string.IsNullOrEmpty(viewModel.NewPassword))
                    {
                        ModelState.AddModelError("NewPassword", "The New password field is required.");
                        success = false;
                    }

                    if (string.IsNullOrEmpty(viewModel.ConfirmNewPassword))
                    {
                        ModelState.AddModelError("ConfirmNewPassword", "The Confirm new password field is required.");
                        success = false;
                    }

                    if (success && viewModel.NewPassword != viewModel.ConfirmNewPassword)
                    {
                        ModelState.AddModelError("ConfirmNewPassword", "New password and confirmation password do not match.");
                        success = false;
                    }

                    if (success && !membershipUser.ChangePassword(viewModel.OldPassword, viewModel.NewPassword))
                    {
                        ModelState.AddModelError("OldPassword", "Old password is incorrect.");
                        success = false;
                    }
                }

                if (viewModel.Photo != null && viewModel.Photo.ContentLength > 0)
                {
                    MemoryStream target = new MemoryStream();
                    viewModel.Photo.InputStream.CopyTo(target);
                    byte[] data = target.ToArray();

                    UploadModel model = new UploadModel
                    {
                        ContentType = viewModel.Photo.ContentType,
                        Contents = data,
                        FileName = viewModel.Photo.FileName
                    };

                    UploadModel upload = new UploadService().UploadFile(LayoutViewModel.ProviderUserKey, model, true);

                }

                if (success)
                {

                    var updated =
                        new ProfileService().UpdateProfile(LayoutViewModel.ProviderUserKey, updateModel);

                    // change username COMPLICATED
                    if (membershipUser.UserName != viewModel.Email)
                    {

                        var config = WebConfigurationManager.OpenWebConfiguration("~");
                        var section = config.SectionGroups["system.web"].Sections["membership"] as MembershipSection;
                        var defaultProvider = section.DefaultProvider;
                        var connectionStringName = section.Providers[defaultProvider].ElementInformation.Properties["connectionStringName"].Value.ToString();

                        string connectionString = config.ConnectionStrings.ConnectionStrings[connectionStringName].ConnectionString;

                        string companyEmail = string.Join(viewModel.CompanyEmail, viewModel.CompanyEmailDomain);

                        var changed = new ProfileService().ChangeUsername(membershipUser.UserName, companyEmail, connectionString);

                        if (changed)
                        {
                            // change email as well
                            membershipUser.Email = companyEmail;

                            // need to re-verify
                            membershipUser.IsApproved = false;
                            SendVerifyEmail(membershipUser.Email, updateModel.FirstName, LayoutViewModel.CurrentAccountId, false);

                            //instead I'm showing verifucation code in the view
                            Membership.UpdateUser(membershipUser);

                            // need to sign out to force verification
                            FormsAuthentication.SignOut();

                            TempData["VerifyCode"] = ZBase32.Encode(LayoutViewModel.CurrentAccountId.ToByteArray());

                            // redirect to screen which tells user to check email
                            return RedirectToAction("EmailChangeSuccess");
                        }
                        else
                        {
                            ModelState.AddModelError("", "A user for that email address already exists. Please enter a different email address.");
                        }
                    }

                    return RedirectToAction("MyProfile");

                }

                //Something not valid, show errors
                viewModel.SupportEmail = ConfigurationManager.AppSettings["SupportEmail"];

                return View(viewModel);
            }

            FillBaseViewModelData(viewModel);

            return View(viewModel);
        }