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

interface FloorStore {
    floors: Floor[];
    setFloors: (floors: Floor[]) => void;
    addFloor: (floor: Floor) => void;
    updateFloor: (uuid: string, updated: Partial<Floor>) => void;
    removeFloor: (uuid: string) => void;
    removePointFromFloors: (pointUuid: string) => void;
    clearFloors: () => void;
    setIsBeveled: (uuid: string, isBeveled: boolean) => void;
    setBevelStrength: (uuid: string, strength: number) => void;
    setDepth: (uuid: string, depth: number) => void;
    setMaterial: (uuid: string, sideMaterial: string, topMaterial: string) => void;

    getFloorById: (uuid: string) => Floor | undefined;
}

export const createFloorStore = () => {
    return create<FloorStore>()(
        immer((set, get) => ({
            floors: [],

            setFloors: (floors) => set(state => {
                state.floors = floors;
            }),

            addFloor: (floor) => set(state => {
                state.floors.push(floor);
            }),

            updateFloor: (uuid, updated) => set(state => {
                const floor = state.floors.find(f => f.floorUuid === uuid);
                if (floor) {
                    Object.assign(floor, updated);
                }
            }),

            removeFloor: (uuid) => set(state => {
                state.floors = state.floors.filter(f => f.floorUuid !== uuid);
            }),

            removePointFromFloors: (pointUuid) => set(state => {
                for (const floor of state.floors) {
                    floor.points = floor.points.filter(p => p.pointUuid !== pointUuid);
                }
            }),

            clearFloors: () => set(state => {
                state.floors = [];
            }),

            setIsBeveled: (uuid, isBeveled) => set(state => {
                const floor = state.floors.find(f => f.floorUuid === uuid);
                if (floor) {
                    floor.isBeveled = isBeveled;
                }
            }),

            setBevelStrength: (uuid, strength) => set(state => {
                const floor = state.floors.find(f => f.floorUuid === uuid);
                if (floor) {
                    floor.bevelStrength = strength;
                }
            }),

            setDepth: (uuid, depth) => set(state => {
                const floor = state.floors.find(f => f.floorUuid === uuid);
                if (floor) {
                    floor.floorDepth = depth;
                }
            }),

            setMaterial: (uuid, sideMaterial, topMaterial) => set(state => {
                const floor = state.floors.find(f => f.floorUuid === uuid);
                if (floor) {
                    floor.sideMaterial = sideMaterial;
                    floor.topMaterial = topMaterial;
                }
            }),

            getFloorById: (uuid) => {
                return get().floors.find(f => f.floorUuid === uuid);
            },
        }))
    );
};

export type FloorStoreType = ReturnType<typeof createFloorStore>;