import Vue from 'vue'
import App from './App.vue'
import store from '@/store'
import { pcRouter } from '@/router'

import Invite from '@/components/popup/invite'
import Avatar from '@/components/popup/avatar'
import Info from '@/components/popup/userInfo'
import otherInfo from '@/components/popup/otherInfo'
import imgPreview from '@/components/popup/imgPreview'
import packetGet from '@/components/popup/packetGet'
import packetSend from '@/components/popup/packetSend'
import Toast from '@/components/popup/toast'
import VueClipboard from 'vue-clipboard2'
import directive from '@/plugins/directive'
import TWEEN from '@tweenjs/tween.js'
import { register } from 'register-service-worker'
import { MessageBox } from 'element-ui'

Vue.use(Invite, store)
Vue.use(Avatar, store)
Vue.use(Info, store)
Vue.use(otherInfo, store, pcRouter)
Vue.use(packetGet, store)
Vue.use(packetSend, store)
Vue.use(Toast)
Vue.use(VueClipboard)
Vue.use(directive)
Vue.use(imgPreview)

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  store,
  router: pcRouter
}).$mount('#app')

// Setup the animation loop.
function animate (time) {
  requestAnimationFrame(animate)
  TWEEN.update(time)
}
requestAnimationFrame(animate)

if (process.env.NODE_ENV === 'production') {
  let updatefound = false
  register('/sw.js', {
    ready () {
      console.log(
        'App is being served from cache by a service worker.\n' +
        'For more details, visit https://goo.gl/AFskqB'
      )
    },
    registered () {
      console.log('Service worker has been registered.')
    },
    cached () {
      console.log('Content has been cached for offline use.')
    },
    updatefound () {
      updatefound = true
      console.log('New content is downloading.')
    },
    updated () {
      if (updatefound) {
        MessageBox.confirm('新的内容已更新完毕，立即刷新页面？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(() => {
          location.reload() && window.location.reload()
        }).catch(() => {})
      }
    },
    offline () {
      console.log('No internet connection found. App is running in offline mode.')
    },
    error (error) {
      console.error('Error during service worker registration:', error)
    }
  })
}
