import { createStore } from 'vuex'
import modules from './modules/'
import { useApp, getApp } from '@/app'
const store = createStore({
    state() {
        return {
            mode: 'panorama',
            flying: false,
            floorId: null,
            player: {
                lang: 'zh_HK',
                showVR: false,
                showMore: false,
                showDescription: false,
                showToolbar: true,
                showMap: true,
                showShare: false,
                showUserGuide: false,
                showWidgets: true,
            },
            router: {},
            controlsBottom: '20px',
            shoppingguide: false
        }
    },
    getters: {
        router: state => state.router,
        mode: state => state.mode,
        player: state => state.player,
        flying: state => state.flying,
        floorId: state => state.floorId || 0,
        controlsBottom: state => state.controlsBottom,
        shoppingguide: state => state.shoppingguide
    },
    mutations: {
        showShoppingguide(state, payload) {
            state.shoppingguide = payload
        },
        /**
         * 设置相机模式状态
         * @param {*} state
         * @param {*} payload
         */
        mode(state, payload) {
            state.mode = payload
        },
        /**
         * 设置相机模式
         * @param {*} state
         * @param {*} payload
         */
        setMode(state, payload) {
            if (payload == state.mode) {
                return
            }
            state.flying = true
            useApp().then(sdk => {
                sdk.Camera[payload]()
                    .then(() => {
                        state.flying = false
                    })
                    .catch(() => {
                        state.flying = false
                    })
            })

            if (payload == 'dollhouse') {
                this.commit('setFloor', 'all')
            }

            store.commit('mode', payload)
        },
        setFlying(state, payload) {
            state.flying = payload
        },
        SetPlayerOptions(state, payload) {
            for (let key in payload) {
                state.player[key] = payload[key]
                if (key == 'showSplit') {
                    state.player['showWidgets'] = !payload[key]
                }
            }
            if (!state.player.showToolbar || state.player.showVR) {
                this.commit('tour/setData', { showTours: false })
            }
        },
        /**
         * 跳转楼层
         * @param {*} state
         * @param {*} id
         */
        setFloor(state, id) {
            state.floorId = id
            getApp().Scene.gotoFloor(id)
        },
        /**
         * 这只楼层Id
         * @param {*} state
         * @param {*} id
         */
        setFloorId(state, id) {
            state.floorId = id
        },
        showVR(state) {
            state.player.showVR = !state.player.showVR
            state.player.showWidgets = !state.player.showWidgets
            getApp().Camera.vr()
        },

        showUserGuide(state,payload) {
            state.player.showUserGuide = payload
        },
        
        setControlsBottom(state, payload) {
            state.controlsBottom = payload
        },
        setPanoId(state, id) {
            state.panoId = id
        },
    },
    actions: {},
    modules: {
        ...modules,
    },
})

export default store
