import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default () => new Vuex.Store({
    modules: {
        //
    },
    state: {
        user: {
            UID: null,
            name: 'Guest',
            type: 'guest',
            avatarFile: null,
            is_active_and_visible: true
        },
        session: {
            screenSharingAvailable: false,
            sessionConnected: false,
            screenSharingActive: false
        },
        active_menu_item: {
            name: 'Lobbies',
            class: 'mdi mdi-view-dashboard',
            template: 'lobby-list'
        },
        meeting: {
            id: '',
            name: '',
            passwordRequired: false,
            lobby: null,
            active_members: 0,
            scheduledDate: null,
            startedAt: null
        },
        lobbies: [],
        settings: {
            newMeetingNotificationExpanded: localStorage.getItem('newMeetingNotificationExpanded') == 'true' ? true : false
        }
    },
    mutations: {
        setUsername: (state, data) => {
            state.user.name = data
        },
        setUsertype: (state, data) => {
            state.user.type = data
        },
        setInitialUser(state, data) {
            state.user = data
        },
        setUser(state, data) {
            state.user = data
        },
        setUserAvatarFile(state, data) {
            state.user.avatarFile = data
        },
        setUserActivityState: (state, data) => {
            state.user.is_active_and_visible = data
        },
        setScreenShareState: (state, data) => {
            state.session.screenSharingActive = data
        },
        setLobbies(state, data) {
            state.lobbies = data
        },
        addNewLobby(state, data) {
            state.lobbies.push(data)
        },
        setSingleLobby(state, data) {
            let lobby = state.lobbies.findIndex((cur) => cur.id == data.id)

            if (lobby !== -1) {
                state.lobbies[lobby].meetings = data.meetings.map(m => {
                    m.active_members = []
                    m.pros_online = []
                    return m
                });
            }
        },
        setLobbyActivity(state, data) {
            let lobby = state.lobbies.filter((cur) => cur.id == data.lobbyID)
            if (lobby.length) {
                let meeting = lobby[0].meetings.filter((cur) => cur.id == data.meetingID)
                if (meeting.length) {
                    meeting[0].active_members = data.active_members
                    meeting[0].pros_online = data.pros_online

                    if (state.meeting.id && state.meeting.id == meeting[0].id) {
                        state.meeting.active_members = data.active_members
                    }
                }
            }

        },
        setNewMeetingInLobby(state, data) {
            let lobby = state.lobbies.filter((cur) => cur.id == parseInt(data.lobby.id))

            if (lobby.length) {
                data.finalDate = new Date(data.scheduled_date ? data.scheduled_date : data.created_at)
                lobby[0].meetings.push(data)
                lobby[0].meetings.sort((a, b) => {
                    return a.finalDate - b.finalDate
                })
            }
        },
        setMeeting(state, data) {
            state.meeting = data
        },
        setScreesharingAvailability(state, data) {
            state.session.screenSharingAvailable = data
        },
        setSessionConnectivityState(state, data) {
            state.session.sessionConnected = data
        },
        updateSettings(state, data) {
            state.settings = data
        }
    },
    actions: {
        leaveMeeting(store) {
            let meeting = {
                id: '',
                name: '',
                passwordRequired: false,
                lobby: null,
                active_members: 0,
                scheduledDate: null,
                startedAt: null
            }

            store.commit('setMeeting', meeting)
        }
    }
})