"""
Tests for the template generation in fontpls.templates
"""
import unittest

from fontpls.templates.css import create_stylesheet
from fontpls.templates.html import create_demo_html


class TestCSSTemplates(unittest.TestCase):
    """Tests for CSS template generation."""

    def test_create_stylesheet_empty(self):
        """Test creating a stylesheet with no fonts."""
        result = create_stylesheet({})
        self.assertIn("/* Font definitions generated by fontpls */", result)
        self.assertIsInstance(result, str)

    def test_create_stylesheet_single_font(self):
        """Test creating a stylesheet with a single font."""
        font_metadata = {
            "https://example.com/fonts/font1.woff2": {
                "family": "Test Font",
                "style": "Regular",
                "path": "/tmp/fonts/test-font-regular.woff2",
                "format": "woff2",
                "filename": "test-font-regular.woff2",
            }
        }

        result = create_stylesheet(font_metadata)

        # Check for required elements in the output
        self.assertIn("@font-face", result)
        self.assertIn("font-family: 'Test Font'", result)
        self.assertIn("src: url('test-font-regular.woff2') format('woff2')", result)
        self.assertIn("font-weight: 400", result)
        self.assertIn("font-style: normal", result)

    def test_create_stylesheet_multiple_fonts(self):
        """Test creating a stylesheet with multiple fonts."""
        font_metadata = {
            "https://example.com/fonts/font1.woff2": {
                "family": "Test Font",
                "style": "Regular",
                "path": "/tmp/fonts/test-font-regular.woff2",
                "format": "woff2",
                "filename": "test-font-regular.woff2",
            },
            "https://example.com/fonts/font2.woff2": {
                "family": "Test Font",
                "style": "Bold",
                "path": "/tmp/fonts/test-font-bold.woff2",
                "format": "woff2",
                "filename": "test-font-bold.woff2",
            },
            "https://example.com/fonts/font3.woff2": {
                "family": "Another Font",
                "style": "Italic",
                "path": "/tmp/fonts/another-font-italic.woff2",
                "format": "woff2",
                "filename": "another-font-italic.woff2",
            },
        }

        result = create_stylesheet(font_metadata)

        # Check for all fonts in the output
        self.assertIn("font-family: 'Test Font'", result)
        self.assertIn("font-family: 'Another Font'", result)

        # Check for weight detection
        self.assertIn("font-weight: 700", result)  # Bold should be 700

        # Check for style detection
        self.assertIn("font-style: italic", result)

    def test_font_weight_detection(self):
        """Test detection of font weights from style names."""
        test_cases = [
            ("Bold", "700"),
            ("Black", "700"),
            ("Light", "300"),
            ("Thin", "100"),
            ("Medium", "500"),
            ("Regular", "400"),
            ("Normal", "400"),
            ("Extra Bold", "700"),
            ("Ultra Light", "100"),
        ]

        for style, expected_weight in test_cases:
            font_metadata = {
                f"https://example.com/fonts/{style.lower()}.woff2": {
                    "family": "Test Font",
                    "style": style,
                    "path": f"/tmp/fonts/test-font-{style.lower()}.woff2",
                    "format": "woff2",
                    "filename": f"test-font-{style.lower()}.woff2",
                }
            }

            result = create_stylesheet(font_metadata)
            self.assertIn(f"font-weight: {expected_weight}", result)

    def test_font_style_detection(self):
        """Test detection of font styles from style names."""
        test_cases = [
            ("Regular", "normal"),
            ("Bold", "normal"),
            ("Italic", "italic"),
            ("Bold Italic", "italic"),
            ("Light Italic", "italic"),
            ("Oblique", "italic"),
        ]

        for style, expected_font_style in test_cases:
            font_metadata = {
                f"https://example.com/fonts/{style.lower().replace(' ', '-')}.woff2": {
                    "family": "Test Font",
                    "style": style,
                    "path": f"/tmp/fonts/test-font-{style.lower().replace(' ', '-')}.woff2",
                    "format": "woff2",
                    "filename": f"test-font-{style.lower().replace(' ', '-')}.woff2",
                }
            }

            result = create_stylesheet(font_metadata)
            self.assertIn(f"font-style: {expected_font_style}", result)


class TestHTMLTemplates(unittest.TestCase):
    """Tests for HTML template generation."""

    def test_create_demo_html_empty(self):
        """Test creating a demo HTML with no fonts."""
        result = create_demo_html({})
        self.assertIn("<!DOCTYPE html>", result)
        self.assertIn("<title>Font Demo</title>", result)
        self.assertIn('<link rel="stylesheet" href="fonts.css">', result)
        self.assertIsInstance(result, str)

    def test_create_demo_html_single_font(self):
        """Test creating a demo HTML with a single font."""
        font_metadata = {
            "https://example.com/fonts/font1.woff2": {
                "family": "Test Font",
                "style": "Regular",
                "weight": "400",
                "font_style": "normal",
                "path": "/tmp/fonts/test-font-regular.woff2",
                "format": "woff2",
                "filename": "test-font-regular.woff2",
            }
        }

        result = create_demo_html(font_metadata)

        # Check for required elements in the output
        self.assertIn("<title>Font Demo</title>", result)
        self.assertIn('<link rel="stylesheet" href="fonts.css">', result)
        self.assertIn("<h2>Test Font Regular</h2>", result)
        self.assertIn("font-family: 'Test Font', sans-serif", result)
        self.assertIn("font-weight: 400", result)
        self.assertIn("font-style: normal", result)
        self.assertIn("Font Weight: 400", result)
        self.assertIn("Font Style: normal", result)
        self.assertIn("Filename: test-font-regular.woff2", result)

    def test_create_demo_html_multiple_fonts(self):
        """Test creating a demo HTML with multiple fonts."""
        font_metadata = {
            "https://example.com/fonts/font1.woff2": {
                "family": "Test Font",
                "style": "Regular",
                "weight": "400",
                "font_style": "normal",
                "path": "/tmp/fonts/test-font-regular.woff2",
                "format": "woff2",
                "filename": "test-font-regular.woff2",
            },
            "https://example.com/fonts/font2.woff2": {
                "family": "Test Font",
                "style": "Bold",
                "weight": "700",
                "font_style": "normal",
                "path": "/tmp/fonts/test-font-bold.woff2",
                "format": "woff2",
                "filename": "test-font-bold.woff2",
            },
            "https://example.com/fonts/font3.woff2": {
                "family": "Another Font",
                "style": "Italic",
                "weight": "400",
                "font_style": "italic",
                "path": "/tmp/fonts/another-font-italic.woff2",
                "format": "woff2",
                "filename": "another-font-italic.woff2",
            },
        }

        result = create_demo_html(font_metadata)

        # Check for all fonts in the output
        self.assertIn("<h2>Test Font Regular</h2>", result)
        self.assertIn("<h2>Test Font Bold</h2>", result)
        self.assertIn("<h2>Another Font Italic</h2>", result)

        # Check for font properties
        self.assertIn("font-family: 'Test Font', sans-serif", result)
        self.assertIn("font-family: 'Another Font', sans-serif", result)
        self.assertIn("font-weight: 700", result)
        self.assertIn("font-style: italic", result)


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