import { create } from 'zustand';
import { immer } from 'zustand/middleware/immer';
import * as THREE from 'three';

interface SelectedEventSphereState {
    selectedEventSphere: THREE.Mesh | null;
    setSelectedEventSphere: (mesh: THREE.Mesh | null) => void;
    clearSelectedEventSphere: () => void;
}

export const useSelectedEventSphere = create<SelectedEventSphereState>()(
    immer((set) => ({
        selectedEventSphere: null,
        setSelectedEventSphere: (mesh) => {
            set((state) => {
                state.selectedEventSphere = mesh;
            });
        },
        clearSelectedEventSphere: () => {
            set((state) => {
                state.selectedEventSphere = null;
            });
        },
    }))
);

interface SelectedEventDataState {
    selectedEventData: { data: EventsSchema; selectedPoint: string } | undefined;
    setSelectedEventData: (data: EventsSchema, selectedPoint: string) => void;
    clearSelectedEventData: () => void;
}

export const useSelectedEventData = create<SelectedEventDataState>()(
    immer((set) => ({
        selectedEventData: undefined,
        setSelectedEventData: (data, selectedPoint) => {
            set((state) => {
                state.selectedEventData = { data, selectedPoint };
            });
        },
        clearSelectedEventData: () => {
            set((state) => {
                state.selectedEventData = undefined;
            });
        },
    }))
);

interface SelectedAssetState {
    selectedAsset: EventsSchema | undefined;
    setSelectedAsset: (EventData: EventsSchema) => void;
    clearSelectedAsset: () => void;
}

export const useSelectedAsset = create<SelectedAssetState>()(
    immer((set) => ({
        selectedAsset: undefined,
        setSelectedAsset: (EventData) => {
            set((state) => {
                state.selectedAsset = EventData;
            });
        },
        clearSelectedAsset: () => {
            set((state) => {
                state.selectedAsset = undefined;
            });
        },
    }))
);

interface SelectedProductState {
    selectedProduct: { productId: string; productName: string };
    setSelectedProduct: (productId: string, productName: string) => void;
    clearSelectedProduct: () => void;
}

export const useSelectedProduct = create<SelectedProductState>()(
    immer((set) => ({
        selectedProduct: { productId: '', productName: '' },
        setSelectedProduct: (productId, productName) => {
            set((state) => {
                state.selectedProduct.productId = productId;
                state.selectedProduct.productName = productName;
            });
        },
        clearSelectedProduct: () => {
            set((state) => {
                state.selectedProduct.productId = '';
                state.selectedProduct.productName = '';
            });
        },
    }))
);

interface SelectedActionState {
    selectedAction: { actionId: string; actionName: string };
    setSelectedAction: (actionId: string, actionName: string) => void;
    clearSelectedAction: () => void;
}

export const useSelectedAction = create<SelectedActionState>()(
    immer((set) => ({
        selectedAction: { actionId: '', actionName: '' },
        setSelectedAction: (actionId, actionName) => {
            set((state) => {
                state.selectedAction.actionId = actionId;
                state.selectedAction.actionName = actionName;
            });
        },
        clearSelectedAction: () => {
            set((state) => {
                state.selectedAction.actionId = '';
                state.selectedAction.actionName = '';
            });
        },
    }))
);