import os

from django.conf import settings
from django.test import TestCase
from django.urls import reverse
from django.utils.translation import gettext_lazy as _

from newsreader.accounts.tests.factories import UserFactory
from newsreader.news.collection.models import CollectionRule
from newsreader.news.collection.tests.factories import FeedFactory


class OPMLImportTestCase(TestCase):
    def setUp(self):
        self.user = UserFactory(password="test")
        self.client.force_login(self.user)

        self.form_data = {"file": "", "skip_existing": False}
        self.url = reverse("news:collection:import")

    def _get_file_path(self, name):
        file_dir = os.path.join(settings.DJANGO_PROJECT_DIR, "utils", "tests", "files")
        return os.path.join(file_dir, name)

    def test_simple(self):
        file_path = self._get_file_path("feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file)

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

        self.assertRedirects(response, reverse("news:collection:rules"))

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 4)

    def test_existing_rules(self):
        FeedFactory(url="http://www.engadget.com/rss-full.xml", user=self.user)
        FeedFactory(url="https://news.ycombinator.com/rss", user=self.user)
        FeedFactory(url="http://feeds.feedburner.com/Techcrunch", user=self.user)
        FeedFactory(url="http://feeds.feedburner.com/tweakers/nieuws", user=self.user)

        file_path = self._get_file_path("feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file)

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

        self.assertRedirects(response, reverse("news:collection:rules"))

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 8)

    def test_skip_existing_rules(self):
        FeedFactory(url="http://www.engadget.com/rss-full.xml", user=self.user)
        FeedFactory(url="https://news.ycombinator.com/rss", user=self.user)
        FeedFactory(url="http://feeds.feedburner.com/Techcrunch", user=self.user)
        FeedFactory(url="http://feeds.feedburner.com/tweakers/nieuws", user=self.user)

        file_path = self._get_file_path("feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file, skip_existing=True)

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

        self.assertEquals(response.status_code, 200)

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 4)

    def test_empty_feed_file(self):
        file_path = self._get_file_path("empty-feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file)

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

        self.assertEquals(response.status_code, 200)

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 0)

        self.assertFormError(response, "form", "file", _("No (new) feeds found"))

    def test_invalid_feeds(self):
        file_path = self._get_file_path("invalid-url-feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file)

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

        self.assertEquals(response.status_code, 200)

        rules = CollectionRule.objects.all()

        self.assertEquals(len(rules), 0)
        self.assertFormError(response, "form", "file", _("No (new) feeds found"))

    def test_invalid_file(self):
        file_path = self._get_file_path("test.png")

        with open(file_path, "rb") as file:
            self.form_data.update(file=file)

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

        self.assertEquals(response.status_code, 200)

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 0)

        self.assertFormError(response, "form", "file", _("Invalid OPML file"))

    def test_feeds_with_missing_attr(self):
        file_path = self._get_file_path("missing-feeds.opml")

        with open(file_path) as file:
            self.form_data.update(file=file)

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

        self.assertRedirects(response, reverse("news:collection:rules"))

        rules = CollectionRule.objects.all()
        self.assertEquals(len(rules), 2)
