from django.test import TestCase
from django.urls import reverse

import pytz

from django_celery_beat.models import PeriodicTask

from newsreader.news.collection.choices import RuleTypeChoices
from newsreader.news.collection.models import CollectionRule
from newsreader.news.collection.tests.factories import TwitterTimelineFactory
from newsreader.news.collection.tests.views.base import CollectionRuleViewTestCase
from newsreader.news.collection.twitter import TWITTER_API_URL
from newsreader.news.core.tests.factories import CategoryFactory


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

        self.form_data = {
            "name": "new rule",
            "screen_name": "RobertsSpaceInd",
            "category": str(self.category.pk),
        }

        self.url = reverse("news:collection:twitter-timeline-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.twitter_timeline)
        self.assertEquals(
            rule.url,
            f"{TWITTER_API_URL}/statuses/user_timeline.json?screen_name=RobertsSpaceInd&tweet_mode=extended",
        )
        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)

        self.assertTrue(
            PeriodicTask.objects.get(
                name=f"{self.user.email}-timeline",
                task="TwitterTimelineTask",
                enabled=True,
            )
        )


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

        self.rule = TwitterTimelineFactory(
            name="Star citizen",
            screen_name="RobertsSpaceInd",
            user=self.user,
            category=self.category,
            type=RuleTypeChoices.twitter_timeline,
        )
        self.url = reverse(
            "news:collection:twitter-timeline-update", kwargs={"pk": self.rule.pk}
        )

        self.form_data = {
            "name": self.rule.name,
            "screen_name": self.rule.screen_name,
            "category": str(self.category.pk),
            "timezone": pytz.utc,
        }

    def test_name_change(self):
        self.form_data.update(name="Star citizen Twitter")

        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, "Star citizen Twitter")

    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_twitter_timelines_only(self):
        rule = TwitterTimelineFactory(
            name="Fake twitter",
            user=self.user,
            category=self.category,
            type=RuleTypeChoices.feed,
            url="https://twitter.com/RobertsSpaceInd",
        )
        url = reverse("news:collection:twitter-timeline-update", kwargs={"pk": rule.pk})

        response = self.client.get(url)

        self.assertEquals(response.status_code, 404)

    def test_screen_name_change(self):
        self.form_data.update(screen_name="CyberpunkGame")

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

        self.assertEquals(response.status_code, 302)

        self.rule.refresh_from_db()

        self.assertEquals(self.rule.type, RuleTypeChoices.twitter_timeline)
        self.assertEquals(
            self.rule.url,
            f"{TWITTER_API_URL}/statuses/user_timeline.json?screen_name=CyberpunkGame&tweet_mode=extended",
        )
        self.assertEquals(self.rule.timezone, str(pytz.utc))
        self.assertEquals(self.rule.favicon, None)
        self.assertEquals(self.rule.category.pk, self.category.pk)
        self.assertEquals(self.rule.user.pk, self.user.pk)
