import { useMemo } from "react";
import * as THREE from "three";
import BaseMesh, { BaseMeshProps } from "../BaseMesh";

export interface CylinderGeometry {
    radiusTop: number;
    radiusBottom: number;
    height: number;
    radialSegments?: number;
    heightSegments?: number;
    openEnded?: boolean;
    thetaStart?: number;
    thetaLength?: number;
}

interface Props extends Omit<BaseMeshProps, "geometry"> {
    geometry: CylinderGeometry;
}

export default function Cylinder(props: Props) {
    const { geometry } = props;

    const geo = useMemo(() => {
        return new THREE.CylinderGeometry(
            geometry.radiusTop,
            geometry.radiusBottom,
            geometry.height,
            geometry.radialSegments ?? 24,
            geometry.heightSegments ?? 1,
            geometry.openEnded ?? false,
            geometry.thetaStart ?? 0,
            geometry.thetaLength ?? Math.PI * 2
        );
    }, [
        geometry.radiusTop,
        geometry.radiusBottom,
        geometry.height,
        geometry.radialSegments,
        geometry.heightSegments,
        geometry.openEnded,
        geometry.thetaStart,
        geometry.thetaLength
    ]);

    return <BaseMesh {...props} geometry={geo} />;
}