using UnityEngine;

/// <summary>
/// Simple terrain setup for the dinosaur world.
/// Creates a basic terrain with some variation.
/// </summary>
public class TerrainGenerator : MonoBehaviour
{
    [Header("Terrain Settings")]
    [Tooltip("Width of the terrain in world units")]
    public int terrainWidth = 512;
    
    [Tooltip("Length of the terrain in world units")]
    public int terrainLength = 512;
    
    [Tooltip("Maximum height of terrain")]
    public float maxHeight = 20f;
    
    [Tooltip("Terrain detail level")]
    public float detailScale = 50f;

    [Header("Features")]
    [Tooltip("Add some hills")]
    public bool addHills = true;
    
    [Tooltip("Add a flat area in center")]
    public bool addCenterFlat = true;
    
    [Tooltip("Radius of center flat area")]
    public float centerFlatRadius = 30f;

    void Start()
    {
        CreateTerrain();
    }

    void CreateTerrain()
    {
        // Create terrain if it doesn't exist
        Terrain terrain = FindAnyObjectByType<Terrain>();
        
        if (terrain == null)
        {
            terrain = new GameObject("Terrain").AddComponent<Terrain>();
            terrain.gameObject.transform.position = Vector3.zero;
        }

        // Setup terrain data
        TerrainData terrainData = new TerrainData();
        terrainData.heightmapResolution = 513;
        terrainData.size = new Vector3(terrainWidth, maxHeight, terrainLength);

        // Generate heights
        float[,] heights = new float[terrainData.heightmapResolution, terrainData.heightmapResolution];
        
        for (int y = 0; y < terrainData.heightmapResolution; y++)
        {
            for (int x = 0; x < terrainData.heightmapResolution; x++)
            {
                // Normalized coordinates
                float nx = (float)x / terrainData.heightmapResolution;
                float ny = (float)y / terrainData.heightmapResolution;

                // Distance from center
                float distFromCenter = Mathf.Sqrt(
                    Mathf.Pow(nx - 0.5f, 2) + Mathf.Pow(ny - 0.5f, 2)
                );

                float height = 0f;

                if (addCenterFlat && distFromCenter < centerFlatRadius / terrainWidth)
                {
                    // Flat center area
                    height = 0.05f;
                }
                else
                {
                    // Hills and variation
                    height = Mathf.PerlinNoise(nx * detailScale, ny * detailScale) * 0.6f;
                    height += Mathf.PerlinNoise(nx * detailScale * 2.5f, ny * detailScale * 2.5f) * 0.3f;
                    height += Mathf.PerlinNoise(nx * detailScale * 0.5f, ny * detailScale * 0.5f) * 0.1f;
                }

                heights[x, y] = height;
            }
        }

        terrainData.SetHeights(0, 0, heights);
        terrain.terrainData = terrainData;

        Debug.Log($"🌍 Terrain created - {terrainWidth}x{terrainLength}, Max Height: {maxHeight}");
    }
}