import Vue from 'vue'
import { Component } from 'vue-property-decorator'
import { UserStore } from '@store'
import router from './router'

import { Header } from './components/Header'

import globalroutines from './globalroutines/index'
import { GlobalStore } from './store/Modules'
import { toolsext } from '@src/store/Modules/toolsext'
import { BannerCookies, CPreloadImages, CTesseraElettronica } from '@components'
import { static_data } from '@src/db/static_data'

@Component({
  components: {
    appHeader: Header,
    BannerCookies, CPreloadImages, CTesseraElettronica
  },
  router
})

export default class App extends Vue {
  public backgroundColor = 'whitesmoke'
  public $q

  public listaRoutingNoLogin = ['/vreg?', '/offline']

  public async created() {

    if (process.env.DEV) {
      console.info('SESSIONE IN SVILUPPO ! (DEV)')
      console.info(process.env)
    }
    if (process.env.TEST) {
      console.info('SESSIONE IN TEST ! (TEST)')
      console.info(process.env)
    }
    console.info('Vers:', process.env.APP_VERSION)

    // Make autologin only if some routing

    // console.log('window.location.href', window.location.href)

    let chiamaautologin = true
    this.listaRoutingNoLogin.forEach((mystr) => {
      if (window.location.href.includes(mystr)) {
        chiamaautologin = false
      }
    })

    if (chiamaautologin) {
      // console.log('CHIAMA autologin_FromLocalStorage')
      await UserStore.actions.autologin_FromLocalStorage()
        .then((loadstorage) => {
          if (loadstorage) {

            // Added others routes

            if (toolsext.getLocale() !== '') {
              // console.log('SETLOCALE :', this.$i18n.locale)
              this.$i18n.locale = toolsext.getLocale()    // Set Lang
            } else {
              UserStore.mutations.setlang(this.$i18n.locale)
            }
            // console.log('lang CARICATO:', this.$i18n.locale)

            globalroutines(this, 'loadapp', '')
            // this.$router.replace('/')

            // Create Subscription to Push Notification
            GlobalStore.actions.createPushSubscription()
          }
        })
    } else {
      GlobalStore.state.finishLoading = true
    }

    // Calling the Server for updates ?
    // Check the verified_email

  }

  get finishLoading() {
    return GlobalStore.state.finishLoading
  }

  get static_data() {
    return static_data
  }
}
