using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnvironmentGenerator : MonoBehaviour
{
    [Tooltip("Width of the whole ground plane")]
    public float width = 1;
    [Tooltip("Height of the whole ground plane")]
    public float height = 1;

    [Tooltip("Amount of planes drawn horizontally in the whole plane area.")]
    [Range(1, 20)]
    public uint amountOfHorizontalElements = 5;
    [Tooltip("Amount of planes drawn vertically in the whole plane area.")]
    [Range(1, 20)]
    public uint amountOfVerticalElements = 5;
    [Tooltip("The material the ground uses")]
    public Material groundMaterial;
    [Tooltip("Only for testing.")]
    public bool testDestroy = false;

    public GameObject[,] CurrentEnvironment { get; private set; }

    private void Start()
    {
        CreateNewEnvironment();
    }

    private void Update()
    {
        TestEnvironmentCreation();
    }

    /// <summary>
    /// Only for testing, can be deleted if not needed anymore
    /// </summary>
    private void TestEnvironmentCreation()
    {
        if (testDestroy)
        {
            UpdateEnvironment();
            testDestroy = !testDestroy;
        }
    }

    /// <summary>
    /// Creates a grid of planes 
    /// </summary>
    public void CreateNewEnvironment()
    {
        CurrentEnvironment = new GameObject[amountOfHorizontalElements, amountOfVerticalElements];

        float widthSingleEleme = width / amountOfHorizontalElements; //width of a single quad
        float heightSingleElem = height / amountOfVerticalElements; //height of a single quad

        // calculate the left-upper-point of the whole environment plane to use it as a startpoint for creating the grid
        Vector3 leftUpperPoint = transform.position + (Vector3.left * (width / 2) + (Vector3.forward * (height / 2)));

        Vector3 currentStartPoint = leftUpperPoint + new Vector3(widthSingleEleme / 2, 0, -heightSingleElem / 2);

        //First draw all horizontal planes than do the next line in the grid
        for (int idxX = 0; idxX < amountOfVerticalElements; idxX++)
        {
            CreateHorizontalPlaneLine(currentStartPoint, widthSingleEleme, heightSingleElem, idxX);
            currentStartPoint += Vector3.back * heightSingleElem;
        }

    }

    // Create a horizontal line of Planes
    private void CreateHorizontalPlaneLine(Vector3 startPoint, float widthPlane = 1, float heightPlane = 1, int idxX = 1)
    {
        Vector3 currentStartPoint = startPoint;

        for (int idxY = 0; idxY < amountOfHorizontalElements; idxY++)
        {
            CreatePlane(widthPlane, heightPlane, idxX, currentStartPoint, idxY);
            currentStartPoint += Vector3.right * widthPlane;
        }
    }

    // Create a single Plane 
    private void CreatePlane(float widthPlane, float heightPlane, int idxX, Vector3 currentStartPoint, int idxY)
    {
        GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Quad);
        plane.transform.localEulerAngles = new Vector3(90, 0, 0);
        plane.transform.position = currentStartPoint;
        plane.transform.localScale = new Vector3(widthPlane, heightPlane, 1);
        plane.transform.SetParent(this.transform);
        plane.GetComponent<Renderer>().material = groundMaterial;

        CurrentEnvironment[idxX, idxY] = plane;
    }

    /// <summary>
    /// Destroy all Planes and set the environment array back to null
    /// </summary>
    public void DeleteCurrentEnvironment()
    {
        foreach (Transform child in transform)
        {
            if (child != this)
            {
                Destroy(child.gameObject);
            }
        }

        CurrentEnvironment = null;
    }

    /// <summary>
    /// Deletes the current environment if there is any and creates a new one
    /// </summary>
    public void UpdateEnvironment()
    {
        DeleteCurrentEnvironment();
        CreateNewEnvironment();
    }
}
