# coding: utf-8

from django.core import mail
from django.conf import settings as project_settings

from the_tale.common.utils import testcase

from the_tale.game.logic import create_test_map

from the_tale.forum.prototypes import ThreadPrototype, SubCategoryPrototype, CategoryPrototype, SubscriptionPrototype, PostPrototype

from the_tale.post_service.models import Message
from the_tale.post_service.prototypes import MessagePrototype


class NewForumPostTests(testcase.TestCase):

    def setUp(self):
        super(NewForumPostTests, self).setUp()

        create_test_map()

        self.account_1 = self.accounts_factory.create_account()

        self.category = CategoryPrototype.create(caption='cat-caption', slug='cat-slug', order=0)
        self.subcategory = SubCategoryPrototype.create(category=self.category, caption='subcat-caption', order=0)
        self.thread = ThreadPrototype.create(self.subcategory, 'thread_1-caption', self.account_1, 'thread-text')

        Message.objects.all().delete()

        self.post = PostPrototype.create(self.thread, self.account_1, 'post-text')

        self.message = MessagePrototype.get_priority_message()


    def test_register_message(self):
        self.assertEqual(Message.objects.all().count(), 1)

    def test_no_subscriptions(self):
        self.assertEqual(len(mail.outbox), 0)
        self.message.process()
        self.assertTrue(self.message.state.is_PROCESSED)
        self.assertEqual(len(mail.outbox), 0)

    def test_one_subscription(self):
        SubscriptionPrototype.create(self.account_1, self.thread)
        self.assertEqual(len(mail.outbox), 0)
        self.message.process()
        self.assertTrue(self.message.state.is_PROCESSED)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [self.account_1.email])

        self.assertTrue(self.post.author.nick in mail.outbox[0].body)
        self.assertTrue(self.post.thread.caption in mail.outbox[0].body)
        self.assertTrue(self.post.thread.paginator.last_page_url in mail.outbox[0].body)
        self.assertTrue(project_settings.SITE_URL in mail.outbox[0].body)

        self.assertTrue(self.post.author.nick in mail.outbox[0].alternatives[0][0])
        self.assertTrue(self.post.thread.caption in mail.outbox[0].alternatives[0][0])
        self.assertTrue(self.post.thread.paginator.last_page_url in mail.outbox[0].alternatives[0][0])
        self.assertTrue(project_settings.SITE_URL in mail.outbox[0].alternatives[0][0])

    def test_mail_send__to_system_user(self):
        from the_tale.accounts.logic import get_system_user

        SubscriptionPrototype.create(get_system_user(), self.thread)
        self.assertEqual(len(mail.outbox), 0)
        self.message.process()
        self.assertTrue(self.message.state.is_PROCESSED)
        self.assertEqual(len(mail.outbox), 0)

    def test_many_subscriptions(self):
        account_2 = self.accounts_factory.create_account()

        SubscriptionPrototype.create(self.account_1, self.thread)
        SubscriptionPrototype.create(account_2, self.thread)

        self.assertEqual(len(mail.outbox), 0)
        self.message.process()
        self.assertTrue(self.message.state.is_PROCESSED)
        self.assertEqual(len(mail.outbox), 2)
        self.assertEqual(mail.outbox[0].to, [self.account_1.email])
        self.assertEqual(mail.outbox[1].to, [account_2.email])
