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

interface WallAssetStore {
    wallAssets: WallAsset[];
    setWallAssets: (assets: WallAsset[]) => void;
    addWallAsset: (asset: WallAsset) => void;
    updateWallAsset: (uuid: string, updated: Partial<WallAsset>) => WallAsset | undefined;
    setWallAssetPosition: (uuid: string, position: [number, number, number]) => void;
    removeWallAsset: (uuid: string) => WallAsset | undefined;
    clearWallAssets: () => void;

    setVisibility: (uuid: string, isVisible: boolean) => void;
    setLock: (uuid: string, isLocked: boolean) => void;
    setOpacity: (uuid: string, opacity: number) => void;

    getWallAssetById: (uuid: string) => WallAsset | undefined;
    getAssetsByWall: (wallUuid: string) => WallAsset[];
}

export const createWallAssetStore = () => {
    return create<WallAssetStore>()(
        immer((set, get) => ({
            wallAssets: [],

            setWallAssets: (assets) => set(state => {
                state.wallAssets = assets;
            }),

            addWallAsset: (asset) => set(state => {
                state.wallAssets.push(asset);
            }),

            updateWallAsset: (uuid, updated) => {
                let updatedWallAsset: WallAsset | undefined;
                set(state => {
                    const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                    if (asset) {
                        Object.assign(asset, updated);
                        updatedWallAsset = JSON.parse(JSON.stringify(asset));
                    }
                });
                return updatedWallAsset;
            },

            setWallAssetPosition: (uuid, position) => set(state => {
                const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                if (asset) {
                    asset.position = position;
                }
            }),

            removeWallAsset: (uuid) => {
                let removedAsset: WallAsset | undefined;
                set(state => {
                    const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                    if (asset) {
                        removedAsset = JSON.parse(JSON.stringify(asset));
                        state.wallAssets = state.wallAssets.filter(a => a.modelUuid !== uuid);
                    }
                });
                return removedAsset;
            },

            clearWallAssets: () => {
                set(state => {
                    state.wallAssets = [];
                })
            },

            setVisibility: (uuid, isVisible) => set(state => {
                const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                if (asset) {
                    asset.isVisible = isVisible;
                }
            }),

            setLock: (uuid, isLocked) => set(state => {
                const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                if (asset) {
                    asset.isLocked = isLocked;
                }
            }),

            setOpacity: (uuid, opacity) => set(state => {
                const asset = state.wallAssets.find(a => a.modelUuid === uuid);
                if (asset) {
                    asset.opacity = opacity;
                }
            }),

            getWallAssetById: (uuid) => {
                return get().wallAssets.find(a => a.modelUuid === uuid);
            },

            getAssetsByWall: (wallUuid) => {
                return get().wallAssets.filter(a => a.wallUuid === wallUuid);
            },
        }))
    );
};

export type WallAssetStoreType = ReturnType<typeof createWallAssetStore>;