import logging
from decimal import Decimal

from measurement.measures import Weight
from django.test import TestCase, Client, RequestFactory
from django.urls import reverse
from django.conf import settings
from django.contrib.sessions.middleware import SessionMiddleware
from paypalcheckoutsdk.orders import OrdersCreateRequest, OrdersCaptureRequest
from paypalcheckoutsdk.core import PayPalHttpClient, SandboxEnvironment

from accounts.models import User, Address
from core.models import Product, Order
from core import CoffeeGrind
from storefront.views import OrderCreateView
from storefront.forms import AddressForm
from storefront.cart import Cart
from storefront.payments import CreateOrder

from . import RequestFaker

logger = logging.getLogger(__name__)

class AddressFormTest(TestCase):

    def test_invalid_address_returns_form_error(self):
        form = AddressForm(data={
            'full_name': 'John Doe',
            'email': 'john@example.com',
            # Wrong street address
            'street_address_1': '1579 ',
            'street_address_2': '',
            'city': 'Logan',
            'state': 'UT',
            # Wrong Zip code
            'postal_code': '23481'
        })
        self.assertFalse(form.is_valid())

    def test_usps_finds_zip_from_address(self):
        form = AddressForm(data={
            'full_name': 'John Doe',
            'email': 'john@example.com',
            'street_address_1': '1579 Talon Dr.',
            'street_address_2': '',
            'city': 'Logan',
            'state': 'UT',
            # Wrong Zip code
            'postal_code': '23481'
        })
        self.assertTrue(form.is_valid())
        if form.is_valid():
            cleaned_data = form.cleaned_data
            postal_code = cleaned_data.get('postal_code')
        self.assertEqual(postal_code, '84321')


    def test_invalid_address_returns_form_error(self):
        form = AddressForm(data={
            'full_name': 'John Doe',
            'email': 'john@example.com',
            # Wrong street address
            'street_address_1': '1579',
            'street_address_2': '',
            'city': 'Logan',
            'state': 'UT',
            # Wrong Zip code
            'postal_code': '84321'
        })
        self.assertFalse(form.is_valid())

    def test_process_full_name_with_two_given_names(self):
        form = AddressForm(data={
            'full_name': 'John Doe',
            'email': 'john@example.com',
            'street_address_1': '1579 Talon Dr',
            'street_address_2': '',
            'city': 'Logan',
            'state': 'UT',
            'postal_code': '84321'
        })
        if form.is_valid():
            cleaned_data = form.cleaned_data
            first_name, last_name = form.process_full_name(
                cleaned_data.get('full_name')
            )
        self.assertEqual(first_name, 'John')
        self.assertEqual(last_name, 'Doe')

    def test_process_full_name_with_more_than_two_given_names(self):
        form = AddressForm(data={
            'full_name': 'John Franklin Rosevelt Doe',
            'email': 'john@example.com',
            'street_address_1': '1579 Talon Dr',
            'street_address_2': '',
            'city': 'Logan',
            'state': 'UT',
            'postal_code': '84321'
        })
        if form.is_valid():
            cleaned_data = form.cleaned_data
            first_name, last_name = form.process_full_name(
                cleaned_data.get('full_name')
            )
        self.assertEqual(first_name, 'John Franklin Rosevelt')
        self.assertEqual(last_name, 'Doe')
