import { useEffect, useState } from 'react'
import RenameInput from '../../../../../ui/inputs/RenameInput'
import LabledDropdown from '../../../../../ui/inputs/LabledDropdown'
import Trigger from '../trigger/Trigger'
import { useSelectedEventData, useSelectedProduct } from "../../../../../../store/simulation/useSimulationStore";
import { useProductStore } from "../../../../../../store/simulation/useProductStore";
import StorageAction from '../actions/StorageAction';

function StorageMechanics() {
    const [activeOption, setActiveOption] = useState<"default" | "store" | "spawn">("default");
    const [selectedPointData, setSelectedPointData] = useState<StoragePointSchema | undefined>();
    const { selectedEventData } = useSelectedEventData();
    const { getPointByUuid, updateAction } = useProductStore();
    const { selectedProduct } = useSelectedProduct();

    useEffect(() => {
        if (selectedEventData) {
            const point = getPointByUuid(
                selectedProduct.productId,
                selectedEventData?.data.modelUuid,
                selectedEventData?.selectedPoint
            ) as StoragePointSchema | undefined;
            if (point && 'action' in point) {
                setSelectedPointData(point);
                setActiveOption(point.action.actionType as "store" | "spawn");
            }
        }
    }, [selectedProduct, selectedEventData])

    const handleActionTypeChange = (option: string) => {
        if (!selectedEventData || !selectedPointData) return;
        const validOption = option as "store" | "spawn";
        setActiveOption(validOption);

        updateAction(
            selectedPointData.action.actionUuid,
            { actionType: validOption }
        );
    };

    const handleRenameAction = (newName: string) => {
        if (!selectedPointData) return;
        updateAction(
            selectedPointData.action.actionUuid,
            { actionName: newName }
        );
    };

    const handleCapacityChange = (value: string) => {
        if (!selectedPointData) return;
        updateAction(
            selectedPointData.action.actionUuid,
            { storageCapacity: parseInt(value) }
        );
    };

    // Get current values from store
    const currentActionName = selectedPointData
        ? selectedPointData.action.actionName
        : "Action Name";

    const currentCapacity = selectedPointData
        ? selectedPointData.action.storageCapacity.toString()
        : "0";

    const availableActions = {
        defaultOption: "store",
        options: ["store", "spawn"],
    };

    return (
        <>
            {selectedEventData &&
                <>
                    <div className="selected-actions-details">
                        <div className="selected-actions-header">
                            <RenameInput
                                value={currentActionName}
                                onRename={handleRenameAction}
                            />
                        </div>
                        <div className="selected-actions-list">
                            <LabledDropdown
                                defaultOption={activeOption}
                                options={availableActions.options}
                                onSelect={handleActionTypeChange}
                            />
                            {activeOption === "store" &&
                                <StorageAction
                                    value={currentCapacity}
                                    defaultValue="0"
                                    min={0}
                                    max={20}
                                    onChange={handleCapacityChange}
                                />
                            }
                            {activeOption === "spawn" && (
                                <div className="spawn-options">
                                    <p>Spawn configuration options would go here</p>
                                </div>
                            )}
                        </div>
                    </div>
                    <div className="tirgger">
                        <Trigger />
                    </div>
                </>
            }
        </>
    )
}

export default StorageMechanics