using System.Collections.Generic;
using UnityEngine;

public class PlanetGenerator : MonoBehaviour
{
    public static PlanetGenerator Instance;
    public List<GravityAttractor> Planets;
    public GravityAttractor PlanetTemplate;

    //private Vector3 positionOffset = Vector3.zero;
    private const int LIMIT = 12;
    public void Awake()
    {
        Instance = this;
    }

    public void Start()
    {
        for (int i = -LIMIT; i < LIMIT; i++)
        {
            for (int j = -LIMIT; j < LIMIT; j++)
            {
                var xCord = ((float)i) / LIMIT;
                var yCord = ((float)j) / LIMIT;
                var noise = Mathf.PerlinNoise(xCord, yCord);
                var shouldSpawn = Random.Range(0, 1) < noise;

                if (shouldSpawn)
                {
                    var type = (GravityAttractor.PlanetType)Random.Range(0, 4);
                    if (Mathf.Abs(i) == LIMIT || Mathf.Abs(j) == LIMIT)
                    {
                        type = GravityAttractor.PlanetType.Lava;
                    }

                    var randomX = Random.Range(20f, 40f);
                    var randomY = Random.Range(20f, 40f);
                    var randomX2 = Random.Range(20f, 40f);
                    var randomY2 = Random.Range(20f, 40f);
                    var planet = Instantiate(PlanetTemplate, new Vector3(0, i * 3 * randomX + randomX2, j * 3 * randomY + randomY2), PlanetTemplate.transform.rotation);
                    planet.SetType(type);
                    Planets.Add(planet);
                }
            }
        }
    }
}
