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

interface ArmBotStore {
    armBots: ArmBotStatus[];

    addArmBot: (productId: string, event: RoboticArmEventSchema) => void;
    removeArmBot: (modelUuid: string) => void;
    updateArmBot: (
        modelUuid: string,
        updates: Partial<Omit<ArmBotStatus, 'modelUuid' | 'productId'>>
    ) => void;

    addCurrentAction: (modelUuid: string, actionUuid: string) => void;
    removeCurrentAction: (modelUuid: string) => void;

    addAction: (modelUuid: string, action: RoboticArmPointSchema['actions'][number]) => void;
    removeAction: (modelUuid: string, actionUuid: string) => void;

    updateStartPoint: (modelUuid: string, actionUuid: string, startPoint: [number, number, number] | null) => void;
    updateEndPoint: (modelUuid: string, actionUuid: string, endPoint: [number, number, number] | null) => void;

    setArmBotActive: (modelUuid: string, isActive: boolean) => void;

    incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
    incrementIdleTime: (modelUuid: string, incrementBy: number) => void;

    getArmBotById: (modelUuid: string) => ArmBotStatus | undefined;
    getArmBotsByProduct: (productId: string) => ArmBotStatus[];
    getArmBotsByState: (state: string) => ArmBotStatus[];
    getActiveArmBots: () => ArmBotStatus[];
    getIdleArmBots: () => ArmBotStatus[];
    getArmBotsByCurrentAction: (actionUuid: string) => ArmBotStatus[];
}

export const useArmBotStore = create<ArmBotStore>()(
    immer((set, get) => ({
        armBots: [],

        addArmBot: (productId, event) => {
            set((state) => {
                state.armBots.push({
                    ...event,
                    productId,
                    isActive: false,
                    idleTime: 0,
                    activeTime: 0,
                    state: 'idle',
                });
            });
        },

        removeArmBot: (modelUuid) => {
            set((state) => {
                state.armBots = state.armBots.filter(a => a.modelUuid !== modelUuid);
            });
        },

        updateArmBot: (modelUuid, updates) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    Object.assign(armBot, updates);
                }
            });
        },

        addCurrentAction: (modelUuid, actionUuid) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    const action = armBot.point.actions.find(a => a.actionUuid === actionUuid);
                    if (action) {
                        armBot.currentAction = {
                            actionUuid: action.actionUuid,
                            actionName: action.actionName,
                        };
                        armBot.isActive = true;
                    }
                }
            });
        },

        removeCurrentAction: (modelUuid) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.currentAction = undefined;
                    armBot.isActive = false;
                }
            });
        },

        addAction: (modelUuid, action) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.point.actions.push(action);
                }
            });
        },

        removeAction: (modelUuid, actionUuid) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.point.actions = armBot.point.actions.filter(a => a.actionUuid !== actionUuid);
                }
            });
        },

        updateStartPoint: (modelUuid, actionUuid, startPoint) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    const action = armBot.point.actions.find(a => a.actionUuid === actionUuid);
                    if (action) {
                        action.process.startPoint = startPoint;
                    }
                }
            });
        },

        updateEndPoint: (modelUuid, actionUuid, endPoint) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    const action = armBot.point.actions.find(a => a.actionUuid === actionUuid);
                    if (action) {
                        action.process.endPoint = endPoint;
                    }
                }
            });
        },

        setArmBotActive: (modelUuid, isActive) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.isActive = isActive;
                }
            });
        },

        incrementActiveTime: (modelUuid, incrementBy) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.activeTime += incrementBy;
                }
            });
        },

        incrementIdleTime: (modelUuid, incrementBy) => {
            set((state) => {
                const armBot = state.armBots.find(a => a.modelUuid === modelUuid);
                if (armBot) {
                    armBot.idleTime += incrementBy;
                }
            });
        },

        getArmBotById: (modelUuid) => {
            return get().armBots.find(a => a.modelUuid === modelUuid);
        },

        getArmBotsByProduct: (productId) => {
            return get().armBots.filter(a => a.productId === productId);
        },

        getArmBotsByState: (state) => {
            return get().armBots.filter(a => a.state === state);
        },

        getActiveArmBots: () => {
            return get().armBots.filter(a => a.isActive);
        },

        getIdleArmBots: () => {
            return get().armBots.filter(a => !a.isActive && a.state === 'idle');
        },

        getArmBotsByCurrentAction: (actionUuid) => {
            return get().armBots.filter(a => a.currentAction?.actionUuid === actionUuid);
        }
    }))
);
