import { defineStore } from 'pinia';
import { getApp } from '/@/hooks/userApp';

export interface FloorsType {
    id: string,
    subgroup?: string,
    name: string
    segment?: any[]
    tagging?: any[]
    ['vertex-xy']?: any[]
}
interface SceneState {
    tags: any[],
    floors: FloorsType[],
    metadata: KankanMetaDataType
}

export const useSceneStore = defineStore({
    id: 'scene',
    state: (): SceneState => ({
        tags: [],
        floors: [],
        metadata: {} as KankanMetaDataType
    }),
    getters: {
        musicURL(): Nullable<string> {
            const musicURl = this.metadata.music
            if (musicURl) {
                if (/^0\d$/.test(musicURl)) {
                    return (window as any).resource.getStaticURL(`static/music/${musicURl}.mp3`)
                } else {
                    const app = getApp();
                    return app.resource.getUserResourceURL(this.metadata.musicFile.replace(/(.+)\.(.+)/, 'music-user.$2'))
                }
            }
            return null
        },
        loadingLogoFile(): Nullable<string> {
            if (this.metadata.loadingLogo == 'user') {
                const app = getApp();
                return app.resource.getUserResourceURL(this.metadata.loadingLogoFile)
            }
            return null
        }

    },
    actions: {
        load(metadata: KankanMetaDataType): void {
            this.metadata = metadata
            document.title = metadata.title
        },
        loadFloorData(floors: FloorsType[]) {
            if (floors?.length) {
                this.floors = floors.map(item => {
                    return { id: item.subgroup, name: item.name }
                }) as FloorsType[]
            }

        },
    }

})