using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Godot;
using Godot.Collections;
using static ld48.Extensions;
using static ld48.Random;

// [Tool]
public class Cave : StaticBody2D {
    private Curve2D path = new Curve2D();
    private List<Vector2> centers = new List<Vector2>();
    private Vector2[] pathPoints;

    private PackedScene rockScene;
    
    public override void _Ready() {
        base._Ready();
        
        rockScene = ResourceLoader.Load<PackedScene>($"res://scene/RockA.tscn");

        var leftPoints = new List<Vector2>();
        var rightPoints = new List<Vector2>();

        var pos = V2(0f, 0f);

        var direction = V2(0f, 10f);
        var width = 300f;
        
        for (int i = 0; i < 200; i++) {
            centers.Add(pos);
            path.AddPoint(pos, Vector2.Zero, Vector2.Zero);//direction.Normalized().Rotated(90f) * 20f, direction.Normalized().Rotated(-90f) * 20f);

            pos += direction*10f;
            direction += V2(
                RandFloat((-(direction.x + 25f)).Clamp(-30f, 0f), (-(direction.x - 25f)).Clamp(0f, 30f)),
                RandFloat((-(direction.y - 10f)).Clamp(-25f, 0f), (-(direction.y - 20f)).Clamp(0f, 25f))
            );
        }

        pathPoints = path.Tessellate(2, 16f);

        var minX = 100f;
        var maxX = -100f;
        // var leftCount = RandInt(5, 13);
        // var rightCount = RandInt(5, 13);
        var depth = 0;

        var rocks = new List<Rock>();
        foreach (var point in pathPoints) {
            // leftCount -= 1;
            // rightCount -= 1;
            depth += 1;
            
            var left = point - V2(width / 2f, 0f);
            var right = point + V2(width / 2f, 0f);
            leftPoints.Add(left);
            rightPoints.Add(right);
            minX = Mathf.Min(left.x, minX);
            maxX = Mathf.Max(right.x, maxX);
            width += RandFloat((-(width - 300f)).Clamp(-80f, 0f), (-(width - 800f)).Clamp(0f, 80f));

            if (Probability(0.3f) && depth > 2) {
                // rocks.Add(MakeRock(left));
                // leftCount = RandInt(3, 13);
            }
            if (Probability(0.3f) && depth > 2) {
                // rocks.Add(MakeRock(right));;
                // rightCount = RandInt(3, 13);
            }
        }

        var maxY = pathPoints.Last().y;
        leftPoints.Add(V2(minX - 2000f, maxY));
        leftPoints.Add(V2(minX - 2000f, 0f));
        rightPoints.Add(V2(maxX + 2000f, maxY));
        rightPoints.Add(V2(maxX + 2000f, 0f));

        var leftData = leftPoints.ToArray();
        GetNode<CollisionPolygon2D>("LeftWall").Polygon = leftData;
        GetNode<Polygon2D>("LeftDraw").Polygon = leftData;
        var rightData = rightPoints.ToArray();
        GetNode<CollisionPolygon2D>("RightWall").Polygon = rightData;
        GetNode<Polygon2D>("RightDraw").Polygon = rightData;
    }

    // private Rock MakeRock(Vector2 v) {
        // var rock = rockScene.Instance<Rock>();
        // rock.rockScale = V2(RandFloat(0.5f, 1f)*RandFloat(0.5f, 1f), RandFloat(0.5f, 1f)*RandFloat(0.5f, 1f));
        // rock.Position = v + V2(RandFloat(-50f, 0f), RandFloat(-20f, 20f)) / (rock.rockScale * 5f);
        // rock.Rotation = RandFloat(0f, Mathf.Pi * 2f);
        
        // AddChild(rock);
        // return rock;
    // }
}

