from unittest import TestCase

from xml.etree.ElementTree import tostring, fromstring

from pymasep.common import Intention, Environment, BaseObject, Action
from test.unit.mocks.game_mock import GameMock


class TestIntention(TestCase):

    def test_init(self):
        expected_name = 'intention'
        env = Environment()
        game = GameMock()
        intention = Intention(environment=env, name='false_name', template=game.templates['EmptyIntentionTemplate'])
        self.assertIsInstance(intention, BaseObject)
        self.assertIsInstance(intention, Intention)
        self.assertEqual(env, intention.environment)
        self.assertEqual(expected_name, intention.name)
        self.assertEqual(0, len(intention.intentions))

    def test_add_intention_intention_added(self):
        env = Environment()
        game = GameMock()
        intention = Intention(environment=env, name='false_name', template=game.templates['EmptyIntentionTemplate'])
        action = env.create_action(game.ACTION_TYPE_1)
        intention.add_intention(action)
        self.assertEqual(1, len(intention.intentions))
        self.assertEqual(action, intention.intentions[0])

    def test_to_xml_transformed(self):
        expected_xml_str = '<Intention name="intention" state="run">' \
                           '<Action type="1" />' \
                           '</Intention>'
        env = Environment()
        game = GameMock()
        intention = Intention(environment=env, name='false_name', template=game.templates['EmptyIntentionTemplate'])
        action = env.create_action(game.ACTION_TYPE_1)
        intention.add_intention(action)

        xml_str_result = tostring(intention.to_xml(), encoding='unicode')

        self.assertEqual(expected_xml_str, xml_str_result)

    def test_from_xml_no_template_should_init(self):
        intention_xml_str = '<Intention name="intention" state="run">' \
                            '<Action type="1" />' \
                            '</Intention>'
        env = Environment()
        game = GameMock()
        env.game = game
        xml_node = fromstring(intention_xml_str)

        intention = Intention(environment=env, xml_node=xml_node)
        intention.from_xml(env, xml_node)

        self.assertIsInstance(intention, Intention)
        self.assertEqual('intention', intention.name)
        self.assertIsNone(intention.parent)
        self.assertEqual(1, len(intention.intentions))
        self.assertIsInstance(intention.intentions[0], Action)
        self.assertEqual(1, intention.intentions[0].type)

    def test_id_str(self):
        intention_xml_str = '<Intention name="intention" state="run">' \
                            '<Action type="1" />' \
                            '</Intention>'
        env = Environment()
        game = GameMock()
        env.game = game
        xml_node = fromstring(intention_xml_str)
        intention = Intention(environment=env, xml_node=xml_node)
        intention.from_xml(env, xml_node)

        id_str = intention.id_str()

        self.assertEqual('intentionaction1', id_str)
