using System.Linq;
using Sirenix.OdinInspector;
using UnityEngine;

namespace VOID.Generic.AreaOfEffect
{
    [TypeRegistryItem("Line")]
    public class LineAreaOfEffect : AbstractAreaOfEffect
    {
        [SerializeField, Required, MinValue(0.5), MaxValue(10)]
        private float length;
        [SerializeField, Required, MinValue(0.1), MaxValue(1)]
        private float width;

        public override Mesh GetMesh()
        {
            var scale = new Vector3(width, 1, length);
            var cubeMesh = Resources.GetBuiltinResource<Mesh>("Cube.fbx");

            var mesh = new Mesh();
            mesh.vertices = cubeMesh.vertices.Select(vertex => Vector3.Scale(vertex, scale)).ToArray();
            mesh.triangles = cubeMesh.triangles;
            mesh.uv = cubeMesh.uv;
            mesh.normals = cubeMesh.normals;
            mesh.colors = cubeMesh.colors;
            mesh.tangents = cubeMesh.tangents;

            return mesh;
        }

        public override Collider AddCollider(GameObject gameObject)
        {
            var collider = gameObject.AddComponent<BoxCollider>();
            collider.isTrigger = true;
            collider.size = new Vector3(width, 1, length);
            collider.center = new Vector3(0, 0, length/2);

            return collider;
        }
    }
}