from django.test import TestCase
from django.urls import reverse
from django.utils.translation import gettext as _

import pytz

from newsreader.news.collection.choices import RuleTypeChoices
from newsreader.news.collection.models import CollectionRule
from newsreader.news.collection.reddit import REDDIT_API_URL, REDDIT_URL
from newsreader.news.collection.tests.factories import SubredditFactory
from newsreader.news.collection.tests.views.base import CollectionRuleViewTestCase
from newsreader.news.core.tests.factories import CategoryFactory


class SubRedditCreateViewTestCase(CollectionRuleViewTestCase, TestCase):
    def setUp(self):
        super().setUp()

        self.form_data = {
            "name": "new rule",
            "url": f"{REDDIT_API_URL}/r/aww",
            "category": str(self.category.pk),
            "reddit_allow_nfsw": False,
            "reddit_allow_spoiler": False,
            "reddit_allow_viewed": True,
            "reddit_upvotes_min": 0,
            "reddit_comments_min": 0,
        }

        self.url = reverse("news:collection:subreddit-create")

    def test_creation(self):
        response = self.client.post(self.url, self.form_data)

        self.assertEquals(response.status_code, 302)

        rule = CollectionRule.objects.get(name="new rule")

        self.assertEquals(rule.type, RuleTypeChoices.subreddit)
        self.assertEquals(rule.url, f"{REDDIT_API_URL}/r/aww")
        self.assertEquals(rule.timezone, str(pytz.utc))
        self.assertEquals(rule.favicon, None)
        self.assertEquals(rule.category.pk, self.category.pk)
        self.assertEquals(rule.user.pk, self.user.pk)

    def test_regular_reddit_url(self):
        self.form_data.update(url=f"{REDDIT_URL}/r/aww")

        response = self.client.post(self.url, self.form_data)

        self.assertContains(response, _("This does not look like an Reddit API URL"))


class SubRedditUpdateViewTestCase(CollectionRuleViewTestCase, TestCase):
    def setUp(self):
        super().setUp()

        self.rule = SubredditFactory(
            name="Python",
            url=f"{REDDIT_API_URL}/r/python.json",
            user=self.user,
            category=self.category,
            type=RuleTypeChoices.subreddit,
        )
        self.url = reverse(
            "news:collection:subreddit-update", kwargs={"pk": self.rule.pk}
        )

        self.form_data = {
            "name": self.rule.name,
            "url": self.rule.url,
            "category": str(self.category.pk),
            "timezone": pytz.utc,
            "reddit_allow_nfsw": False,
            "reddit_allow_spoiler": False,
            "reddit_allow_viewed": True,
            "reddit_upvotes_min": 0,
            "reddit_comments_min": 0,
        }

    def test_name_change(self):
        self.form_data.update(name="Python 2")

        response = self.client.post(self.url, self.form_data)
        self.assertEquals(response.status_code, 302)

        self.rule.refresh_from_db()

        self.assertEquals(self.rule.name, "Python 2")

    def test_category_change(self):
        new_category = CategoryFactory(user=self.user)

        self.form_data.update(category=new_category.pk)

        response = self.client.post(self.url, self.form_data)
        self.assertEquals(response.status_code, 302)

        self.rule.refresh_from_db()

        self.assertEquals(self.rule.category.pk, new_category.pk)

    def test_subreddit_rules_only(self):
        rule = SubredditFactory(
            name="Fake subreddit",
            url="https://leddit.com/r/python",
            user=self.user,
            category=self.category,
            type=RuleTypeChoices.feed,
        )
        url = reverse("news:collection:subreddit-update", kwargs={"pk": rule.pk})

        response = self.client.get(url)

        self.assertEquals(response.status_code, 404)

    def test_url_change(self):
        self.form_data.update(name="aww", url=f"{REDDIT_API_URL}/r/aww")

        response = self.client.post(self.url, self.form_data)

        self.assertEquals(response.status_code, 302)

        rule = CollectionRule.objects.get(name="aww")

        self.assertEquals(rule.type, RuleTypeChoices.subreddit)
        self.assertEquals(rule.url, f"{REDDIT_API_URL}/r/aww")
        self.assertEquals(rule.timezone, str(pytz.utc))
        self.assertEquals(rule.favicon, None)
        self.assertEquals(rule.category.pk, self.category.pk)
        self.assertEquals(rule.user.pk, self.user.pk)

    def test_regular_reddit_url(self):
        self.form_data.update(url=f"{REDDIT_URL}/r/aww")

        response = self.client.post(self.url, self.form_data)

        self.assertContains(response, _("This does not look like an Reddit API URL"))
