import { useEffect } from "react";
import * as Types from '../../../types/world/worldTypes';
import { useActiveLayer, useDeletedLines, useToolMode, useNewLines, useRemovedLayer, useSocketStore, useToggleView, useUpdateScene } from "../../../store/builder/store";
import Layer2DVisibility from "../geomentries/layers/layer2DVisibility";
import { useFrame, useThree } from "@react-three/fiber";
import DeletableLineorPoint from "../functions/deletableLineOrPoint";
import removeSoloPoint from "../geomentries/points/removeSoloPoint";
import removeReferenceLine from "../geomentries/lines/removeReferenceLine";
import DeleteLayer from "../geomentries/layers/deleteLayer";
import { getLines } from "../../../services/factoryBuilder/lines/getLinesApi";
import objectLinesToArray from "../geomentries/lines/lineConvertions/objectLinesToArray";
import loadInitialPoint from "../IntialLoad/loadInitialPoint";
import loadInitialLine from "../IntialLoad/loadInitialLine";
import deletePoint from "../geomentries/points/deletePoint";
import deleteLine from "../geomentries/lines/deleteLine";
import drawWall from "../geomentries/lines/drawWall";
import drawOnlyFloor from "../geomentries/floors/drawOnlyFloor";
import addDragControl from "../eventDeclaration/dragControlDeclaration";
import { useParams } from "react-router-dom";

const FloorPlanGroup = ({ floorPlanGroup, floorPlanGroupLine, floorPlanGroupPoint, floorGroup, currentLayerPoint, dragPointControls, hoveredDeletablePoint, hoveredDeletableLine, plane, line, lines, onlyFloorline, onlyFloorlines, ReferenceLineMesh, LineCreated, isSnapped, ispreSnapped, snappedPoint, isSnappedUUID, isAngleSnapped, anglesnappedPoint }: any) => {
    const state = useThree();
    const { camera, gl, raycaster, controls } = state;
    const { activeLayer } = useActiveLayer();
    const { toggleView } = useToggleView();
    const { toolMode } = useToolMode();
    const { removedLayer, setRemovedLayer } = useRemovedLayer();
    const { setUpdateScene } = useUpdateScene();
    const { setNewLines } = useNewLines();
    const { setDeletedLines } = useDeletedLines();
    const { socket } = useSocketStore();
    const { projectId } = useParams();

    useEffect(() => {
        if (toolMode === 'move') {
            addDragControl(dragPointControls, currentLayerPoint, state, floorPlanGroupPoint, floorPlanGroupLine, lines, onlyFloorlines, socket, projectId);
        }

        return () => {
            if (dragPointControls.current) {
                dragPointControls.current.enabled = false;
            }
        };
    }, [toolMode, state]);

    useEffect(() => {
        const email = localStorage.getItem('email')
        const organization = (email!.split("@")[1]).split(".")[0];

        // Load data from localStorage if available
        getLines(organization, projectId).then((data) => {
            // console.log('data: ', data);

            const Lines: Types.Lines = objectLinesToArray(data);

            // const data = localStorage.getItem("Lines");

            if (Lines) {
                lines.current = Lines;
                loadInitialPoint(lines, floorPlanGroupPoint, currentLayerPoint, dragPointControls);
                loadInitialLine(floorPlanGroupLine, lines);
                setUpdateScene(true);
            }
        })
    }, []);

    useEffect(() => {
        if (!toggleView) {
            removeSoloPoint(line, floorPlanGroupLine, floorPlanGroupPoint);
            removeReferenceLine(floorPlanGroup, ReferenceLineMesh, LineCreated, line);
        }
    }, [toggleView]);

    useEffect(() => {
        removeSoloPoint(line, floorPlanGroupLine, floorPlanGroupPoint);
        removeReferenceLine(floorPlanGroup, ReferenceLineMesh, LineCreated, line);
    }, [toolMode]);

    useEffect(() => {
        if (toolMode === 'move' && toggleView) {
            if (dragPointControls.current) {
                dragPointControls.current.enabled = true;
            }
        } else {
            if (dragPointControls.current) {
                dragPointControls.current.enabled = false;
            }
        }
    }, [toolMode, toggleView, state]);

    useEffect(() => {
        Layer2DVisibility(activeLayer, floorPlanGroup, floorPlanGroupLine, floorPlanGroupPoint, currentLayerPoint, dragPointControls);
    }, [activeLayer]);

    useEffect(() => {
        if (removedLayer !== null) {
            DeleteLayer(removedLayer, lines, floorPlanGroupLine, floorPlanGroupPoint, onlyFloorlines, floorGroup, setDeletedLines, setRemovedLayer, socket, projectId);
        }
    }, [removedLayer]);

    useEffect(() => {

        const canvasElement = gl.domElement;

        let drag = false;
        let isLeftMouseDown = false;

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

        const onMouseUp = (evt: any) => {
            if (evt.button === 0) {
                isLeftMouseDown = false;
            }
            if (controls) {
                (controls as any).enabled = true;
            }
        }

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

        const onContextMenu = (e: any) => {
            e.preventDefault();
            if (toolMode === "Wall" || toolMode === "Floor") {
                removeSoloPoint(line, floorPlanGroupLine, floorPlanGroupPoint);
                removeReferenceLine(floorPlanGroup, ReferenceLineMesh, LineCreated, line);
            }
        };

        const onMouseClick = (evt: any) => {
            if (!plane.current || drag) return;

            if (toolMode === "2D-Delete") {
                if (hoveredDeletablePoint.current !== null) {
                    deletePoint(hoveredDeletablePoint, onlyFloorlines, floorPlanGroupPoint, floorPlanGroupLine, lines, setDeletedLines, socket, projectId);
                }
                if (hoveredDeletableLine.current !== null) {
                    deleteLine(hoveredDeletableLine, onlyFloorlines, lines, floorPlanGroupLine, floorPlanGroupPoint, setDeletedLines, socket, projectId);
                }
            }

            if (toolMode === "Wall") {
                drawWall(raycaster, plane, floorPlanGroupPoint, snappedPoint, isSnapped, isSnappedUUID, line, ispreSnapped, anglesnappedPoint, isAngleSnapped, lines, floorPlanGroupLine, floorPlanGroup, ReferenceLineMesh, LineCreated, currentLayerPoint, dragPointControls, setNewLines, setDeletedLines, activeLayer, socket, projectId);
            }

            if (toolMode === "Floor") {
                drawOnlyFloor(raycaster, state, camera, plane, floorPlanGroupPoint, snappedPoint, isSnapped, isSnappedUUID, line, ispreSnapped, anglesnappedPoint, isAngleSnapped, onlyFloorline, onlyFloorlines, lines, floorPlanGroupLine, floorPlanGroup, ReferenceLineMesh, LineCreated, currentLayerPoint, dragPointControls, setNewLines, setDeletedLines, activeLayer, socket, projectId);
            }
        }

        if (toolMode === "2D-Delete" || toolMode === "Wall" || toolMode === "Floor") {
            canvasElement.addEventListener("mousedown", onMouseDown);
            canvasElement.addEventListener("mouseup", onMouseUp);
            canvasElement.addEventListener("mousemove", onMouseMove);
            canvasElement.addEventListener("click", onMouseClick);
            canvasElement.addEventListener("contextmenu", onContextMenu);
        }

        return () => {
            canvasElement.removeEventListener("mousedown", onMouseDown);
            canvasElement.removeEventListener("mouseup", onMouseUp);
            canvasElement.removeEventListener("mousemove", onMouseMove);
            canvasElement.removeEventListener("click", onMouseClick);
            canvasElement.removeEventListener("contextmenu", onContextMenu);
        };
    }, [toolMode, activeLayer])


    useFrame(() => {
        if (toolMode === '2D-Delete') {
            DeletableLineorPoint(state, plane, floorPlanGroupLine, floorPlanGroupPoint, hoveredDeletableLine, hoveredDeletablePoint);
        }
    })

    return (
        <group ref={floorPlanGroup} visible={toggleView} name="floorPlanGroup">
            <group ref={floorPlanGroupLine} name="floorPlanGroupLine"></group>
            <group ref={floorPlanGroupPoint} name="floorPlanGroupPoint"></group>
        </group>
    )
}

export default FloorPlanGroup;