"""Test core methods of the IRC bot."""
from unittest import mock

from django.test import TestCase

from ircbot.bot import DrReactor
from ircbot.models import IrcServer


class DrReactorTestCase(TestCase):
    """Test the bot innards."""

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

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

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

    def test_handle_event_not_addressed(self):
        """Test that the core identifies not being addressed."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['someone: hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertFalse(mock_event.addressed)

    def test_handle_event_addressed(self):
        """Test that the core identifies being addressed IRC style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['test_bot: hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertTrue(mock_event.addressed)

    def test_handle_event_addressed_alternate(self):
        """Test that the core identifies being addressed in the alternate (e.g. discord) style."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['@test_bot hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertTrue(mock_event.addressed)

    def test_handle_event_bridge_addressed(self):
        """Test that the core identifies being addressed IRC style through a discord bridge."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<someone> test_bot: hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        mock_event.source = 'bridge!bridge@localhost'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertTrue(mock_event.addressed)

    def test_handle_event_bridge_addressed_alternate(self):
        """Test that the core identifies being addressed in the alternate style through a discord bridge."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<someone> @test_bot hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        mock_event.source = 'bridge!bridge@localhost'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertTrue(mock_event.addressed)

    def test_handle_event_bridge_addressed_alternate_with_colon(self):
        """Test that the core identifies being addressed in the alternate style through a discord bridge."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<someone> @test_bot: hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        mock_event.source = 'bridge!bridge@localhost'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertTrue(mock_event.addressed)

    def test_handle_event_not_addressed_not_bridge(self):
        """Test that the core identifies not to chop the first word from a paste not from the bridge user."""
        mock_event = mock.MagicMock()
        mock_event.arguments = ['<somebody> test_bot: hello this is a test message']
        mock_event.type = 'pubmsg'
        mock_event.target = '#test'
        mock_event.source = 'not-bridge!not-bridge@localhost'
        self.bot._handle_event(self.mock_connection, mock_event)

        self.assertFalse(mock_event.addressed)