using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System;

namespace LunaLightXMG
{
    public class Platform
    {
        public Vector2 Position { get; private set; }
        public BoundingBox BoundingBox { get; private set; }

        public Platform(Vector2 position, int width, int height)
        {
            Position = position;
            BoundingBox = new BoundingBox(new Rectangle((int)position.X, (int)position.Y, width, height));
        }

        public void Draw(SpriteBatch spriteBatch, Texture2D texture)
        {
            spriteBatch.Draw(texture, Position, Color.White);
        }
    }

    public class PlatformGenerator
    {
        private static Random random = new Random();

        public static Platform[] CreatePlatforms(int numberOfPlatforms)
        {
            // Bottom wall (static)
            List<Platform> bottomPlatforms = new List<Platform>
            {
                new Platform(new Vector2(0, 164), 16, 16),
                new Platform(new Vector2(16, 164), 16, 16),
                new Platform(new Vector2(32, 164), 16, 16),
                new Platform(new Vector2(48, 164), 16, 16),
                new Platform(new Vector2(64, 164), 16, 16),
                new Platform(new Vector2(80, 164), 16, 16),
                new Platform(new Vector2(96, 164), 16, 16),
                new Platform(new Vector2(112, 164), 16, 16),
                new Platform(new Vector2(128, 164), 16, 16),
                new Platform(new Vector2(144, 164), 16, 16),
                new Platform(new Vector2(160, 164), 16, 16),
                new Platform(new Vector2(176, 164), 16, 16),
                new Platform(new Vector2(192, 164), 16, 16),
                new Platform(new Vector2(208, 164), 16, 16),
                new Platform(new Vector2(224, 164), 16, 16),
                new Platform(new Vector2(240, 164), 16, 16),
                new Platform(new Vector2(256, 164), 16, 16),
                new Platform(new Vector2(272, 164), 16, 16),
                new Platform(new Vector2(288, 164), 16, 16),
                new Platform(new Vector2(304, 164), 16, 16)
            };
            
            // Left side wall (static)
            List<Platform> leftSidePlatforms = new List<Platform>
            {
                new Platform(new Vector2(0, -28), 16, 16),
                new Platform(new Vector2(0, -12), 16, 16),
                new Platform(new Vector2(0, 4), 16, 16),
                new Platform(new Vector2(0, 20), 16, 16),
                new Platform(new Vector2(0, 36), 16, 16),
                new Platform(new Vector2(0, 52), 16, 16),
                new Platform(new Vector2(0, 68), 16, 16),
                new Platform(new Vector2(0, 84), 16, 16),
                new Platform(new Vector2(0, 100), 16, 16),
                new Platform(new Vector2(0, 116), 16, 16),
                new Platform(new Vector2(0, 132), 16, 16),
                new Platform(new Vector2(0, 148), 16, 16)
            };
            
            // Right side wall (static)
            List<Platform> rightSidePlatforms = new List<Platform>
            {
                new Platform(new Vector2(304, -28), 16, 16),
                new Platform(new Vector2(304, -12), 16, 16),
                new Platform(new Vector2(304, 4), 16, 16),
                new Platform(new Vector2(304, 20), 16, 16),
                new Platform(new Vector2(304, 36), 16, 16),
                new Platform(new Vector2(304, 52), 16, 16),
                new Platform(new Vector2(304, 68), 16, 16),
                new Platform(new Vector2(304, 84), 16, 16),
                new Platform(new Vector2(304, 100), 16, 16),
                new Platform(new Vector2(304, 116), 16, 16),
                new Platform(new Vector2(304, 132), 16, 16),
                new Platform(new Vector2(304, 148), 16, 16)
            };
            // Generate random platforms
            List<Platform> randomizedPlatforms = CreateRandomPlatforms(numberOfPlatforms);
            // Combine all platforms
            List<Platform> allPlatforms = new List<Platform>();
            allPlatforms.AddRange(randomizedPlatforms);
            allPlatforms.AddRange(bottomPlatforms);
            allPlatforms.AddRange(leftSidePlatforms);
            allPlatforms.AddRange(rightSidePlatforms);

            return allPlatforms.ToArray();
        }

        public static List<Platform> CreateRandomPlatforms(int numberOfPlatforms)
        {
            List<Platform> platforms = new List<Platform>();
            // Static platforms
            platforms.Add(new Platform(new Vector2(16, 132), 16, 16));
            platforms.Add(new Platform(new Vector2(128, 132), 16, 16));
            platforms.Add(new Platform(new Vector2(176, 132), 16, 16));
            platforms.Add(new Platform(new Vector2(288, 132), 16, 16));

            for (int i = 0; i < numberOfPlatforms; i++)
            {
                bool validPosition = false;
                int x = 0;
                int y = 0;

                // Create platforms at random x,y coordinate until a valid position is found.
                while (!validPosition)
                {
                    x = random.Next(1, 18) * 16; // Random x multiple of 16 between 16 and 288
                    y = random.Next(3, 9) * 16;  // Random y multiple of 16 between 48 and 132

                    // Check if position is already in the platforms list
                    validPosition = !platforms.Exists(p => p.Position.X == x && p.Position.Y == y);
                    // If it's already taken, look at the space to the right.
                    if (!validPosition)
                    {
                        x += 16;
                        // If all spaces are taken in that row, move to the above row.
                        if (x > 288)
                        {
                            x = 16;
                            y += 16;
                        }
                    }
                }
                // Add valid platform to the platforms list.
                platforms.Add(new Platform(new Vector2(x, y + 4), 16, 16));
                // If we aren't at the end of the row, add two more platforms.
                if (x + 16 <= 288)
                {
                    for (int j = 1; j < 3; j++)
                    {
                        int nextX = x + (j * 16);
                        if (!platforms.Exists(p => p.Position.X == nextX && p.Position.Y == y + 4)) {
                            platforms.Add(new Platform(new Vector2(nextX, y + 4), 16, 16));
                        }
                    }
                }
            }
            return platforms;
        }
    }
}

