import unittest

from django.contrib.auth import get_user_model
from django.core.management import CommandError, call_command
from django.urls import reverse

from oscar.core.loading import get_model

from ocyan.core.fender import config
from ocyan.core.plugins import get_fixtures
from ocyan.plugin.testing.screenshot_test import ScreenshotTestCase

from .test_urls import OCYAN_FREE_SHIPPING_CONFIG

Product = get_model("catalogue", "Product")
Category = get_model("catalogue", "Category")
ProductReview = get_model("reviews", "ProductReview")
Order = get_model("order", "Order")
User = get_user_model()


@unittest.mock.patch.object(config, "settings", OCYAN_FREE_SHIPPING_CONFIG)
class ScreenshotTest(ScreenshotTestCase):
    # Login required credentials
    USER_EMAIL = "superuser@mandelblog.com"

    fixtures = list(get_fixtures()) + [
        "oscar/screenshot_order",
        "oscar/screenshot_basket",
    ]

    @classmethod
    def setUpTestData(cls):
        try:
            call_command("update_oscar_index")
        except CommandError:
            pass

    # Root catalogue (all products)
    def test_catalogue_root(self):
        self.assertScreenshotOk(
            reverse("catalogue:index"), category="oscar_root_category_browse"
        )

    def test_category_browse(self):
        self.assertScreenshotOk(
            Category.objects.first().get_absolute_url(),
            category="oscar_catagory_browse",
        )

    def test_product_detail(self):
        self.assertScreenshotOk(
            Product.objects.first().get_absolute_url(), category="oscar_product_detail"
        )

    def test_empty_basket_summary(self):
        self.assertScreenshotOk(
            reverse("basket:summary"), category="oscar_empty_basket_summary"
        )

    def test_customer_login_page(self):
        self.assertScreenshotOk(
            reverse("customer:login"), category="oscar_customer_login"
        )

    def test_customer_summary(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:summary"),
            category="oscar_customer_summary",
        )

    def test_customer_order_list(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:order-list"),
            category="oscar_customer_order_list",
        )

    def test_customer_order_detail(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse(
                "customer:order", kwargs={"order_number": Order.objects.first().number}
            ),
            category="oscar_customer_order_detail",
        )

    def test_customer_address_list(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:address-list"),
            category="oscar_customer_address_list",
        )

    def test_customer_address_detail(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:address-detail", kwargs={"pk": "1"}),
            category="oscar_customer_address_detail",
        )

    def test_customer_email_list(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:email-list"),
            category="oscar_customer_email_list",
        )

    def test_customer_notification_inbox(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:notifications-inbox"),
            category="oscar_customer_notifications_inbox",
        )

    def test_customer_notification_archive(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:notifications-archive"),
            category="oscar_customer_notifications_archive",
        )

    def test_customer_alerts_list(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:alerts-list"),
            category="oscar_customer_alert_list",
        )

    def test_wishlist_list(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("customer:wishlists-list"),
            category="oscar_customer_wishlist_list",
        )

    def test_basket_summary(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("basket:summary"),
            category="oscar_basket_summary",
        )

    def test_checkout_summary(self):
        self.login(email=self.USER_EMAIL)
        self.assertScreenshotOk(
            reverse("checkout:index"),
            category="oscar_checkout_index",
        )
