public void Can_remove_address_assigned_as_billing_address()
        {
            var customer = new Customer();
            var address = new Address { Id = 1 };

            customer.Addresses.Add(address);
            customer.BillingAddress  = address;

            customer.BillingAddress.ShouldBeTheSameAs(customer.Addresses.First());

            customer.RemoveAddress(address);
            customer.Addresses.Count.ShouldEqual(0);
            customer.BillingAddress.ShouldBeNull();
        }
        public bool FindAndApplyAddress(OrderReferenceDetails details, Customer customer, bool isShippable, bool forceToTakeAmazonAddress)
        {
            // PlaceOrder requires billing address but we don't get one from Amazon here. so use shipping address instead until we get it from amazon.
            bool countryAllowsShipping, countryAllowsBilling;

            var amazonAddress = new SmartStore.Core.Domain.Common.Address()
            {
                CreatedOnUtc = DateTime.UtcNow
            };

            details.ToAddress(amazonAddress, _countryService, _stateProvinceService, out countryAllowsShipping, out countryAllowsBilling);

            if (isShippable && !countryAllowsShipping)
                return false;

            if (amazonAddress.Email.IsEmpty())
                amazonAddress.Email = customer.Email;

            if (forceToTakeAmazonAddress)
            {
                // first time to get in touch with an amazon address
                var existingAddress = customer.Addresses.ToList().FindAddress(amazonAddress, true);

                if (existingAddress == null)
                {
                    customer.Addresses.Add(amazonAddress);
                    customer.BillingAddress = amazonAddress;
                }
                else
                {
                    customer.BillingAddress = existingAddress;
                }
            }
            else
            {
                if (customer.BillingAddress == null)
                {
                    customer.Addresses.Add(amazonAddress);
                    customer.BillingAddress = amazonAddress;
                }

                // we already have the address but it is uncomplete, so just complete it
                details.ToAddress(customer.BillingAddress, _countryService, _stateProvinceService, out countryAllowsShipping, out countryAllowsBilling);

                // but now we could have dublicates
                int newAddressId = customer.BillingAddress.Id;
                var addresses = customer.Addresses.Where(x => x.Id != newAddressId).ToList();

                var existingAddress = addresses.FindAddress(customer.BillingAddress, false);

                if (existingAddress != null)
                {
                    // remove the new and take the old one
                    customer.RemoveAddress(customer.BillingAddress);
                    customer.BillingAddress = existingAddress;

                    try
                    {
                        _addressService.DeleteAddress(newAddressId);
                    }
                    catch (Exception exc)
                    {
                        exc.Dump();
                    }
                }
            }

            customer.ShippingAddress = (isShippable ? customer.BillingAddress : null);

            return true;
        }