import Vue from 'vue'
import Vuex from 'vuex'
import persistedState from 'vuex-persistedstate'
import getters from './getters'
import user from './modules/user'
import my_content from '@/store/modules/my_content'
import image_zoom from '@/store/modules/image_zoom'

Vue.use(Vuex)

// Vuex 中的数据存储在内存中，页面刷新会使数据丢失，使用 persistedState 将其存储在 localStorage 而非内存中
const store = new Vuex.Store({
  state: {
    webInfo: {
      name: 'bili',
      openNoVipLimit: 1,
      noVipViewCount: 5,
      logoUrl: '/logo.png',
      openInvitationRegister: 1,
      describe: '一个综合社区',
      openUploadVideoAddViewCount: 1,
      openExamine: 1,
      id: 1,
      createTime: 0
    },
    darkThemOpen: false,
    userInfo: (localStorage.getItem('user') != null &&
        localStorage.getItem('user') !== 'undefined' &&
        localStorage.getItem('user') !== '') ? JSON.parse(localStorage.getItem('user')) : null,
    uploadVideoDateTemp: {}
  },
  mutations: {
    setUserInfo(state, userInfo) {
      // 将传递的数据先保存到 localStorage 中
      localStorage.setItem('user', JSON.stringify(userInfo))
      // 之后才是修改state中的状态
      state.userInfo = userInfo
    },
    setUserToken(state, token) {
      // 将传递的数据先保存到 localStorage 中
      // localStorage.setItem('user', JSON.stringify(userInfo))
      // 之后才是修改state中的状态
      state.token = token
    },
    setWebInfo(state, webInfo) {
      localStorage.setItem('webInfo', JSON.stringify(webInfo))
      state.userInfo = webInfo
    }
  },
  actions: {
  },
  modules: {
    user,
    my_content,
    image_zoom
  },
  getters,
  plugins: [persistedState()]
})

export default store
