import { useEffect } from "react";
import RoboticArmInstances from "./instances/roboticArmInstances";
import { useArmBotStore } from "../../../store/simulation/useArmBotStore";
import { useFloorItems } from "../../../store/store";
import { useSelectedEventData, useSelectedEventSphere, useSelectedProduct } from "../../../store/simulation/useSimulationStore";
import { useProductStore } from "../../../store/simulation/useProductStore";
import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
import ArmBotUI from "../ui/arm/armBotUI";

function RoboticArm() {
    const { armBots, addArmBot, clearArmBots } = useArmBotStore();
    const { products, getProductById } = useProductStore();
    const { selectedProduct } = useSelectedProduct();
    const { selectedEventSphere } = useSelectedEventSphere();
    const { selectedEventData } = useSelectedEventData();
    const { isPlaying } = usePlayButtonStore();
    const { floorItems } = useFloorItems();

    const armBotStatusSample: RoboticArmEventSchema[] = [
        {
            state: "idle",
            modelUuid: "8790b4d5-fb6e-49e0-8161-04945fe3fdc4",
            modelName: "ArmBot-X200",
            position: [4.317833205016363, 0, -3.2829924989068715],
            rotation: [-1.3768690876192207e-15, 1.4883085074751308, 1.5407776675834467e-15],
            type: "roboticArm",
            speed: 1.5,
            point: {
                uuid: "point-123",
                position: [0, 2.6, 0],
                rotation: [0, 0, 0],
                actions: [
                    {
                        actionUuid: "action-003",
                        actionName: "Pick Component",
                        actionType: "pickAndPlace",
                        process: {
                            startPoint: [-1, 2, 1],
                            endPoint: [-2, 1, -1],
                            // startPoint: [-2, 1, -1],
                            // endPoint: [-1, 2, 1],
                        },
                        triggers: [
                            {
                                triggerUuid: "trigger-001",
                                triggerName: "Start Trigger",
                                triggerType: "onStart",
                                delay: 0,
                                triggeredAsset: {
                                    triggeredModel: {
                                        modelName: "Conveyor A1",
                                        modelUuid: "conveyor-01",
                                    },
                                    triggeredPoint: {
                                        pointName: "Start Point",
                                        pointUuid: "conveyor-01-point-001",
                                    },
                                    triggeredAction: {
                                        actionName: "Move Forward",
                                        actionUuid: "conveyor-action-01",
                                    },
                                },
                            },
                            {
                                triggerUuid: "trigger-002",
                                triggerName: "Complete Trigger",
                                triggerType: "onComplete",
                                delay: 0,
                                triggeredAsset: {
                                    triggeredModel: {
                                        modelName: "StaticMachine B2",
                                        modelUuid: "machine-02",
                                    },
                                    triggeredPoint: {
                                        pointName: "Receive Point",
                                        pointUuid: "machine-02-point-001",
                                    },
                                    triggeredAction: {
                                        actionName: "Process Part",
                                        actionUuid: "machine-action-01",
                                    },
                                },
                            },
                        ],
                    },
                ],
            },
        },
        {
            state: "idle",
            modelUuid: "armbot-xyz-002",
            modelName: "ArmBot-X200",
            position: [95.94347308985614, 0, 6.742905194869091],
            rotation: [0, 0, 0],
            type: "roboticArm",
            speed: 0.1,
            point: {
                uuid: "point-123",
                position: [0, 1.5, 0],
                rotation: [0, 0, 0],
                actions: [
                    {
                        actionUuid: "action-001",
                        actionName: "Pick Component",
                        actionType: "pickAndPlace",
                        process: {
                            // startPoint: [2.52543010919071, 0, 8.433681161200905],
                            // endPoint: [95.3438373267953, 0, 9.0279187421610025],
                            startPoint: null,
                            endPoint: null,
                        },
                        triggers: [
                            {
                                triggerUuid: "trigger-001",
                                triggerName: "Start Trigger",
                                triggerType: "onStart",
                                delay: 0,
                                triggeredAsset: {
                                    triggeredModel: {
                                        modelName: "Conveyor A1",
                                        modelUuid: "conveyor-01",
                                    },
                                    triggeredPoint: {
                                        pointName: "Start Point",
                                        pointUuid: "conveyor-01-point-001",
                                    },
                                    triggeredAction: {
                                        actionName: "Move Forward",
                                        actionUuid: "conveyor-action-01",
                                    },
                                },
                            },
                            {
                                triggerUuid: "trigger-002",
                                triggerName: "Complete Trigger",
                                triggerType: "onComplete",
                                delay: 0,
                                triggeredAsset: {
                                    triggeredModel: {
                                        modelName: "StaticMachine B2",
                                        modelUuid: "machine-02",
                                    },
                                    triggeredPoint: {
                                        pointName: "Receive Point",
                                        pointUuid: "machine-02-point-001",
                                    },
                                    triggeredAction: {
                                        actionName: "Process Part",
                                        actionUuid: "machine-action-01",
                                    },
                                },
                            },
                        ],
                    },
                ],
            },
        },
    ];

    useEffect(() => {
        if (selectedProduct.productId) {
            const product = getProductById(selectedProduct.productId);
            if (product) {
                clearArmBots();
                product.eventDatas.forEach(events => {
                    if (events.type === 'roboticArm') {
                        addArmBot(selectedProduct.productId, events);
                    }
                });
            }
        }
    }, [selectedProduct, products]);

    useEffect(() => {
    }, [armBots])

    useEffect(() => {

    }, [selectedEventData, selectedEventSphere, isPlaying]);

    return (
        <>
            <RoboticArmInstances />
            {selectedEventSphere && selectedEventData?.data.type === "roboticArm" &&
                < ArmBotUI />
            }
        </>
    );
}

export default RoboticArm;
