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

public class LevelGenerator : MonoBehaviour
{
    [SerializeField]private GameObject LevelPrefab;
    [SerializeField]private GameObject TopPrefab;
    private List<GameObject> spawnedLevels = new List<GameObject>();
    [SerializeField]private float spaceBetween = 1;
    [SerializeField]private float verticalSpace = 2;
    [SerializeField]private float maxLow;
    [SerializeField]private float maxHigh;

    [SerializeField]private List<float> Levels;
    void Start()
    {
        GenerateAhead(150);
    }


    public void GenerateAhead(int count = 10){
        if(Levels.Count <= 0){
            Levels = new List<float>();
        }
        Levels.Add(1);
        int startIndex= Levels.Count;
        int lastIndex = Levels.Count +count;
        for(int i =startIndex; i < lastIndex; i++){
            float LastLevel = Levels[i-1];
            float NewLevel = LastLevel + Random.Range(-maxLow, maxHigh);

            Levels.Add(NewLevel);
            
            float posX = i * spaceBetween;
            float posY = Levels[i];

            GameObject newLevelObj = Instantiate(LevelPrefab, new Vector3(posX,posY,0), Quaternion.identity);
            spawnedLevels.Add(newLevelObj);
            newLevelObj.transform.localScale = new Vector3(newLevelObj.transform.localScale.x, Mathf.Abs(LastLevel - NewLevel),1);
            Color color = (LastLevel < NewLevel) ? Color.green : Color.red;
            Color color2 = (LastLevel < NewLevel) ? new Color(0,0.29f,0) : new Color(0.29f,0,0);

            
            newLevelObj.GetComponent<SpriteRenderer>().color = color;
            

            GameObject topObj = Instantiate(TopPrefab, new Vector3(posX, posY + verticalSpace,0), Quaternion.identity);
            topObj.transform.parent = newLevelObj.transform;

            topObj.GetComponentInChildren<SpriteRenderer>().color =color2;
        }
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
