from django.contrib.auth.models import User
from django.test import TestCase
from django.test.client import Client

from satchmo.caching import cache_delete
from satchmo.contact.models import Contact
from satchmo.product.models import Product
from satchmo.shop.satchmo_settings import get_satchmo_setting
from satchmo.wishlist.factories import ProductWishFactory
from satchmo.wishlist.models import ProductWish

domain = "http://testserver"
prefix = get_satchmo_setting("SHOP_BASE")
if prefix == "/":
    prefix = ""


def get_step1_post_data(US):
    return {
        "email": "sometester@example.com",
        "first_name": "Teddy",
        "last_name": "Tester",
        "phone": "456-123-5555",
        "street1": "8299 Some Street",
        "city": "Springfield",
        "state": "MO",
        "postal_code": "81122",
        "country": US.pk,
        "ship_street1": "1011 Some Other Street",
        "ship_city": "Springfield",
        "ship_state": "MO",
        "ship_postal_code": "81123",
        "paymentmethod": "DUMMY",
    }


class WishManagerTest(TestCase):
    def test_active(self):
        active_product = ProductWishFactory()
        inactive_product = ProductWishFactory()
        inactive_product.product.active = False
        inactive_product.product.save()

        active_product_wishes = ProductWish.objects.active()
        self.assertTrue(active_product in active_product_wishes)
        self.assertFalse(inactive_product in active_product_wishes)


class WishTest(TestCase):
    fixtures = [
        "l10n_data.xml",
        "sample-store-data.yaml",
        "products.yaml",
        "test-config.yaml",
    ]

    def setUp(self):
        # Every test needs a client
        self.client = Client()

    def tearDown(self):
        cache_delete()

    def test_wish_adding_not_logged_in(self):
        """
        Validate we can't add unless we are logged in.
        """
        response = self.client.get(prefix + "/product/dj-rocks/")
        self.assertContains(response, "Django Rocks shirt", count=2, status_code=200)
        response = self.client.post(
            prefix + "/add/",
            {
                "productname": "dj-rocks",
                "1": "M",
                "2": "BL",
                "addwish": "Add to wishlist",
            },
        )
        self.assertContains(response, "Sorry, you must be", count=1, status_code=200)


class WishTestLoggedIn(TestCase):
    fixtures = [
        "l10n_data.xml",
        "sample-store-data.yaml",
        "products.yaml",
        "test-config.yaml",
    ]

    def setUp(self):
        self.client = Client()
        user = User.objects.create_user("wisher", "wisher@example.com", "passwd")
        user.is_staff = False
        user.is_superuser = False
        user.save()
        self.contact = Contact.objects.create(
            user=user, first_name="Wish", last_name="Tester"
        )
        self.client.login(username="wisher", password="passwd")

    def tearDown(self):
        cache_delete()

    def test_wish_adding(self):
        """
        Validate we can add some items to the wishlist
        """
        response = self.client.get(prefix + "/product/dj-rocks/")
        self.assertContains(response, "Django Rocks shirt", count=2, status_code=200)
        response = self.client.post(
            prefix + "/add/",
            {
                "productname": "dj-rocks",
                "1": "M",
                "2": "BL",
                "addwish": "Add to wishlist",
            },
        )
        self.assertRedirects(
            response,
            domain + prefix + "/wishlist/",
            status_code=302,
            target_status_code=200,
        )
        response = self.client.get(prefix + "/wishlist/")

        self.assertContains(
            response, "Django Rocks shirt (Medium/Blue)", count=1, status_code=200
        )

    def test_wish_removing(self):
        """
        Validate that we can remove wishlist items
        """
        product = Product.objects.get(slug="dj-rocks-m-bl")
        wish = ProductWish(product=product, contact=self.contact)
        wish.save()

        product = Product.objects.get(slug="robot-attack-soft")
        wish = ProductWish(product=product, contact=self.contact)
        wish.save()

        response = self.client.get(prefix + "/wishlist/")
        self.assertContains(response, "Robots Attack", count=1, status_code=200)
        self.assertContains(
            response, "Django Rocks shirt (Medium/Blue)", count=1, status_code=200
        )

        response = self.client.post(prefix + "/wishlist/remove/", {"id": wish.id})
        self.assertContains(response, "Robots Attack", count=0, status_code=200)
        self.assertContains(
            response, "Django Rocks shirt (Medium/Blue)", count=1, status_code=200
        )

    def test_wish_to_cart(self):
        """
        Validate that we can move an item to the cart
        """
        product = Product.objects.get(slug="dj-rocks-m-bl")
        wish = ProductWish(product=product, contact=self.contact)
        wish.save()

        product = Product.objects.get(slug="robot-attack-soft")
        wish = ProductWish(product=product, contact=self.contact)
        wish.save()

        response = self.client.get(prefix + "/wishlist/")
        self.assertContains(response, "Robots Attack", count=1, status_code=200)
        self.assertContains(
            response, "Django Rocks shirt (Medium/Blue)", count=1, status_code=200
        )

        response = self.client.post(prefix + "/wishlist/add_cart/", {"id": wish.id})
        self.assertRedirects(
            response,
            domain + prefix + "/cart/",
            status_code=302,
            target_status_code=200,
        )
        response = self.client.get(prefix + "/cart/")
        self.assertContains(response, "Robots Attack", count=2, status_code=200)
