using UnityEngine;

/// <summary>
/// Test script to verify team marker camera functionality.
/// Attach this to any GameObject to test the camera system.
/// </summary>
public class TeamMarkerCameraTest : MonoBehaviour
{
    [Header("Test Controls")]
    [Tooltip("Key to run all camera tests")]
    public KeyCode testAllKey = KeyCode.T;

    [Tooltip("Key to test team marker centering")]
    public KeyCode testCenterKey = KeyCode.Y;

    [Tooltip("Key to test full map view")]
    public KeyCode testFullMapKey = KeyCode.U;

    void Update()
    {
        if (Input.GetKeyDown(testAllKey))
        {
            RunAllTests();
        }

        if (Input.GetKeyDown(testCenterKey))
        {
            TestTeamMarkerCentering();
        }

        if (Input.GetKeyDown(testFullMapKey))
        {
            TestFullMapView();
        }
    }

    public void RunAllTests()
    {
        Debug.Log("🧪 Running Team Marker Camera Tests...");

        // Test 1: Check if team placement exists
        TestTeamPlacementExists();

        // Test 2: Check if camera controllers exist
        TestCameraControllersExist();

        // Test 3: Test team marker centering
        TestTeamMarkerCentering();

        // Test 4: Test full map view
        TestFullMapView();

        Debug.Log("✅ Team Marker Camera Tests Complete!");
    }

    private void TestTeamPlacementExists()
    {
        SimpleTeamPlacement teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();

        if (teamPlacement == null)
        {
            Debug.LogError("❌ SimpleTeamPlacement not found in scene!");
            return;
        }

        Debug.Log("✅ SimpleTeamPlacement found");

        if (!teamPlacement.IsTeamPlaced())
        {
            Debug.LogWarning("⚠️ Team not yet placed, attempting to place...");
            teamPlacement.RandomlyPlaceTeam();
        }
        else
        {
            Debug.Log("✅ Team is placed");
        }

        GameObject teamMarker = teamPlacement.GetTeamMarker();
        if (teamMarker != null)
        {
            Debug.Log($"✅ Team marker found at position: {teamMarker.transform.position}");
        }
        else
        {
            Debug.LogError("❌ Team marker object not found!");
        }
    }

    private void TestCameraControllersExist()
    {
        // Check for enhanced camera controllers
        MapCameraController mapCameraController = FindFirstObjectByType<MapCameraController>();
        MMCameraController mmCameraController = FindFirstObjectByType<MMCameraController>();
        TeamMarkerCameraController teamMarkerController = FindFirstObjectByType<TeamMarkerCameraController>();

        int controllerCount = 0;

        if (mapCameraController != null)
        {
            Debug.Log("✅ MapCameraController found (enhanced with team marker support)");
            controllerCount++;
        }

        if (mmCameraController != null)
        {
            Debug.Log("✅ MMCameraController found (enhanced with team marker support)");
            controllerCount++;
        }

        if (teamMarkerController != null)
        {
            Debug.Log("✅ TeamMarkerCameraController found (universal component)");
            controllerCount++;
        }

        if (controllerCount == 0)
        {
            Debug.LogWarning("⚠️ No enhanced camera controllers found. You may need to add TeamMarkerCameraController to your camera.");
        }
        else
        {
            Debug.Log($"✅ Found {controllerCount} camera controller(s) with team marker support");
        }
    }

    private void TestTeamMarkerCentering()
    {
        Debug.Log("🎯 Testing team marker centering...");

        // Try to find any camera controller that supports team marker centering
        TeamMarkerCameraController teamMarkerController = FindFirstObjectByType<TeamMarkerCameraController>();
        if (teamMarkerController != null)
        {
            teamMarkerController.CenterOnTeamMarker();
            Debug.Log("✅ Called CenterOnTeamMarker() on TeamMarkerCameraController");
            return;
        }

        MapCameraController mapCameraController = FindFirstObjectByType<MapCameraController>();
        if (mapCameraController != null)
        {
            mapCameraController.CenterOnTeamMarker();
            Debug.Log("✅ Called CenterOnTeamMarker() on MapCameraController");
            return;
        }

        MMCameraController mmCameraController = FindFirstObjectByType<MMCameraController>();
        if (mmCameraController != null)
        {
            mmCameraController.CenterOnTeamMarker();
            Debug.Log("✅ Called CenterOnTeamMarker() on MMCameraController");
            return;
        }

        Debug.LogWarning("⚠️ No camera controller with CenterOnTeamMarker() method found!");
    }

    private void TestFullMapView()
    {
        Debug.Log("🗺️ Testing full map view...");

        // Try to find any camera controller that supports full map view
        TeamMarkerCameraController teamMarkerController = FindFirstObjectByType<TeamMarkerCameraController>();
        if (teamMarkerController != null)
        {
            teamMarkerController.ZoomToFullMap();
            Debug.Log("✅ Called ZoomToFullMap() on TeamMarkerCameraController");
            return;
        }

        MapCameraController mapCameraController = FindFirstObjectByType<MapCameraController>();
        if (mapCameraController != null)
        {
            mapCameraController.ZoomToFullMap();
            Debug.Log("✅ Called ZoomToFullMap() on MapCameraController");
            return;
        }

        MMCameraController mmCameraController = FindFirstObjectByType<MMCameraController>();
        if (mmCameraController != null)
        {
            mmCameraController.ZoomToFullMap();
            Debug.Log("✅ Called ZoomToFullMap() on MMCameraController");
            return;
        }

        Debug.LogWarning("⚠️ No camera controller with ZoomToFullMap() method found!");
    }

    void OnEnable()
    {
        Debug.Log($"🧪 Team Marker Camera Test script enabled!\n" +
                 $"- {testAllKey}: Run all tests\n" +
                 $"- {testCenterKey}: Test team marker centering\n" +
                 $"- {testFullMapKey}: Test full map view");
    }
}
