"""Tests for timer, canvas layer, and particle operations."""

from pathlib import Path

import pytest

from godot_mcp.server import (
    timer_add,
    timer_configure,
    canvas_layer_add,
    canvas_layer_set_order,
    particle_add_system,
    particle_set_properties,
    scene_create,
)


class TestTimer:
    """Test cases for timer operations."""

    def test_add_timer(self, temp_godot_project: Path) -> None:
        """Test adding a Timer node."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        result = timer_add(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            timer_name="GameTimer",
            wait_time=2.0,
            one_shot=True,
        )

        assert "successfully" in result.lower() or "added" in result.lower()
        assert "GameTimer" in result

    def test_timer_configure(self, temp_godot_project: Path) -> None:
        """Test configuring timer properties."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node",
        )

        # Add timer first
        timer_add(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            timer_name="Timer",
        )

        # Configure it
        result = timer_configure(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            timer_path="Timer",
            wait_time=5.0,
            autostart=True,
        )

        assert "successfully" in result.lower() or "configured" in result.lower()


class TestCanvasLayer:
    """Test cases for canvas layer operations."""

    def test_add_canvas_layer(self, temp_godot_project: Path) -> None:
        """Test adding a CanvasLayer node."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        result = canvas_layer_add(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            layer_name="UILayer",
            layer_order=10,
        )

        assert "successfully" in result.lower() or "added" in result.lower()
        assert "UILayer" in result

    def test_canvas_layer_set_order(self, temp_godot_project: Path) -> None:
        """Test setting canvas layer order."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        # Add layer first
        canvas_layer_add(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            layer_name="Layer",
        )

        # Set order
        result = canvas_layer_set_order(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            layer_path="Layer",
            layer_order=5,
        )

        assert "successfully" in result.lower() or "set" in result.lower()


class TestParticle:
    """Test cases for particle system operations."""

    def test_add_cpu_particles_2d(self, temp_godot_project: Path) -> None:
        """Test adding CPUParticles2D."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        result = particle_add_system(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            particle_name="Particles",
            particle_type="CPUParticles2D",
            amount=16,
        )

        assert "successfully" in result.lower() or "added" in result.lower()
        assert "Particles" in result

    def test_add_gpu_particles_2d(self, temp_godot_project: Path) -> None:
        """Test adding GPUParticles2D."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        result = particle_add_system(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            particle_name="GPUParticles",
            particle_type="GPUParticles2D",
        )

        assert "successfully" in result.lower() or "added" in result.lower()

    def test_particle_invalid_type(self, temp_godot_project: Path) -> None:
        """Test error handling for invalid particle type."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
        )

        result = particle_add_system(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            particle_name="Invalid",
            particle_type="InvalidParticles",
        )

        assert "Invalid particle type" in result

    def test_particle_set_properties(self, temp_godot_project: Path) -> None:
        """Test setting particle properties."""
        scene_path = "test_scene.tscn"
        scene_create(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            root_node_type="Node2D",
        )

        # Add particles first
        particle_add_system(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            parent_node_path=".",
            particle_name="Particles",
            particle_type="CPUParticles2D",
        )

        # Set properties
        result = particle_set_properties(
            project_path=str(temp_godot_project),
            scene_path=scene_path,
            particle_path="Particles",
            lifetime=2.0,
            speed_scale=1.5,
            explosiveness=0.5,
        )

        assert "successfully" in result.lower() or "set" in result.lower()
