import * as THREE from 'three'
import { useEffect, useMemo, useRef, useState } from 'react'
import { useThree } from '@react-three/fiber';
import { useActiveLayer, useSocketStore, useToggleView, useToolMode } from '../../../../store/builder/store';
import { useSceneContext } from '../../../scene/sceneContext';
import { useBuilderStore } from '../../../../store/builder/useBuilderStore';
import { useParams } from 'react-router-dom';
import { useVersionContext } from '../../version/versionContext';
import { getUserData } from '../../../../functions/getUserData';
import ReferencePoint from '../../point/reference/referencePoint';
import ReferenceFloor from './referenceFloor';

// import { upsertFloorApi } from '../../../../services/factoryBuilder/floor/upsertFloorApi';

function FloorCreator() {
    const { scene, camera, raycaster, gl, pointer } = useThree();
    const plane = useMemo(() => new THREE.Plane(new THREE.Vector3(0, 1, 0), 0), []);
    const { toggleView } = useToggleView();
    const { toolMode } = useToolMode();
    const { activeLayer } = useActiveLayer();
    const { socket } = useSocketStore();
    const { floorStore } = useSceneContext();
    const { addFloor, getFloorPointById, getFloorByPoints } = floorStore();
    const drag = useRef(false);
    const isLeftMouseDown = useRef(false);
    const { selectedVersionStore } = useVersionContext();
    const { selectedVersion } = selectedVersionStore();
    const { userId, organization } = getUserData();
    const { projectId } = useParams();

    const [tempPoints, setTempPoints] = useState<Point[]>([]);
    const [isCreating, setIsCreating] = useState(false);
    const { floorDepth, isBeveled, bevelStrength, sideMaterial, topMaterial, snappedPosition, snappedPoint, setSnappedPoint, setSnappedPosition } = useBuilderStore();

    useEffect(() => {
        const canvasElement = gl.domElement;

        const onMouseDown = (evt: any) => {
            if (evt.button === 0) {
                isLeftMouseDown.current = true;
                drag.current = false;
            }
        };

        const onMouseUp = (evt: any) => {
            if (evt.button === 0) {
                isLeftMouseDown.current = false;
            }
        };

        const onMouseMove = () => {
            if (isLeftMouseDown) {
                drag.current = true;
            }
        };

        const onMouseClick = () => {
            if (drag.current || !toggleView) return;

            raycaster.setFromCamera(pointer, camera);
            const intersectionPoint = new THREE.Vector3();
            let position = raycaster.ray.intersectPlane(plane, intersectionPoint);
            if (!position) return;

            const pointIntersects = raycaster.intersectObjects(scene.children).find((intersect) => intersect.object.name === 'Floor-Point');

            // const floorIntersect = raycaster.intersectObjects(scene.children).find((intersect) => intersect.object.name === 'Floor-Line');

            // if (floorIntersect && !pointIntersects) {

            // }

            const newPoint: Point = {
                pointUuid: THREE.MathUtils.generateUUID(),
                pointType: 'Floor',
                position: [position.x, position.y, position.z],
                layer: activeLayer
            };

            if (snappedPosition && snappedPoint) {
                newPoint.pointUuid = snappedPoint.pointUuid;
                newPoint.position = snappedPosition;
                newPoint.layer = snappedPoint.layer;
            }

            if (snappedPoint && snappedPoint.pointUuid === tempPoints[tempPoints.length - 1]?.pointUuid) { return }

            if (snappedPosition && !snappedPoint) {
                newPoint.position = snappedPosition;
            }

            if (tempPoints.length > 2 && isCreating && snappedPoint && snappedPoint.pointUuid === tempPoints[0].pointUuid) {
                const floor: Floor = {
                    floorUuid: THREE.MathUtils.generateUUID(),
                    floorName: "Floor",
                    points: tempPoints,
                    topMaterial,
                    sideMaterial,
                    floorDepth,
                    isBeveled,
                    bevelStrength,
                    decals: [],
                };

                addFloor(floor);
                if (projectId) {

                    // API

                    // upsertFloorApi(projectId, selectedVersion?.versionId || '', floor);

                    // SOCKET

                    const data = {
                        floorData: floor,
                        projectId: projectId,
                        versionId: selectedVersion?.versionId || '',
                        userId: userId,
                        organization: organization
                    }

                    socket.emit('v1:model-Floor:add', data);

                }
                setTempPoints([]);
                setIsCreating(false);
            } else if (isCreating && snappedPoint && !tempPoints.some(p => p.pointUuid === snappedPoint.pointUuid)) {
                setTempPoints(prev => [...prev, newPoint]);
                setIsCreating(true);
            } else if (pointIntersects) {
                if (tempPoints.length > 2 && isCreating && pointIntersects.object.uuid === tempPoints[0].pointUuid) {
                    const floor: Floor = {
                        floorUuid: THREE.MathUtils.generateUUID(),
                        floorName: "Floor",
                        points: tempPoints,
                        topMaterial,
                        sideMaterial,
                        floorDepth,
                        isBeveled,
                        bevelStrength,
                        decals: [],
                    };

                    addFloor(floor);
                    if (projectId) {

                        // API

                        // upsertFloorApi(projectId, selectedVersion?.versionId || '', floor);

                        // SOCKET

                        const data = {
                            floorData: floor,
                            projectId: projectId,
                            versionId: selectedVersion?.versionId || '',
                            userId: userId,
                            organization: organization
                        }

                        socket.emit('v1:model-Floor:add', data);

                    }
                    setTempPoints([]);
                    setIsCreating(false);
                } else if (tempPoints.length === 0 || (tempPoints.length > 1 && !tempPoints.slice(1).some(p => p.pointUuid === pointIntersects.object.uuid))) {
                    tempPoints.push(pointIntersects.object.userData as Point);
                    setIsCreating(true);
                }
            } else {
                setTempPoints(prev => [...prev, newPoint]);
                setIsCreating(true);
            }

        };

        const onContext = (event: any) => {
            event.preventDefault();
            if (isCreating) {
                if (tempPoints.length >= 3) {
                    const floor: Floor = {
                        floorUuid: THREE.MathUtils.generateUUID(),
                        floorName: "Floor",
                        points: tempPoints,
                        topMaterial,
                        sideMaterial,
                        floorDepth,
                        isBeveled,
                        bevelStrength,
                        decals: [],
                    };

                    addFloor(floor);
                    if (projectId) {

                        // API

                        // upsertFloorApi(projectId, selectedVersion?.versionId || '', floor);

                        // SOCKET

                        const data = {
                            floorData: floor,
                            projectId: projectId,
                            versionId: selectedVersion?.versionId || '',
                            userId: userId,
                            organization: organization
                        }

                        socket.emit('v1:model-Floor:add', data);

                    }
                }
                setTempPoints([]);
                setIsCreating(false);
            }
        };

        if (toolMode === "Floor" && toggleView) {
            if (tempPoints.length === 0) {
                setSnappedPosition(null);
                setSnappedPoint(null);
            }
            canvasElement.addEventListener("mousedown", onMouseDown);
            canvasElement.addEventListener("mouseup", onMouseUp);
            canvasElement.addEventListener("mousemove", onMouseMove);
            canvasElement.addEventListener("click", onMouseClick);
            canvasElement.addEventListener("contextmenu", onContext);
        } else {
            setTempPoints([]);
            setIsCreating(false);
            canvasElement.removeEventListener("mousedown", onMouseDown);
            canvasElement.removeEventListener("mouseup", onMouseUp);
            canvasElement.removeEventListener("mousemove", onMouseMove);
            canvasElement.removeEventListener("click", onMouseClick);
            canvasElement.removeEventListener("contextmenu", onContext);
        }

        return () => {
            canvasElement.removeEventListener("mousedown", onMouseDown);
            canvasElement.removeEventListener("mouseup", onMouseUp);
            canvasElement.removeEventListener("mousemove", onMouseMove);
            canvasElement.removeEventListener("click", onMouseClick);
            canvasElement.removeEventListener("contextmenu", onContext);
        };
    }, [gl, camera, scene, raycaster, pointer, plane, toggleView, toolMode, activeLayer, socket, tempPoints, isCreating, addFloor, getFloorPointById, getFloorByPoints, floorDepth, isBeveled, bevelStrength, sideMaterial, topMaterial, snappedPosition, snappedPoint]);

    return (
        <>
            {toggleView &&
                <>
                    <group name='Floor-Reference-Points-Group'>
                        {tempPoints.map((point) => (
                            <ReferencePoint key={point.pointUuid} point={point} />
                        ))}
                    </group>

                    {tempPoints.length > 0 &&
                        <ReferenceFloor tempPoints={tempPoints} />
                    }
                </>
            }
        </>
    )
}

export default FloorCreator