using UnityEngine;

/// <summary>
/// Clean MapMaker2 controller - handles only the new system
/// </summary>
public class MapMaker2Controller : MonoBehaviour
{
    [Header("Map References")]
    public MapMaker2 mapMaker;
    public SimpleTeamPlacement teamPlacement;

    [Header("Debug")]
    public bool showDebugInfo = true;

    void Start()
    {
        SetupMapMaker2System();
    }

    private void SetupMapMaker2System()
    {
        // Find MapMaker2 if not assigned
        if (mapMaker == null)
        {
            mapMaker = FindFirstObjectByType<MapMaker2>();
        }

        // Find SimpleTeamPlacement if not assigned
        if (teamPlacement == null)
        {
            teamPlacement = FindFirstObjectByType<SimpleTeamPlacement>();
        }

        // Create SimpleTeamPlacement if it doesn't exist
        if (teamPlacement == null)
        {
            Debug.Log("Creating SimpleTeamPlacement...");
            GameObject teamPlacementObj = new GameObject("SimpleTeamPlacement");
            teamPlacement = teamPlacementObj.AddComponent<SimpleTeamPlacement>();

            // Configure the team placement
            teamPlacement.markerSize = 1.5f;
            teamPlacement.markerHeight = 0.5f;
            teamPlacement.markerColor = Color.green;
            teamPlacement.enableBlinking = true;
        }

        if (showDebugInfo)
        {
            Debug.Log("=== MapMaker2 System Status ===");
            Debug.Log($"MapMaker2: {(mapMaker != null ? "✅ Found" : "❌ Missing")}");
            Debug.Log($"SimpleTeamPlacement: {(teamPlacement != null ? "✅ Found" : "❌ Missing")}");

            if (mapMaker != null)
            {
                Debug.Log($"Map Size: {mapMaker.initialMapSize}x{mapMaker.initialMapSize}");
                Debug.Log($"Seed: {mapMaker.seed}");
                Debug.Log($"MapVisualizer: {(mapMaker.mapVisualizer != null ? "✅ Assigned" : "❌ Missing")}");
            }
        }
    }

    [ContextMenu("Regenerate Map")]
    public void RegenerateMap()
    {
        if (mapMaker != null)
        {
            // Change seed for variety
            mapMaker.seed = Random.Range(1000, 9999);
            mapMaker.RegenerateMap();

            if (showDebugInfo)
            {
                Debug.Log($"🎲 Map regenerated with new seed: {mapMaker.seed}");
            }
        }
        else
        {
            Debug.LogError("❌ MapMaker2 not found!");
        }
    }

    [ContextMenu("Debug River Paths Info")]
    public void DebugRiverPathsInfo()
    {
        if (mapMaker?.GetMapData() == null)
        {
            Debug.LogError("❌ No map data available");
            return;
        }

        var mapData = mapMaker.GetMapData();
        int riverPathsCount = 0;
        int forestCount = 0;
        int riverCount = 0;

        // Count rivers that have forests around them (creating paths)
        for (int x = 0; x < mapData.Width; x++)
        {
            for (int y = 0; y < mapData.Height; y++)
            {
                var tile = mapData.GetTile(x, y);
                switch (tile.terrainType)
                {
                    case TerrainType.Forest:
                        forestCount++;
                        break;
                    case TerrainType.River:
                        riverCount++;
                        // Check if this river has forests nearby (creating a path effect)
                        bool hasNearbyForest = false;
                        for (int dx = -1; dx <= 1; dx++)
                        {
                            for (int dy = -1; dy <= 1; dy++)
                            {
                                if (dx == 0 && dy == 0) continue;
                                int checkX = x + dx, checkY = y + dy;
                                if (mapData.IsValidPosition(checkX, checkY))
                                {
                                    if (mapData.GetTile(checkX, checkY).terrainType == TerrainType.Forest)
                                    {
                                        hasNearbyForest = true;
                                        break;
                                    }
                                }
                            }
                            if (hasNearbyForest) break;
                        }
                        if (hasNearbyForest) riverPathsCount++;
                        break;
                }
            }
        }

        Debug.Log("=== RIVER PATHS ANALYSIS ===");
        Debug.Log($"🌊 Total River tiles: {riverCount}");
        Debug.Log($"🌲 Forest tiles: {forestCount}");
        Debug.Log($"🛤️ River paths through forests: {riverPathsCount}");
        Debug.Log($"💡 Rivers create clear blue paths through green forests");

        if (riverPathsCount > 0)
        {
            Debug.Log($"✅ SUCCESS: Found {riverPathsCount} river paths through forests! Look for blue waterways cutting through green forest areas.");
        }
        else
        {
            Debug.Log("❓ No river paths found. Try regenerating the map with a different seed.");
        }
    }
    void Update()
    {
        // Debug keys
        if (Input.GetKeyDown(KeyCode.F5))
        {
            RegenerateMap();
        }

        if (Input.GetKeyDown(KeyCode.F6))
        {
            DebugRiverPathsInfo();
        }
    }
}
