"""
Tests for the test font utilities module.

These tests verify that the bundled font system works correctly for consistent testing.
"""

import unittest
import os
from PIL import ImageFont

from tests.utils.test_fonts import (
    get_bundled_font_path,
    verify_bundled_font_available,
    create_test_font,
    create_default_test_font,
    ensure_consistent_font_in_tests
)
from pyWebLayout.style.fonts import Font, FontWeight, FontStyle, TextDecoration


class TestFontUtilities(unittest.TestCase):
    """Test cases for font utility functions."""

    def test_get_bundled_font_path_finds_font(self):
        """Test that get_bundled_font_path finds the bundled font."""
        font_path = get_bundled_font_path()
        self.assertIsNotNone(font_path, "Bundled font path should not be None")
        self.assertTrue(
            os.path.exists(font_path),
            f"Font file should exist at {font_path}")
        self.assertTrue(
            font_path.endswith("DejaVuSans.ttf"),
            "Font path should end with DejaVuSans.ttf")

    def test_verify_bundled_font_available(self):
        """Test that the bundled font can be verified and loaded."""
        self.assertTrue(verify_bundled_font_available(),
                        "Bundled font should be available and loadable")

    def test_create_test_font_with_defaults(self):
        """Test creating a test font with default parameters."""
        font = create_test_font()
        self.assertIsInstance(font, Font)
        self.assertEqual(font.font_size, 16)
        self.assertEqual(font.colour, (0, 0, 0))
        self.assertEqual(font.weight, FontWeight.NORMAL)
        self.assertEqual(font.style, FontStyle.NORMAL)
        self.assertEqual(font.decoration, TextDecoration.NONE)

    def test_create_test_font_with_custom_parameters(self):
        """Test creating a test font with custom parameters."""
        font = create_test_font(
            font_size=24,
            colour=(255, 0, 0),
            weight=FontWeight.BOLD,
            style=FontStyle.ITALIC,
            decoration=TextDecoration.UNDERLINE
        )
        self.assertIsInstance(font, Font)
        self.assertEqual(font.font_size, 24)
        self.assertEqual(font.colour, (255, 0, 0))
        self.assertEqual(font.weight, FontWeight.BOLD)
        self.assertEqual(font.style, FontStyle.ITALIC)
        self.assertEqual(font.decoration, TextDecoration.UNDERLINE)

    def test_create_default_test_font(self):
        """Test creating a default test font."""
        font = create_default_test_font()
        self.assertIsInstance(font, Font)
        self.assertEqual(font.font_size, 16)
        self.assertEqual(font.colour, (0, 0, 0))

    def test_ensure_consistent_font_in_tests_succeeds(self):
        """Test that ensure_consistent_font_in_tests runs without error when font is available."""
        # This should not raise any exceptions if the font is properly available
        try:
            ensure_consistent_font_in_tests()
        except RuntimeError:
            self.fail(
                "ensure_consistent_font_in_tests() raised RuntimeError when font should be available")

    def test_bundled_font_loads_with_pil(self):
        """Test that the bundled font can be loaded directly with PIL."""
        font_path = get_bundled_font_path()
        self.assertIsNotNone(font_path)

        # Test loading with different sizes
        for size in [12, 16, 24, 48]:
            with self.subTest(size=size):
                pil_font = ImageFont.truetype(font_path, size)
                self.assertIsNotNone(pil_font)

    def test_font_metrics_consistency(self):
        """Test that font metrics are consistent between different Font objects using the same parameters."""
        font1 = create_test_font(font_size=16)
        font2 = create_test_font(font_size=16)

        # Both fonts should have the same size
        self.assertEqual(font1.font_size, font2.font_size)

        # Test that text measurements are consistent
        # This is a basic check - in real usage, text measurement consistency is
        # what matters most
        self.assertEqual(font1.font_size, font2.font_size)

    def test_different_sizes_create_different_fonts(self):
        """Test that different font sizes create fonts with different metrics."""
        small_font = create_test_font(font_size=12)
        large_font = create_test_font(font_size=24)

        self.assertNotEqual(small_font.font_size, large_font.font_size)
        self.assertEqual(small_font.font_size, 12)
        self.assertEqual(large_font.font_size, 24)


class TestFontPathResolution(unittest.TestCase):
    """Test cases for font path resolution from different locations."""

    def test_font_path_is_absolute(self):
        """Test that the returned font path is absolute."""
        font_path = get_bundled_font_path()
        if font_path:
            self.assertTrue(os.path.isabs(font_path), "Font path should be absolute")

    def test_font_path_points_to_file(self):
        """Test that the font path points to a file, not a directory."""
        font_path = get_bundled_font_path()
        if font_path:
            self.assertTrue(
                os.path.isfile(font_path),
                "Font path should point to a file")

    def test_font_file_has_correct_extension(self):
        """Test that the font file has the expected .ttf extension."""
        font_path = get_bundled_font_path()
        if font_path:
            self.assertTrue(
                font_path.lower().endswith('.ttf'),
                "Font file should have .ttf extension")


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