using System;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
using Random = UnityEngine.Random;

public class WorldGenerator : MonoBehaviour{
    public Vector2 worldSize;

    public int platformCount;
    
    [MinMaxSlider(1f, 100f)]
    public Vector2 platformHeightRange;
    [MinMaxSlider(25f, 300f)]
    public Vector2 platformWidthRange;
    
    [Header("Mega Platforms")]
    public int megaPlatformCount;
    [MinMaxSlider(30f, 100f)]
    public Vector2 megaPlatformtHeightRange = new Vector2(20, 20);
    [MinMaxSlider(250f, 500f)]
    public Vector2 megaPlatformWidthRange = new Vector2(50, 50);

    private List<GameObject> allPlatforms = new List<GameObject>();
    private List<GameObject> megaPlatforms = new List<GameObject>();
    
    void Start(){
        GenerateWorld();
    }

    [Button]
    public void RegenerateWorld(){
        ClearWorld();
        GenerateWorld();
    }

    void ClearWorld(){
        allPlatforms.ForEach(Destroy);
    }
    
    void GenerateWorld(){
        GenerateMegaPlatforms();
        GenerateStandardPlatforms();
    }

    void GenerateMegaPlatforms(){
        for (int i = 0; i < megaPlatformCount; i++) {
            Vector3 newPlatformSize = new Vector3(
                Random.Range(megaPlatformWidthRange.x, megaPlatformWidthRange.y),
                Random.Range(megaPlatformtHeightRange.x, megaPlatformtHeightRange.y),
                Random.Range(megaPlatformWidthRange.x, megaPlatformWidthRange.y)
            );

            var newPlatform = ShapeGenerator.GenerateCube(PivotLocation.Center, newPlatformSize);
            
            newPlatform.SetMaterial(newPlatform.faces, BuiltinMaterials.defaultMaterial);
            newPlatform.gameObject.AddComponent<BoxCollider>();

            newPlatform.transform.position = new Vector3(
                Random.Range(-worldSize.x, worldSize.x),
                newPlatformSize.y / 2f,
                Random.Range(-worldSize.y, worldSize.y)
            );

            newPlatform.gameObject.name = "Megaplatform";
            
            megaPlatforms.Add(newPlatform.gameObject);
            allPlatforms.Add(newPlatform.gameObject);
        }
    }

    void GenerateStandardPlatforms(){
        for (int i = 0; i < platformCount; i++) {
            Vector3 newPlatformSize = new Vector3(
                Random.Range(platformWidthRange.x, platformWidthRange.y),
                Random.Range(platformHeightRange.x, platformHeightRange.y),
                Random.Range(platformWidthRange.x, platformWidthRange.y)
            );

            var newPlatform = ShapeGenerator.GenerateCube(PivotLocation.Center, newPlatformSize);
            newPlatform.gameObject.layer = 2;

            newPlatform.SetMaterial(newPlatform.faces, BuiltinMaterials.defaultMaterial);
            
            Vector3 newPlatformPosXZ = new Vector3(
                Random.Range(-worldSize.x, worldSize.x),
                350f,
                Random.Range(-worldSize.y, worldSize.y)
            );

            
            Physics.Raycast(newPlatformPosXZ, Vector3.down, out RaycastHit hit, Mathf.Infinity, LayerMask.GetMask("Default"));
            
            Debug.Log(hit.collider.name);
            
            newPlatform.transform.position = new Vector3(
                newPlatformPosXZ.x,
                hit.point.y + newPlatformSize.y /2f,
                newPlatformPosXZ.z
            );
            
            newPlatform.gameObject.name = "Platform";
            
            // newPlatform.gameObject.AddComponent<BoxCollider>();
            allPlatforms.Add(newPlatform.gameObject);
        }
    }

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