import { LineBasicMaterial } from "three";
import Entity from "../../../../engine/ecs/Entity.js";
import { Transform } from "../../../../engine/ecs/transform/Transform.js";
import { DrawMode } from "../../../../engine/graphics/ecs/mesh-v2/DrawMode.js";
import { ShadedGeometry } from "../../../../engine/graphics/ecs/mesh-v2/ShadedGeometry.js";
import { ShadedGeometryFlags } from "../../../../engine/graphics/ecs/mesh-v2/ShadedGeometryFlags.js";
import { assert } from "../../../assert.js";
import { build_tetrahedral_mesh_buffer_geometry } from "./graphics/build_tetrahedral_mesh_buffer_geometry.js";

/**
 *
 * @param {TetrahedralMesh} mesh
 * @param {number[]} positions
 * @param {string|number} [color] CSS color definition
 * @param {number} [opacity]
 * @returns {Entity}
 */
export function visualize_tetrahedral_mesh({
                                               mesh,
                                               positions,
                                               color = '#FF00FF',
                                               opacity = 0.35
                                           }) {
    assert.defined(mesh, 'mesh');
    assert.defined(positions, 'positions');

    const geometry = build_tetrahedral_mesh_buffer_geometry(mesh, positions);

    const sg = ShadedGeometry.from(geometry, new LineBasicMaterial({
        color: color,
        transparent: true,
        opacity,
        depthWrite: false
    }), DrawMode.LineSegments);

    sg.clearFlag(ShadedGeometryFlags.CastShadow);
    sg.clearFlag(ShadedGeometryFlags.ReceiveShadow);

    const entity = new Entity();

    entity
        .add(sg)
        .add(new Transform());

    return entity;
}