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 ArrowsAisle({ aisle }: { readonly aisle: Aisle }) {
    const aisleRef = useRef<THREE.Group>(null);
    const { toolMode } = useToolMode();
    const { setSelectedAisle, hoveredPoint } = useBuilderStore();

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

        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 arrowLength = aisle.type.aisleLength || 0.6;
        const spacing = aisle.type.gapLength || 0.6;

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

        const count = Math.floor((length + spacing) / (arrowLength + spacing));

        const arrowShapes: { shape: THREE.Shape; position: THREE.Vector3; rotationY: number }[] = [];

        for (let i = 0; i < count; i++) {
            const initialOffset = arrowLength;
            const center = new THREE.Vector3().copy(start).addScaledVector(direction, initialOffset + i * (arrowLength + spacing));

            const shape = new THREE.Shape();
            const w = width * 0.8;
            const h = arrowLength;

            shape.moveTo(0, 0);
            shape.lineTo(w, h * 0.6);
            shape.lineTo(w * 0.4, h * 0.6);
            shape.lineTo(w * 0.4, h);
            shape.lineTo(-w * 0.4, h);
            shape.lineTo(-w * 0.4, h * 0.6);
            shape.lineTo(-w, h * 0.6);
            shape.lineTo(0, 0);

            const angle = Math.atan2(direction.x, direction.z) + Math.PI;

            arrowShapes.push({ shape, position: center, rotationY: angle });
        }

        return arrowShapes;
    }, [aisle]);

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

    if (arrows.length === 0) return null;

    return (
        <group
            name='Arrows-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);
            }}
        >
            {arrows.map(({ shape, position, rotationY }, index) => (
                <group key={index} position={[position.x, position.z, 0]} rotation={[0, 0, -rotationY]}>
                    <Extrude
                        args={[shape, { depth: 0.01, bevelEnabled: false }]}
                        receiveShadow
                        castShadow
                    >
                        <meshStandardMaterial
                            color={aisle.type.aisleColor || '#ffffff'}
                            side={THREE.DoubleSide}
                        />
                    </Extrude>
                </group>
            ))}
        </group>
    );
}

export default ArrowsAisle;
