import { Vector3 } from "three";
import { RoundedBox } from "@react-three/drei";

function ZoneCornerReference({
    point,
    prevPoint,
    nextPoint,
    zone,
    showTop = true,
    showBottom = true,
    cornerThickness = 0.25,
}: Readonly<{
    point: Point;
    prevPoint: Point;
    nextPoint: Point;
    zone: Zone;
    showTop?: boolean;
    showBottom?: boolean;
    cornerThickness?: number;
}>) {
    const baseCorner = new Vector3(point.position[0], (zone.points[0].layer - 1) * zone.zoneHeight, point.position[2]);
    const halfHeight = zone.zoneHeight / 4;

    const dirNext = new Vector3(nextPoint.position[0] - point.position[0], 0, nextPoint.position[2] - point.position[2]).normalize();
    const dirPrev = new Vector3(prevPoint.position[0] - point.position[0], 0, prevPoint.position[2] - point.position[2]).normalize();

    const bottom1: [number, number, number] = [baseCorner.x, baseCorner.y + halfHeight / 2, baseCorner.z];
    const bottom2: [number, number, number] = [
        baseCorner.clone().add(dirNext.clone().multiplyScalar(halfHeight / 2)).x,
        baseCorner.clone().add(dirNext.clone().multiplyScalar(halfHeight / 2)).y + cornerThickness / 2,
        baseCorner.clone().add(dirNext.clone().multiplyScalar(halfHeight / 2)).z,
    ];
    const bottom3: [number, number, number] = [
        baseCorner.clone().add(dirPrev.clone().multiplyScalar(halfHeight / 2)).x,
        baseCorner.clone().add(dirPrev.clone().multiplyScalar(halfHeight / 2)).y + cornerThickness / 2,
        baseCorner.clone().add(dirPrev.clone().multiplyScalar(halfHeight / 2)).z,
    ];

    const top1: [number, number, number] = [bottom1[0], bottom1[1] + zone.zoneHeight - halfHeight + cornerThickness / 2, bottom1[2]];
    const top2: [number, number, number] = [bottom2[0], bottom2[1] + zone.zoneHeight - cornerThickness / 2, bottom2[2]];
    const top3: [number, number, number] = [bottom3[0], bottom3[1] + zone.zoneHeight - cornerThickness / 2, bottom3[2]];

    return (
        <>
            {showBottom && (
                <>
                    <RoundedBox radius={0.1} args={[cornerThickness, halfHeight, cornerThickness]} position={bottom1}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>

                    <RoundedBox radius={0.1} args={[halfHeight, cornerThickness, cornerThickness]} position={bottom2} rotation={[0, -Math.atan2(dirNext.z, dirNext.x), 0]}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>

                    <RoundedBox radius={0.1} args={[halfHeight, cornerThickness, cornerThickness]} position={bottom3} rotation={[0, -Math.atan2(dirPrev.z, dirPrev.x), 0]}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>
                </>
            )}

            {showTop && (
                <>
                    <RoundedBox radius={0.1} args={[cornerThickness, halfHeight, cornerThickness]} position={top1}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>

                    <RoundedBox radius={0.1} args={[halfHeight, cornerThickness, cornerThickness]} position={top2} rotation={[0, -Math.atan2(dirNext.z, dirNext.x), 0]}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>

                    <RoundedBox radius={0.1} args={[halfHeight, cornerThickness, cornerThickness]} position={top3} rotation={[0, -Math.atan2(dirPrev.z, dirPrev.x), 0]}>
                        <meshStandardMaterial color={zone.zoneColor} />
                    </RoundedBox>
                </>
            )}
        </>
    );
}

export default ZoneCornerReference;
