import { assert } from "../../../../../core/assert.js";
import {
    bvh32_query_user_data_overlaps_clipping_volume
} from "../../../../../core/bvh2/binary/2/bvh32_query_user_data_overlaps_clipping_volume.js";
import {
    typed_array_value_denormalize
} from "../../../../../core/collection/array/typed/typed_array_value_denormalize.js";
import {
    triangle_intersects_clipping_volume
} from "../../../../../core/geom/3d/triangle/triangle_intersects_clipping_volume.js";

const scratch_array = []

/**
 *
 * @param {BinaryUint32BVH} bvh
 * @param {number[]|ArrayLike<number>} vertices
 * @param {number} vertex_offset Unless you're using an interleaved buffer of some kind, this will be 0
 * @param {number} vertex_stride Unless you're using an interleaved buffer, this should be 3
 * @param {boolean} vertex_data_normalized do we need to denormalize vertex data?
 * @param {number[]|ArrayLike<number>|undefined} indices if this is set to undefined - implicit indexing will be used
 * @param {number[]|Float32Array} planes
 * @param {number} planes_offset
 * @param {number} plane_count
 * @returns {boolean}
 */
export function bvh32_geometry_overlap_clipping_volume(
    bvh,
    vertices, vertex_offset, vertex_stride,
    vertex_data_normalized,
    indices,
    planes, planes_offset, plane_count
) {

    assert.isBoolean(vertex_data_normalized, 'vertex_data_normalized');
    assert.isNonNegativeInteger(planes_offset, 'planes_offset');
    assert.isNonNegativeInteger(plane_count, 'plane_count');

    const hit_count = bvh32_query_user_data_overlaps_clipping_volume(
        scratch_array, 0,
        bvh,
        planes, planes_offset, plane_count
    );

    let a, b, c;

    for (let i = 0; i < hit_count; i++) {
        const triangle_index = scratch_array[i];

        const index3 = triangle_index * 3;

        if (indices !== undefined) {
            assert.lessThan(index3 + 2, indices.length, 'triangle index overflow, possibly geometry changed but tree was not rebuilt?');

            a = indices[index3];
            b = indices[index3 + 1];
            c = indices[index3 + 2];
        } else {
            // implicit indices
            a = index3;
            b = index3 + 1;
            c = index3 + 2;
        }

        const a_address = a * vertex_stride + vertex_offset;
        const b_address = b * vertex_stride + vertex_offset;
        const c_address = c * vertex_stride + vertex_offset;

        assert.lessThan(a_address + 2, vertices.length, 'a-vertex overflow');
        assert.lessThan(b_address + 2, vertices.length, 'b-vertex overflow');
        assert.lessThan(c_address + 2, vertices.length, 'c-vertex overflow');

        let ax = vertices[a_address];
        let ay = vertices[a_address + 1];
        let az = vertices[a_address + 2];

        let bx = vertices[b_address];
        let by = vertices[b_address + 1];
        let bz = vertices[b_address + 2];

        let cx = vertices[c_address];
        let cy = vertices[c_address + 1];
        let cz = vertices[c_address + 2];

        // denormalize if necessary
        if (vertex_data_normalized) {
            ax = typed_array_value_denormalize(ax, vertices);
            ay = typed_array_value_denormalize(ay, vertices);
            az = typed_array_value_denormalize(az, vertices);

            bx = typed_array_value_denormalize(bx, vertices);
            by = typed_array_value_denormalize(by, vertices);
            bz = typed_array_value_denormalize(bz, vertices);

            cx = typed_array_value_denormalize(cx, vertices);
            cy = typed_array_value_denormalize(cy, vertices);
            cz = typed_array_value_denormalize(cz, vertices);
        }

        if (triangle_intersects_clipping_volume(
            planes, planes_offset, plane_count,
            ax, ay, az,
            bx, by, bz,
            cx, cy, cz
        )) {
            return true;
        }

    }

    return false;
}