"""
Tests for Project Settings Management functionality.

Tests the ability to modify project.godot settings programmatically.
"""

from pathlib import Path


from godot_mcp.server import project_setting_set, project_setting_get


class TestProjectSettings:
    """Tests for the project settings tools."""

    def test_project_setting_set_viewport_width(self, temp_godot_project: Path) -> None:
        """
        Test setting the viewport width in project settings.

        This modifies display/window/size/viewport_width.
        """
        # Set a new viewport width
        result = project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_width",
            setting_value=1920,
        )

        assert "successfully" in result.lower() or "set" in result.lower(), (
            f"project_setting_set failed: {result}"
        )

        # Verify the setting was saved to project.godot
        project_file = temp_godot_project / "project.godot"
        content = project_file.read_text(encoding="utf-8")

        # Check that the setting exists in the file
        assert "viewport_width" in content, (
            f"viewport_width not found in project.godot. Content:\n{content}"
        )
        assert "1920" in content, f"Value 1920 not found in project.godot. Content:\n{content}"

    def test_project_setting_set_viewport_height(self, temp_godot_project: Path) -> None:
        """Test setting the viewport height."""
        result = project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_height",
            setting_value=1080,
        )

        assert "successfully" in result.lower() or "set" in result.lower(), (
            f"project_setting_set failed: {result}"
        )

        # Verify the setting
        project_file = temp_godot_project / "project.godot"
        content = project_file.read_text(encoding="utf-8")
        assert "1080" in content

    def test_project_setting_set_string_value(self, temp_godot_project: Path) -> None:
        """Test setting a string value in project settings."""
        result = project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="application/config/name",
            setting_value="My Awesome Game",
        )

        assert "successfully" in result.lower() or "set" in result.lower(), (
            f"project_setting_set failed: {result}"
        )

        # Verify the setting
        project_file = temp_godot_project / "project.godot"
        content = project_file.read_text(encoding="utf-8")
        assert "My Awesome Game" in content

    def test_project_setting_set_boolean_value(self, temp_godot_project: Path) -> None:
        """Test setting a boolean value in project settings."""
        result = project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/resizable",
            setting_value=False,
        )

        assert "successfully" in result.lower() or "set" in result.lower(), (
            f"project_setting_set failed: {result}"
        )

    def test_project_setting_set_invalid_project(self) -> None:
        """Test that setting with invalid project path fails gracefully."""
        result = project_setting_set(
            project_path="/nonexistent/project/path",
            setting_key="display/window/size/viewport_width",
            setting_value=1920,
        )

        assert (
            "error" in result.lower() or "not" in result.lower() or "invalid" in result.lower()
        ), f"Expected error for invalid project, got: {result}"

    def test_project_setting_set_missing_parameters(self) -> None:
        """Test that missing required parameters are handled."""
        result = project_setting_set(
            project_path="",
            setting_key="some/key",
            setting_value=123,
        )
        assert "required" in result.lower() or "invalid" in result.lower(), (
            f"Expected error for empty project_path, got: {result}"
        )

    def test_project_setting_get(self, temp_godot_project: Path) -> None:
        """Test getting a project setting value."""
        # First set a value
        project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_width",
            setting_value=1280,
        )

        # Then get it back
        result = project_setting_get(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_width",
        )

        assert "1280" in result, f"Expected to find 1280 in result, got: {result}"

    def test_set_multiple_settings(self, temp_godot_project: Path) -> None:
        """Test setting multiple settings in sequence."""
        # Set width
        project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_width",
            setting_value=1920,
        )

        # Set height
        project_setting_set(
            project_path=str(temp_godot_project),
            setting_key="display/window/size/viewport_height",
            setting_value=1080,
        )

        # Verify both are in the file
        project_file = temp_godot_project / "project.godot"
        content = project_file.read_text(encoding="utf-8")

        assert "1920" in content
        assert "1080" in content
