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

public class LevelGenerator : MonoBehaviour
{
    public static LevelGenerator instance { get; private set; }
    public GameObject spriteShapeControllerPrefab;
    public int LevelCount = 1000;
    public float groundLevel = -5.5f;
    public float maxHeight = 5;
    public Vector2 minDiff, maxDiff;
    public Vector3[] points {get; private set;}

    public GameObject holePrefab;
    public GameObject flagPrefab;
    public GameObject treePrefab;

    public static List<GameObject> holes;


    public int GoalDistance= 10;
    void Awake()
    {
        instance=this;
        
        holes = new List<GameObject>();

        GenerateBlock(lastOffset);
        GenerateBlock(lastOffset);


    }

    float lastOffset=0;
    void GenerateBlock(float offset){
        SpriteShapeController spriteShapeController = Instantiate(spriteShapeControllerPrefab, new Vector3(offset,0),Quaternion.identity).GetComponent<SpriteShapeController>();
        spriteShapeController.spline.Clear();
        points = new Vector3[LevelCount+1];
        for(int i=0; i < LevelCount; i++){
            if(i ==0){
                points[i] = new Vector3(0,groundLevel);
                spriteShapeController.spline.InsertPointAt(i, points[i]);
                continue;
            }

            Vector3 addition = new Vector3(Random.Range(minDiff.x, maxDiff.x), Random.Range(minDiff.y, maxDiff.y));
            float newX = points[i-1].x + addition.x;
            float newY = points[i-1].y + addition.y;
            while(newY < groundLevel+2){
                newY += Mathf.Abs(addition.y);
            }
            while(newY > maxHeight){
                newY -= Mathf.Abs(addition.y);
            }
            points[i] = new Vector3(newX, newY);
            // spriteShapeController.spline.InsertPointAt(i, points[i]);
            InsertNewPoint(spriteShapeController, points[i]);

            
            if(Random.Range(0f,1f) > 0.5f){
                Vector2 newTreePosition = points[i];

                Vector3 diff = points[i]-points[i-1];
                Vector2 newTreePosition2 = points[i-1] + (diff/2f) + new Vector3(lastOffset,0);
                Debug.Log($"{points[i-1]} + {points[i]} - {points[i-1]} / 0.5f | diff = {diff}");
                GameObject newTree = Instantiate(treePrefab, newTreePosition2, Quaternion.identity);
                newTree.transform.localScale = Vector3.one * Random.Range(0.75f, 1.1f);
            }
            


            if(i % GoalDistance == 0){
                InsertNewPoint(spriteShapeController, points[i] + new Vector3(0, -1f));
                InsertNewPoint(spriteShapeController, points[i] + new Vector3(1f, -1f));
                InsertNewPoint(spriteShapeController, points[i] + new Vector3(1f, 0f));

                Vector3 newHolePos = points[i]  + new Vector3(lastOffset,0);

                Instantiate(flagPrefab,newHolePos, Quaternion.identity);
                holes.Add(Instantiate(holePrefab,newHolePos, Quaternion.identity));
            }

        }

        points[LevelCount] = new Vector3(points[LevelCount-1].x+maxDiff.x, groundLevel);
        lastOffset = points[LevelCount].x - maxDiff.x;
        InsertNewPoint(spriteShapeController, points[LevelCount]);
    }


    void InsertNewPoint(SpriteShapeController spriteShapeController,Vector3 point){
        int index= spriteShapeController.spline.GetPointCount();
        spriteShapeController.spline.InsertPointAt(index, point);
        spriteShapeController.spline.SetLeftTangent(index, Vector3.zero);
        spriteShapeController.spline.SetTangentMode(index, ShapeTangentMode.Broken);
    }


    void Update()
    {
        
    }
}
