import { create } from "zustand";
import { immer } from "zustand/middleware/immer";

interface BakedPoint {
    x: number;
    y: number;
}

interface BakedPointImage {
    type: string;
    points: BakedPoint;
}

interface HeatMapState {
    monitoringVehicle: string[];
    monitoringHuman: string[];
    bakedPoints: BakedPointImage[];

    // Vehicle monitoring
    setMonitoringVehicle: (vehiclesUuid: string[]) => void;
    addMonitoringVehicle: (vehicleUuid: string) => void;
    removeMonitoringVehicle: (vehicleUuid: string) => void;
    clearMonitoringVehicle: () => void;

    // Human monitoring
    setMonitoringHuman: (humansUuid: string[]) => void;
    addMonitoringHuman: (humanUuid: string) => void;
    removeMonitoringHuman: (humanUuid: string) => void;
    clearMonitoringHuman: () => void;

    // Clear both monitors
    clearAllMonitors: () => void;

    // Baked points
    setBakedPoints: (points: BakedPointImage[]) => void;
    addBakedPoint: (point: BakedPointImage) => void;
    clearBakedPoints: () => void;

    // Utility checkers
    hasVehicle: (vehicleUuid: string) => boolean;
    hasHuman: (humanUuid: string) => boolean;
}

export const useHeatMapStore = create<HeatMapState>()(
    immer((set, get) => ({
        monitoringVehicle: [],
        monitoringHuman: [],
        bakedPoints: [],

        // Vehicles
        setMonitoringVehicle: (vehiclesUuid) =>
            set((state) => {
                state.monitoringVehicle = vehiclesUuid;
            }),

        addMonitoringVehicle: (vehicleUuid) =>
            set((state) => {
                if (!state.monitoringVehicle.includes(vehicleUuid)) {
                    state.monitoringVehicle.push(vehicleUuid);
                }
            }),

        removeMonitoringVehicle: (vehicleUuid) =>
            set((state) => {
                state.monitoringVehicle = state.monitoringVehicle.filter((v) => v !== vehicleUuid);
            }),

        clearMonitoringVehicle: () =>
            set((state) => {
                state.monitoringVehicle = [];
            }),

        // Humans
        setMonitoringHuman: (humansUuid) =>
            set((state) => {
                state.monitoringHuman = humansUuid;
            }),

        addMonitoringHuman: (humanUuid) =>
            set((state) => {
                if (!state.monitoringHuman.includes(humanUuid)) {
                    state.monitoringHuman.push(humanUuid);
                }
            }),

        removeMonitoringHuman: (humanUuid) =>
            set((state) => {
                state.monitoringHuman = state.monitoringHuman.filter((h) => h !== humanUuid);
            }),

        clearMonitoringHuman: () =>
            set((state) => {
                state.monitoringHuman = [];
            }),

        // Clear all
        clearAllMonitors: () =>
            set((state) => {
                state.monitoringVehicle = [];
                state.monitoringHuman = [];
            }),

        // Baked Points
        setBakedPoints: (points) =>
            set((state) => {
                state.bakedPoints = points;
            }),

        addBakedPoint: (point) =>
            set((state) => {
                state.bakedPoints.push(point);
            }),

        clearBakedPoints: () =>
            set((state) => {
                state.bakedPoints = [];
            }),

        // Utility checkers
        hasVehicle: (vehicleUuid) => get().monitoringVehicle.includes(vehicleUuid),
        hasHuman: (humanUuid) => get().monitoringHuman.includes(humanUuid),
    }))
);

//    useEffect(() => {
//         const selectedProductData = getProductById(selectedProduct.productUuid);
//         const newEvents: EventsSchema[] = [];

//         if (selectedProductData) {
//             determineExecutionMachineSequences([selectedProductData]).then((sequences) => {
//                 sequences.forEach((sequence) => {
//                     sequence.forEach((event) => {
//                         if (event.type === "human" || event.type === "vehicle") {
//                             newEvents.push(event);
//                         }
//                     });
//                 });
//                 setEvents(newEvents);
//             });
//         }
//     }, [selectedProduct, products, monitoringHuman, monitoringVehicle]);
