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

public class LevelGenerator : MonoBehaviour
{

    public GameObject spriteShapecontrollerPrefab;
    public int levelCount = 200;

    public float bot_groundLevel,bot_topLevel, top_lowLevel,top_ceilingLevel;
    public Vector2 minDiff, maxDiff;
    public float tangentSmoothness = 3;
    void Start()
    {
        GenerateBlock();
        GenerateBlock();
        GenerateBlock();
        GenerateBlock();
        GenerateBlock();

    }

    // Update is called once per frame
    void Update()
    {
        
    }
    Vector3[] bot_points;
    float curBlockOffset=-20;
    void GenerateBlock(){
        float offset = curBlockOffset;
        SpriteShapeController bot_spriteShapeController = Instantiate(spriteShapecontrollerPrefab, new Vector3(offset,0),Quaternion.identity).GetComponent<SpriteShapeController>();
        bot_spriteShapeController.spline.Clear();
        Vector3[] bot_points = new Vector3[levelCount +1];
        InsertNewPoint(bot_spriteShapeController, new Vector3(0,bot_groundLevel *2));
        for(int i=0; i < levelCount; i++){
            if(i==0){
                bot_points[i] = new Vector3(0,bot_groundLevel);

                bot_points[i] = new Vector3(0,bot_groundLevel);
                // bot_spriteShapeController.spline.InsertPointAt(i,bot_points[i]);
                InsertNewPoint(bot_spriteShapeController, bot_points[i]);
                continue;
            }

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

        bot_points[levelCount] = new Vector3(bot_points[levelCount-1].x+maxDiff.x, bot_groundLevel * 2);
        curBlockOffset += bot_points[levelCount].x - maxDiff.x;
        InsertNewPoint(bot_spriteShapeController, bot_points[levelCount]);
    }

    void InsertNewPoint(SpriteShapeController spriteShapeController,Vector3 point){
        int index= spriteShapeController.spline.GetPointCount();
        spriteShapeController.spline.InsertPointAt(index, point);
        spriteShapeController.spline.SetTangentMode(index, ShapeTangentMode.Continuous);
        spriteShapeController.spline.SetLeftTangent(index, new Vector3(-tangentSmoothness,0));
        spriteShapeController.spline.SetRightTangent(index,new Vector3(tangentSmoothness,0));
    }
}
