using System.Collections.Generic;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Gameplay.Prototyping
{
    public class GroundSlabModelPrototype : GroundModelPrototype
    {
        private const float Height = 0.5f;
        
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeX = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeZ = 1;

        [GroupDebug, SerializeField, Min(1)] private int x = 1;
        [GroupDebug, SerializeField, Min(1)] private int z = 1;
        
        public override void SetSize(Vector2Int size)
        {
            x = size.x;
            z = size.y;
            SizeFitting();
        }
        
        protected override void DebugResize()
        {
            sizeX = Mathf.Max(1, x);
            sizeZ = Mathf.Max(1, z);
        }
        
        protected override void Generate()
        {
            var vertices = new List<Vector3>();
            var triangles = new List<int>();
            var uvs = new List<Vector2>();
            
            MeshUtils.AddCubeFaces(vertices, triangles, uvs,
                sizeX, 1, sizeZ,
                sizeX * 0.5f, Height * 0.5f, sizeZ * 0.5f,
                true);
            
            PrototypeModel.BuildMesh(new MeshData(
                vertices.ToArray(),
                triangles.ToArray(),
                uvs.ToArray()
            ));
            
            PrototypeModel.BoxCollider.size = new Vector3(sizeX, Height, sizeZ);
            PrototypeModel.BoxCollider.center = Vector3.zero;
            PrototypeModel.transform.localPosition = new Vector3(0f, -.25f, 0f);
        }
    }
}