"""
E2E tests for TileMap operations.

Tests TileMap manipulation using real Godot demo projects:
- tilemap_set_cell
- tilemap_get_cell
"""

import json
from pathlib import Path

import pytest

from godot_mcp.server import (
    node_add,
    scene_create,
    tilemap_get_cell,
    tilemap_set_cell,
)


class TestSetTilemapCell:
    """E2E tests for tilemap_set_cell tool."""

    @pytest.fixture
    def tilemap_scene(self, demo_project_copy: Path) -> Path:
        """Create a scene with a TileMap node for testing."""
        scene_create(
            project_path=str(demo_project_copy),
            scene_path="e2e_tilemap_test.tscn",
            root_node_type="Node2D",
        )
        node_add(
            project_path=str(demo_project_copy),
            scene_path="e2e_tilemap_test.tscn",
            node_type="TileMap",
            node_name="TestTileMap",
            parent_node_path="root",
        )
        return Path("e2e_tilemap_test.tscn")

    def test_tilemap_set_cell_basic(
        self,
        demo_project_copy: Path,
        tilemap_scene: Path,
    ) -> None:
        """Test setting a basic tilemap cell."""
        result = tilemap_set_cell(
            project_path=str(demo_project_copy),
            scene_path=str(tilemap_scene),
            node_path="root/TestTileMap",
            x=0,
            y=0,
            source_id=0,
            atlas_x=0,
            atlas_y=0,
        )

        # May warn about missing tileset, but should not error
        assert isinstance(result, str)

    def test_set_tilemap_multiple_cells(
        self,
        demo_project_copy: Path,
        tilemap_scene: Path,
    ) -> None:
        """Test setting multiple tilemap cells."""
        # Set a row of cells
        for x in range(5):
            result = tilemap_set_cell(
                project_path=str(demo_project_copy),
                scene_path=str(tilemap_scene),
                node_path="root/TestTileMap",
                x=x,
                y=0,
                source_id=0,
            )
            assert isinstance(result, str)

    def test_tilemap_set_cell_invalid_scene(self, demo_project_copy: Path) -> None:
        """Test tilemap_set_cell with invalid scene."""
        result = tilemap_set_cell(
            project_path=str(demo_project_copy),
            scene_path="nonexistent.tscn",
            node_path="root/TileMap",
            x=0,
            y=0,
        )

        assert "error" in result.lower() or "not exist" in result.lower()


class TestGetTilemapCell:
    """E2E tests for tilemap_get_cell tool."""

    @pytest.fixture
    def tilemap_scene(self, demo_project_copy: Path) -> Path:
        """Create a scene with a TileMap node for testing."""
        scene_create(
            project_path=str(demo_project_copy),
            scene_path="e2e_tilemap_read.tscn",
            root_node_type="Node2D",
        )
        node_add(
            project_path=str(demo_project_copy),
            scene_path="e2e_tilemap_read.tscn",
            node_type="TileMap",
            node_name="TileMap",
            parent_node_path="root",
        )
        return Path("e2e_tilemap_read.tscn")

    def test_tilemap_get_cell_empty(
        self,
        demo_project_copy: Path,
        tilemap_scene: Path,
    ) -> None:
        """Test getting an empty tilemap cell."""
        result = tilemap_get_cell(
            project_path=str(demo_project_copy),
            scene_path=str(tilemap_scene),
            node_path="root/TileMap",
            x=0,
            y=0,
        )

        data = json.loads(result)
        assert "x" in data
        assert "y" in data
        assert data["x"] == 0
        assert data["y"] == 0

    def test_tilemap_get_cell_after_set(
        self,
        demo_project_copy: Path,
        tilemap_scene: Path,
    ) -> None:
        """Test getting a tilemap cell after setting it."""
        # Set a cell
        tilemap_set_cell(
            project_path=str(demo_project_copy),
            scene_path=str(tilemap_scene),
            node_path="root/TileMap",
            x=5,
            y=3,
            source_id=0,
            atlas_x=1,
            atlas_y=2,
        )

        # Read it back
        result = tilemap_get_cell(
            project_path=str(demo_project_copy),
            scene_path=str(tilemap_scene),
            node_path="root/TileMap",
            x=5,
            y=3,
        )

        data = json.loads(result)
        assert data["x"] == 5
        assert data["y"] == 3
