//
        // GET: /Checkout/AddressAndPayment
        public ActionResult AddressAndPayment()
        {
            ViewBag.CreditCardTypes = CreditCardTypes;
            var userManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var user = userManager.FindById(User.Identity.GetUserId());
            var previousOrder = storeDB.Orders.FirstOrDefault(x => x.Username == User.Identity.Name);
            CheckoutViewModel checkoutViewModel = new CheckoutViewModel { User = user, Order = previousOrder };

            if (previousOrder != null || user != null)
                return View(checkoutViewModel);
            else
                return View();
        }
        public async Task<ActionResult> AddressAndPayment(FormCollection values)
        {
            ViewBag.CreditCardTypes = CreditCardTypes;
            string result =  values[9];
            string deliveryType = values[10];

            var checkoutViewModel = new CheckoutViewModel();
            TryUpdateModel(checkoutViewModel);
            checkoutViewModel.Order.CreditCard = result;

            checkoutViewModel.Order.DeliveryOption = Convert.ToInt32(deliveryType) == 0 ? Common.DeliveryOption.Pickup : Common.DeliveryOption.Delivery;

            try
            {
                checkoutViewModel.Order.Username = User.Identity.Name;
                checkoutViewModel.Order.Email = User.Identity.Name;
                checkoutViewModel.Order.OrderDate = DateTime.Now;

                /*
                     * Alot of repition but just added this to make saving to db work coz without it its throwing exception
                     * errors for required fields
                     */
                checkoutViewModel.Order.Address = checkoutViewModel.Order.Address;
                checkoutViewModel.Order.City = checkoutViewModel.Order.City;
                checkoutViewModel.Order.Country = checkoutViewModel.Order.Country;
                checkoutViewModel.Order.State = checkoutViewModel.Order.State;
                checkoutViewModel.Order.Phone = checkoutViewModel.Order.Phone;
                checkoutViewModel.Order.PostalCode = checkoutViewModel.Order.PostalCode;
                checkoutViewModel.Order.FirstName = checkoutViewModel.Order.FirstName;
                checkoutViewModel.Order.LastName = checkoutViewModel.Order.LastName;
                checkoutViewModel.Order.Total = (decimal) HttpContext.Session["CartTotal"];

                //var currentUserId = User.Identity.GetUserId();
                //if (checkoutViewModel.Order.SaveInfo && !checkoutViewModel.Order.Username.Equals("[email protected]"))
                //{

                //    var manager =
                //        new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));
                //    var store = new UserStore<ApplicationUser>(new ApplicationDbContext());
                //    var ctx = store.Context;
                //    var currentUser = manager.FindById(User.Identity.GetUserId());

                //    currentUser.Address = checkoutViewModel.Order.Address;
                //    currentUser.City = checkoutViewModel.Order.City;
                //    currentUser.Country = checkoutViewModel.Order.Country;
                //    currentUser.State = checkoutViewModel.Order.State;
                //    currentUser.Phone = checkoutViewModel.Order.Phone;
                //    currentUser.PostalCode = checkoutViewModel.Order.PostalCode;
                //    currentUser.FirstName = checkoutViewModel.Order.FirstName;
                //    currentUser.LastName = checkoutViewModel.Order.LastName;

                //    //Save this back
                //    //http://stackoverflow.com/questions/20444022/updating-user-data-asp-net-identity
                //    //var result = await UserManager.UpdateAsync(currentUser);
                //    await ctx.SaveChangesAsync();

                //    await storeDB.SaveChangesAsync();
                //}


                //Save Order
                storeDB.Orders.Add(checkoutViewModel.Order);
                await storeDB.SaveChangesAsync();
                //Process the order
                var cart = ShoppingCart.GetCart(this.HttpContext);
                checkoutViewModel.Order = cart.CreateOrder(checkoutViewModel.Order);



                CheckoutController.SendOrderMessage(checkoutViewModel.Order.FirstName,
                    "New Order: " + checkoutViewModel.Order.OrderId,
                    checkoutViewModel.Order.ToString(checkoutViewModel.Order), appConfig.OrderEmail);

                return RedirectToAction("Complete",
                    new {id = checkoutViewModel.Order.OrderId});

            }
            catch (Exception ex)
            {
                if (ex is System.Data.Entity.Validation.DbEntityValidationException)
                {
                    // Retrieve the error messages as a list of strings.
                    var errorMessages = (ex as System.Data.Entity.Validation.DbEntityValidationException)
                        .EntityValidationErrors
                        .SelectMany(x => x.ValidationErrors)
                        .Select(x => x.ErrorMessage);

                    // Join the list to a single string.
                    var fullErrorMessage = string.Join("; ", errorMessages);

                    // Combine the original exception message with the new one.
                    var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage);
                }
                //Invalid - redisplay with errors
                return View(checkoutViewModel);
            }
        }