import logging
from decimal import Decimal

from django.test import TestCase, Client, RequestFactory
from django.urls import reverse
from django.conf import settings
from measurement.measures import Weight
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 .views import OrderCreateView
from .cart import Cart

logger = logging.getLogger(__name__)

WHOLE = 'whole-beans'
ESPRESSO = 'espresso'
CONE_DRIP = 'cone-drip'
BASKET_DRIP = 'basket-drip'
FRENCH_PRESS = 'french-press'
STOVETOP_ESPRESSO = 'stovetop-espresso'
AEROPRESS = 'aeropress'
PERCOLATOR = 'percolator'
CAFE_STYLE = 'cafe-style'

class CartTest(TestCase):
    def setUp(self):
        self.client = Client()
        self.factory = RequestFactory()

        self.customer = User.objects.create_user(
            username='petertempler', email='peter@testing.com', password='peterspassword321'
        )
        self.product = Product.objects.create(
            name='Dante\'s Tornado',
            description='Coffee',
            sku='23987',
            price=13.4,
            weight=Weight(oz=16),
            visible_in_listings=True
        )
        self.order = Order.objects.create(
            customer=self.customer,
            total_net_amount=13.4
        )

        self.client.force_login(self.customer)

    def test_post_checkout_form(self):
        url = reverse('storefront:order-create')
        response = self.client.get(url)
        self.assertTemplateUsed(response, 'storefront/order_form.html')

        request = response.wsgi_request
        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=WHOLE,
            update_quantity=False
        )

        params = {
            'email': 'nathanchapman@hey.com',
            'first_name': 'Nathan',
            'last_name': 'Chapman',
            'total_net_amount': 26.80
        }

        response = self.client.post(url, params)
        self.assertContains(response, 'Checkout', status_code=200)

    def test_cart_item_variations(self):
        cart_detail_url = reverse('storefront:cart-detail')
        response = self.client.get(cart_detail_url)
        request = response.wsgi_request
        cart = Cart(request)

        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=WHOLE,
            update_quantity=False
        )
        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=ESPRESSO,
            update_quantity=False
        )
        for item in cart.cart.values():
            self.assertTrue('variations' in item, item)

    def test_add_item_to_cart(self):
        cart_detail_url = reverse('storefront:cart-detail')
        response = self.client.get(cart_detail_url)
        request = response.wsgi_request
        cart = Cart(request)

        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=WHOLE,
            update_quantity=False
        )

        self.assertEqual(cart.cart[f'{self.product.id}']['variations'][WHOLE]['quantity'], 1)
        self.assertEqual(len(cart), 1)
        self.assertEqual(sum(cart.get_item_prices()), Decimal('13.4'))
        self.assertEqual(cart.get_total_price(), Decimal('13.4'))
        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=WHOLE,
            update_quantity=False
        )
        self.assertEqual(cart.cart[f'{self.product.id}']['variations'][WHOLE]['quantity'], 2)
        self.assertEqual(len(cart), 2)

        cart.add(
            request,
            product=self.product,
            quantity=3,
            grind=ESPRESSO,
            update_quantity=False
        )
        self.assertEqual(cart.cart[f'{self.product.id}']['variations'][ESPRESSO]['quantity'], 3)
        self.assertEqual(len(cart), 5)
        self.assertEqual(cart.get_total_price(), Decimal('67'))

    def test_update_cart_item_quantity(self):
        cart_detail_url = reverse('storefront:cart-detail')
        response = self.client.get(cart_detail_url)
        request = response.wsgi_request
        cart = Cart(request)

        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=3,
            grind=WHOLE,
            update_quantity=False
        )
        self.assertEqual(cart.cart[f'{self.product.id}']['variations'][WHOLE]['quantity'], 3)

        cart.add(
            request,
            product=self.product,
            quantity=1,
            grind=WHOLE,
            update_quantity=True
        )
        self.assertEqual(cart.cart[f'{self.product.id}']['variations'][WHOLE]['quantity'], 1)

    def test_cart_remove_item(self):
        cart_detail_url = reverse('storefront:cart-detail')
        response = self.client.get(cart_detail_url)
        request = response.wsgi_request
        cart = Cart(request)

        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=3,
            grind=WHOLE,
            update_quantity=False
        )
        self.assertEqual(len(cart), 3)
        cart.remove(self.product)
        self.assertEqual(len(cart), 0)

    def test_cart_get_total_weight(self):
        cart_detail_url = reverse('storefront:cart-detail')
        response = self.client.get(cart_detail_url)
        request = response.wsgi_request
        cart = Cart(request)

        cart = Cart(request)
        cart.add(
            request,
            product=self.product,
            quantity=3,
            grind=WHOLE,
            update_quantity=False
        )
        self.assertEqual(cart.get_total_weight(), Decimal(48))

# 96oz
