namespace splat;

using Chickensoft.GoDotTest;
using Godot;
using Shouldly;

public class CameraControllerTest : TestClass
{
    public CameraControllerTest(Node testScene) : base(testScene) { }

    [Test]
    public void CameraStartsAtConfiguredOrbitDistance()
    {
        var (rig, camera) = CreateCamera();

        camera.Position.Length().ShouldBe(30f, tolerance: 0.01f);
        camera.Position.Y.ShouldBeGreaterThan(0f);
        rig.Free();
    }

    [Test]
    public void PanMovesRigAndZoomInMovesCameraCloser()
    {
        EnsureCameraActions();
        var (rig, camera) = CreateCamera();
        var initialDistance = camera.Position.Length();

        Input.ActionPress("cam_pan_right");
        rig._Process(0.5d);
        Input.ActionRelease("cam_pan_right");
        rig.Position.Length().ShouldBeGreaterThan(0f);

        Input.ActionPress("cam_zoom_in");
        rig._Process(0.5d);
        Input.ActionRelease("cam_zoom_in");
        camera.Position.Length().ShouldBeLessThan(initialDistance);
        rig.Free();
    }

    [Test]
    public void ZoomRespectsConfiguredLimits()
    {
        EnsureCameraActions();
        var (rig, camera) = CreateCamera();
        rig.MinZoom = 12f;
        rig.MaxZoom = 32f;

        Input.ActionPress("cam_zoom_in");
        rig._Process(100d);
        Input.ActionRelease("cam_zoom_in");
        camera.Position.Length().ShouldBe(12f, tolerance: 0.01f);

        Input.ActionPress("cam_zoom_out");
        rig._Process(100d);
        Input.ActionRelease("cam_zoom_out");
        camera.Position.Length().ShouldBe(32f, tolerance: 0.01f);
        rig.Free();
    }

    private (CameraController Rig, Camera3D Camera) CreateCamera()
    {
        var rig = new CameraController();
        var camera = new Camera3D { Name = "Camera3D" };
        rig.AddChild(camera);
        TestScene.AddChild(rig);
        return (rig, camera);
    }

    private static void EnsureCameraActions()
    {
        var manager = new InputManager();
        manager._Ready();
        manager.Free();
    }
}
