using System;
using System.Linq;
using UnityEngine;

public class PrismGenerator : PlaneGenerator
{
    #region InspectorVariables
    [Header("Plane values")]
    [SerializeField]
    private Vector3 _offset;
    public Vector3 Offset
    {
        get => _offset;
        set
        {
            _offset = value;
            GenerateShapeForAll();
        }
    }

    public bool ExpandBothDirections = false;
    #endregion InspectorVariables

    #region Implementation
    protected override (Vector3[] vertices, int[] triangles) GenerateTopology()
    {
        (Vector3[] vertices, int[] triangles) plane = CreatePlane(_vertices);
        Vector3[] normals = CreateMesh(plane).normals;

        return CreatePrism(
                ( // Topology for top
                    plane.vertices
                        .Zip(normals, (v, n) =>
                            v + Quaternion.FromToRotation(Vector3.up, n) *
                                (ExpandBothDirections ? (Offset * 0.5f) : Offset))
                        .ToArray(),
                    plane.triangles
                ),
                ( // Topology for bottom
                    ExpandBothDirections
                        ? plane.vertices
                            .Zip(normals, (v, n) =>
                                v + Quaternion.FromToRotation(Vector3.up, n) *
                                    -(Offset * 0.5f))
                            .ToArray()
                        : plane.vertices,
                    plane.triangles.Reverse().ToArray()
                ),
                null, null
            );
    }
    #endregion Implementation
}