import { useEffect } from "react";
import { useActionHandler } from "../actions/useActionHandler";
import { usePlayButtonStore, useResetButtonStore } from "../../../store/usePlayButtonStore";
import { determineExecutionOrder } from "./functions/determineExecutionOrder";
import { useProductContext } from "../products/productContext";
import { useSceneContext } from "../../scene/sceneContext";
import SimulationHandler from "./SimulationHandler";
import { getSimulationData, saveSimulationData } from "../../../components/layout/scenes/functions/simulationStorage";
import { useParams } from "react-router-dom";
import { useVersionContext } from "../../builder/version/versionContext";
import { version } from "os";
import { get } from "http";
import { useSimulationManager } from "../../../store/rough/useSimulationManagerStore";

function Simulator() {
    const { selectedProductStore } = useProductContext();
    const { productStore } = useSceneContext();
    const { products, getProductById } = productStore();
    const { handleAction } = useActionHandler();
    const { selectedProduct } = selectedProductStore();
    const { isPlaying } = usePlayButtonStore();
    const { isReset } = useResetButtonStore();
    const { projectId } = useParams();
    const { selectedVersionStore } = useVersionContext();
    const { selectedVersion } = selectedVersionStore();

    useEffect(() => {
        if (!isPlaying || isReset || !selectedProduct.productUuid) return;

        const product = getProductById(selectedProduct.productUuid);
        if (!product) return;

        const executionOrder = determineExecutionOrder([product]);

        executionOrder.forEach((action) => {
            handleAction(action);
        });
    }, [products, isPlaying, isReset, selectedProduct]);
    useEffect(() => {
        if (!projectId || !selectedVersion || !selectedProduct?.productUuid) return;
        const fetchSimulateData = async () => {
            const getData = await getSimulationData(projectId, selectedVersion.versionId, selectedProduct?.productUuid);
            const product = getProductById(selectedProduct.productUuid);
            if (!product) return;
            const products: productsSchema = [product];
            const getSimulate = getData?.data;
            if (getData && getSimulate && getSimulate.productTimestamp === products[0]?.timestamp) {
                return;
            } else {
                //call create API
                const data = {
                    projectId: projectId,
                    versionId: selectedVersion.versionId,
                    productUuid: selectedProduct.productUuid,
                    simulateData: useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData,
                };
                const simulations = await saveSimulationData(data);
                console.log("simulations: ", simulations);
                echo.log("Simulation data saved successfully");
            }
        };
        fetchSimulateData();
    }, []);
    return (
        <>
            {/* <simulationHandler/> */}
            <SimulationHandler />
        </>
    );
}

export default Simulator;
