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 spritePrefab;
    public int startPoints = 500;
    public int trendChangeThreshold = 10;
    public float maxHeightChange = 2;
    public float xMultiplier = 1;
    public Vector3[] points {get; private set;}

    bool goingUp = false;
    int lastIndex =0;
    SpriteShapeController curSpriteController;
    void Awake()
    {
        instance = this;
        points = new Vector3[startPoints];
        for(int i=0; i < startPoints; i++){
            if(i < 1){
                points[i] = new Vector3(i, Random.Range(-1f,1f));
                GameObject initSprite = Instantiate(spritePrefab, Vector3.zero,Quaternion.identity);
                SpriteShapeController initSpriteController= initSprite.GetComponent<SpriteShapeController>();
                initSpriteController.spline.Clear();
                for(int x= 0; x < 10; x++){
                    initSpriteController.spline.InsertPointAt(x, points[i] - new Vector3(x*5 + 0.5f,0));
                }
                NewTrend(i);

            }else{
                float log = Random.Range(0, maxHeightChange);
                points[i] = new Vector3(i*xMultiplier, points[i-1].y + Random.Range((goingUp)? 0: -maxHeightChange*log,(goingUp) ? maxHeightChange*log : 0));
            }

            if(i % trendChangeThreshold == 0){
                bool newGoingUp = Random.Range(0,100) > 50 ? true : false;
                if(newGoingUp != goingUp){
                    //Gotta change trend
                    goingUp = newGoingUp;
                    NewTrend(i);
                }
            }

            curSpriteController.spline.InsertPointAt(i-lastIndex, points[i]);
            curSpriteController.spline.SetTangentMode(i-lastIndex, ShapeTangentMode.Continuous);
            curSpriteController.spline.SetLeftTangent(i-lastIndex,new Vector3(0.2f,0));
            curSpriteController.spline.SetRightTangent(i-lastIndex, new Vector3(0.2f,0));

            // if(i > 1){
            //     spriteShape.spline.SetLeftTangent(i, points[i-1].MidPoint(points[i]));
            // }
        }
    }

    void NewTrend(int i){

        if(curSpriteController != null){

            EdgeCollider2D edgeCollider = curSpriteController.GetComponent<EdgeCollider2D>();
            edgeCollider.points = new Vector2[curSpriteController.spline.GetPointCount()];
            for(int x=0; x < curSpriteController.spline.GetPointCount(); x++){
                edgeCollider.points[x] = curSpriteController.spline.GetPosition(x);
            }
            curSpriteController.spline.InsertPointAt(i-lastIndex, points[i] - new Vector3(0.5f,0));

        }
        lastIndex = i;

        GameObject newSpriteController=  Instantiate(spritePrefab, Vector3.zero, Quaternion.identity);
        curSpriteController = newSpriteController.GetComponent<SpriteShapeController>();
        curSpriteController.spline.Clear();
        curSpriteController.spriteShapeRenderer.color = goingUp ? Color.green : Color.red;
    }

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