"""Test IRC behavior of the markov plugin."""
from unittest import mock

from django.test import TestCase

from ircbot.models import IrcChannel, IrcServer
from markov.ircplugin import Markov


class MarkovTestCase(TestCase):
    """Test the markov plugin."""

    fixtures = ['tests/fixtures/irc_server_fixture.json']

    def setUp(self):
        """Create common objects."""
        self.mock_bot = mock.MagicMock()
        self.mock_connection = mock.MagicMock()

        self.mock_connection.get_nickname.return_value = 'test_bot'
        self.mock_connection.server_config = IrcServer.objects.get(pk=1)

        self.plugin = Markov(self.mock_bot, self.mock_connection, mock.MagicMock())

    def test_learn(self):
        """Test that an IRC event triggers learning as expected."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_self_edit(self):
        """Test that we don't learn our own name when learning something addressed to us."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['test_bot: hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            with mock.patch('markov.lib.generate_line'):
                self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_variant_self_edit(self):
        """Test that we don't learn our own name when learning something addressed to us, discord style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['@test_bot hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            with mock.patch('markov.lib.generate_line'):
                self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_bridge_edit(self):
        """Test that we don't learn the speaker's nick when learning a message from the bridge."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<tester> hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False
        mock_event.source = 'bridge!bridge@localhost'

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_bridge_no_edit(self):
        """Test that if a message is from the bridge, we learn what looks like a speaker's nick.

        This is primarily for if someone on IRC is pasting a log --- the "<somebody> whatever" should
        be retained in full, rather than learned as "whatever".
        """
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<tester> hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False
        mock_event.source = 'not-bridge!not-bridge@localhost'

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], '<tester> hello this is a test message')

    def test_learn_bridge_and_self_edit(self):
        """Test that we don't learn our own name when learning something addressed to us, discord style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<tester> test_bot: hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False
        mock_event.source = 'bridge!bridge@localhost'

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            with mock.patch('markov.lib.generate_line'):
                self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_bridge_and_variant_self_edit(self):
        """Test that we don't learn our own name when learning something addressed to us, discord style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<tester> @test_bot hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False
        mock_event.source = 'bridge!bridge@localhost'

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            with mock.patch('markov.lib.generate_line'):
                self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_learn_bridge_and_variant_with_colon_self_edit(self):
        """Test that we don't learn our own name when learning something addressed to us, discord style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<tester> @test_bot: hello this is a test message']
        mock_event.target = '#test'
        mock_event.recursing = False
        mock_event.source = 'bridge!bridge@localhost'

        with mock.patch('markov.lib.learn_line') as mock_learn_line:
            with mock.patch('markov.lib.generate_line'):
                self.plugin.handle_chatter(self.mock_connection, mock_event)

        self.assertEqual(mock_learn_line.call_args.args[0], 'hello this is a test message')

    def test_autocreate_ircchannel(self):
        """Test that we create the necessary config objects when seeing a target for the first time."""
        self.assertEqual(IrcChannel.objects.filter(name='#fakechannel').count(), 0)
        context = self.plugin.get_or_create_target_context('#fakechannel')

        self.assertEqual(IrcChannel.objects.filter(name='#fakechannel').count(), 1)
        self.assertIsNotNone(context)
        self.assertIsNotNone(context.markovtarget_set)
        self.assertIsNotNone(context.markovtarget_set.all()[0].channel)
        self.assertEqual(context.markovtarget_set.all()[0].channel.name, '#fakechannel')
        self.assertEqual(context.markovtarget_set.all()[0].name, '#fakechannel')