import * as THREE from 'three';
import { useMemo, useRef } from 'react';
import { Extrude } from '@react-three/drei';
import * as Constants from '../../../../../../types/world/worldConstants';
import { useToolMode } from '../../../../../../store/builder/store';
import { useBuilderStore } from '../../../../../../store/builder/useBuilderStore';

function ArrowAisle({ aisle }: { readonly aisle: Aisle }) {
    const aisleRef = useRef<THREE.Group>(null);
    const { toolMode } = useToolMode();
    const { setSelectedAisle, hoveredPoint } = useBuilderStore();

    const arrow = useMemo(() => {
        if (aisle.points.length < 2 || aisle.type.aisleType !== 'arrow-aisle') return null;

        const start = new THREE.Vector3(...aisle.points[0].position);
        const end = new THREE.Vector3(...aisle.points[1].position);
        const width = aisle.type.aisleWidth || 0.1;

        const direction = new THREE.Vector3().subVectors(end, start);
        const length = direction.length();
        direction.normalize();

        const shape = new THREE.Shape();
        const arrowHeadLength = width * 2;
        const shaftLength = length - arrowHeadLength;

        if (shaftLength > 0) {
            shape.moveTo(0, 0);
            shape.lineTo(width, -arrowHeadLength);
            shape.lineTo(width / 2, -arrowHeadLength);
            shape.lineTo(width / 2, -length);
            shape.lineTo(-width / 2, -length);
            shape.lineTo(-width / 2, -arrowHeadLength);
            shape.lineTo(-width, -arrowHeadLength);
            shape.lineTo(0, 0);
        } else {
            shape.moveTo(0, 0);
            shape.lineTo(width, -length);
            shape.lineTo(-width, -length);
            shape.lineTo(0, 0);
        }

        shape.closePath();

        const position = end;
        const angle = Math.atan2(direction.x, direction.z);

        return { shape, position, rotationY: angle };
    }, [aisle]);

    const handleClick = () => {
        if (toolMode === 'move' && !hoveredPoint) {
            setSelectedAisle(aisleRef.current);
        }
    }

    if (!arrow) return null;

    return (
        <group
            name='Arrow-Aisle'
            uuid={aisle.aisleUuid}
            ref={aisleRef}
            position={[0, (aisle.points[0].layer - 1) * Constants.wallConfig.height + 0.01, 0]}
            rotation={[Math.PI / 2, 0, 0]}
            userData={aisle}
            onDoubleClick={handleClick}
            onPointerMissed={() => {
                setSelectedAisle(null);
            }}
        >
            <group position={[arrow.position.x, arrow.position.z, 0]} rotation={[0, 0, -arrow.rotationY]}>
                <Extrude
                    args={[arrow.shape, { depth: 0.01, bevelEnabled: false }]}
                    receiveShadow
                    castShadow
                >
                    <meshStandardMaterial
                        color={aisle.type.aisleColor || '#ffffff'}
                        side={THREE.DoubleSide}
                    />
                </Extrude>
            </group>
        </group>
    );
}

export default ArrowAisle;