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

namespace Intrepid.Gameplay.Prototyping
{
    [ExecuteAlways]
    public class ObstacleWallModelPrototype : ObstacleModelPrototype
    {
        [GroupConfigurations, SerializeField, ReadOnly]  private int sizeX = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeY = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private int sizeZ = 1;
        [GroupConfigurations, SerializeField, ReadOnly] private Vector2 offset_x = new(1f, 1.5f);
        [GroupConfigurations, SerializeField, ReadOnly] private Vector2 offset_y = new(1f, 1.5f);
        
        [GroupDebug, SerializeField, Min(1)]  private int x = 1;
        [GroupDebug, SerializeField, Min(1)] private int y = 1;
        [GroupDebug, SerializeField, Min(1)] private int z = 1;
        [GroupDebug, SerializeField] private Vector2 contact_offset_x = new(1f, 1.5f);
        [GroupDebug, SerializeField] private Vector2 contact_offset_y = new(1f, 1.5f);

        public override void SetSize(Vector2Int size)
        {
            x = size.x;
            z = size.y;
            SizeFitting();
        }

        protected override void DebugResize()
        {
            x = Mathf.Max(1, x);
            y = Mathf.Max(1, y);
            z = Mathf.Max(1, z);
            sizeX = x;
            sizeY = y;
            sizeZ = z;
            offset_x.x = Mathf.Clamp(contact_offset_x.x, 0f, 2f);
            offset_x.y = Mathf.Clamp(contact_offset_x.y, 0f, 2f);
            offset_y.x = Mathf.Clamp(contact_offset_y.x, 0f, 2f);
            offset_y.y = Mathf.Clamp(contact_offset_y.y, 0f, 2f);
        }

        // 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, sizeY, sizeZ,
        //         sizeX * 0.5f, sizeY * 0.5f, sizeZ * 0.5f);
        //     
        //     PrototypeModel.BuildMesh(new MeshData(
        //         vertices.ToArray(),
        //         triangles.ToArray(),
        //         uvs.ToArray()));
        //
        //     var offsetX = offset_x.x + offset_x.y;
        //     var offsetY = offset_y.x + offset_y.y;
        //     PrototypeModel.BoxCollider.size = new Vector3(sizeX + offsetX, sizeY, sizeZ + offsetY);
        //     PrototypeModel.BoxCollider.center = new Vector3((offset_x.x - offset_x.y) / 2f, 0f, (offset_y.x - offset_y.y) / 2f);
        //     PrototypeModel.transform.localPosition = new Vector3(0f, sizeY / 2f, 0f);
        // }
        
        protected override void Generate()
        {
            var vertices = new List<Vector3>();
            var triangles = new List<int>();
            var uvs = new List<Vector2>();

            var halfX = sizeX * 0.5f;
            var halfY = sizeY * 0.5f;
            var halfZ = sizeZ * 0.5f;

            MeshUtils.AddCubeFaces(
                vertices,
                triangles,
                uvs,
                sizeX,
                sizeY,
                sizeZ,
                halfX,
                halfY,
                halfZ
            );
            
            // MeshUtils.AddInternalHorizontalCapFaces(
            //     vertices,
            //     triangles,
            //     uvs,
            //     sizeX,
            //     sizeY,
            //     sizeZ,
            //     halfX,
            //     halfY,
            //     halfZ
            // );

            PrototypeModel.BuildMesh(new MeshData(
                vertices.ToArray(),
                triangles.ToArray(),
                uvs.ToArray()));

            var offsetX = offset_x.x + offset_x.y;
            var offsetY = offset_y.x + offset_y.y;

            PrototypeModel.BoxCollider.size = new Vector3(
                sizeX + offsetX,
                sizeY,
                sizeZ + offsetY
            );

            PrototypeModel.BoxCollider.center = new Vector3(
                (offset_x.x - offset_x.y) / 2f,
                0f,
                (offset_y.x - offset_y.y) / 2f
            );

            PrototypeModel.transform.localPosition = new Vector3(
                0f,
                sizeY / 2f,
                0f
            );
        }
    }
}