"""
Unit tests for LinkedWord and LinkedImage classes.
"""

import unittest
from pyWebLayout.abstract.inline import LinkedWord
from pyWebLayout.abstract.block import LinkedImage
from pyWebLayout.abstract.functional import LinkType
from pyWebLayout.style import Font


class TestLinkedWord(unittest.TestCase):
    """Test cases for LinkedWord class."""

    def setUp(self):
        """Set up test fixtures."""
        self.font = Font(font_size=16)
        self.location = "https://example.com"

    def test_linked_word_creation(self):
        """Test creating a LinkedWord."""
        linked_word = LinkedWord(
            text="example",
            style=self.font,
            location=self.location,
            link_type=LinkType.EXTERNAL
        )

        self.assertEqual(linked_word.text, "example")
        self.assertEqual(linked_word.location, self.location)
        self.assertEqual(linked_word.link_type, LinkType.EXTERNAL)
        self.assertIsNone(linked_word.link_callback)

    def test_linked_word_inherits_from_word(self):
        """Test that LinkedWord inherits Word properties."""
        linked_word = LinkedWord(
            text="test",
            style=self.font,
            location=self.location
        )

        # Should have Word properties
        self.assertEqual(linked_word.text, "test")
        self.assertEqual(linked_word.style, self.font)
        self.assertIsNone(linked_word.previous)
        self.assertIsNone(linked_word.next)

    def test_linked_word_with_callback(self):
        """Test LinkedWord with a callback function."""
        callback_called = []

        def test_callback(location, **params):
            callback_called.append((location, params))
            return "navigated"

        linked_word = LinkedWord(
            text="click",
            style=self.font,
            location=self.location,
            link_type=LinkType.FUNCTION,
            callback=test_callback,
            params={"source": "test"}
        )

        linked_word.execute_link()

        self.assertEqual(len(callback_called), 1)
        self.assertEqual(callback_called[0][0], self.location)
        self.assertIn("text", callback_called[0][1])
        self.assertEqual(callback_called[0][1]["text"], "click")
        self.assertEqual(callback_called[0][1]["source"], "test")

    def test_linked_word_execute_external_link(self):
        """Test executing an external link returns the location."""
        linked_word = LinkedWord(
            text="link",
            style=self.font,
            location=self.location,
            link_type=LinkType.EXTERNAL
        )

        result = linked_word.execute_link()
        self.assertEqual(result, self.location)

    def test_linked_word_with_title(self):
        """Test LinkedWord with title/tooltip."""
        linked_word = LinkedWord(
            text="hover",
            style=self.font,
            location=self.location,
            title="Click to visit example.com"
        )

        self.assertEqual(linked_word.link_title, "Click to visit example.com")

    def test_linked_word_chain(self):
        """Test chaining multiple LinkedWords."""
        word1 = LinkedWord(
            text="click",
            style=self.font,
            location=self.location
        )

        word2 = LinkedWord(
            text="here",
            style=self.font,
            location=self.location,
            previous=word1
        )

        # Check chain
        self.assertEqual(word1.next, word2)
        self.assertEqual(word2.previous, word1)


class TestLinkedImage(unittest.TestCase):
    """Test cases for LinkedImage class."""

    def setUp(self):
        """Set up test fixtures."""
        self.source = "logo.png"
        self.alt_text = "Company Logo"
        self.location = "https://example.com/home"

    def test_linked_image_creation(self):
        """Test creating a LinkedImage."""
        linked_image = LinkedImage(
            source=self.source,
            alt_text=self.alt_text,
            location=self.location,
            width=100,
            height=50,
            link_type=LinkType.EXTERNAL
        )

        self.assertEqual(linked_image.source, self.source)
        self.assertEqual(linked_image.alt_text, self.alt_text)
        self.assertEqual(linked_image.location, self.location)
        self.assertEqual(linked_image.width, 100)
        self.assertEqual(linked_image.height, 50)
        self.assertEqual(linked_image.link_type, LinkType.EXTERNAL)

    def test_linked_image_inherits_from_image(self):
        """Test that LinkedImage inherits Image properties."""
        linked_image = LinkedImage(
            source=self.source,
            alt_text=self.alt_text,
            location=self.location
        )

        # Should have Image properties and methods
        self.assertEqual(linked_image.source, self.source)
        self.assertEqual(linked_image.alt_text, self.alt_text)
        self.assertIsNotNone(linked_image.get_dimensions)

    def test_linked_image_with_callback(self):
        """Test LinkedImage with a callback function."""
        callback_called = []

        def image_callback(location, **params):
            callback_called.append((location, params))
            return "image_clicked"

        linked_image = LinkedImage(
            source=self.source,
            alt_text=self.alt_text,
            location=self.location,
            link_type=LinkType.FUNCTION,
            callback=image_callback
        )

        linked_image.execute_link()

        self.assertEqual(len(callback_called), 1)
        self.assertEqual(callback_called[0][0], self.location)
        self.assertIn("alt_text", callback_called[0][1])
        self.assertEqual(callback_called[0][1]["alt_text"], self.alt_text)
        self.assertIn("source", callback_called[0][1])

    def test_linked_image_execute_internal_link(self):
        """Test executing an internal link returns the location."""
        linked_image = LinkedImage(
            source=self.source,
            alt_text=self.alt_text,
            location="#section2",
            link_type=LinkType.INTERNAL
        )

        result = linked_image.execute_link()
        self.assertEqual(result, "#section2")


if __name__ == '__main__':
    unittest.main()
