/*
 * @Description: In User Settings Edit
 * @Author: your name
 * @Date: 2019-09-24 12:34:06
 * @LastEditTime: 2019-09-24 12:34:06
 * @LastEditors: your name
 */
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import FastClick from 'fastclick'
import router from './router'
import App from './App'
import Vuex from 'vuex'
import vuexI18n from 'vuex-i18n'

import flexible from './common/ydui.flexible'
import components from './components/component'
import {componentInstall, appStyle} from 'hls-easy-ui'
/**
 * 指令
 */
import directives from './scripts/directives'

import filter from './scripts/filter'

import './scripts/prototype'
/**
 * 组件
 */
// import component from './components/component.js'

import {
  Tabbar,
  TabbarItem,
} from 'vux'

/**
 * 弹框组件
 */
import hlsPopup from './scripts/hlsPopup'

/**
 * http
 */
import {post, get} from './scripts/hlsHttp'

/**
 * PDFJS
 */
import PDFJS from 'pdfjs-dist'

/** 全局函数hlsUtil**/

import hlsUtil from './scripts/hlsUtil'

/** end**/

/**
 * jpush jmessage
 */
import Jpush from './scripts/jpushService'

import Jmessage from './scripts/jmessageService'
// || process.env.CONFIG_ENV === 'prod'
if (process.env.CONFIG_ENV === 'uat') {
  const VConsole = require('vconsole')
  new VConsole() // eslint-disable-line
}
Vue.use(componentInstall)
Vue.use(components)
Vue.use(appStyle)
Vue.use(flexible)
Vue.use(directives)

Vue.use(filter)

// Vue.use(component)

Vue.use(Vuex)
Vue.component('tabbar', Tabbar)
Vue.component('tabbar-item', TabbarItem)

/** i18n **/
let store = new Vuex.Store({
  modules: {
    i18n: vuexI18n.store,
  },
})
Vue.use(vuexI18n.plugin, store)

Vue.prototype.hlsPopup = window.hlsPopup = hlsPopup
Vue.prototype.$devicePixelRatio = 2

Vue.prototype.$post = post
Vue.prototype.$get = get
let hlsHttp = {
  get: get,
  post: post,
}
Vue.prototype.hlsHttp = window.hlsHttp = hlsHttp

Vue.prototype.PDFJS = window.PDFJS = PDFJS

Vue.prototype.hlsUtil = window.hlsUtil = hlsUtil

Vue.prototype.Jpush = window.Jpush = Jpush

Vue.prototype.Jmessage = window.Jmessage = Jmessage

/**
 * 全局返回上一页面
 * @param index
 */
let routeGo = function (index) {
  if (!index) {
    index = -1
  }
  this.$router.go(index)
}
Vue.prototype.$routeGo = routeGo

FastClick.attach(document.body)

Vue.config.productionTip = false

let backButtonPressedOnceToExit

vum.$vumPlatform.ready(function () {
  if ((vum.Platform.isAndroid()) || (vum.Platform.isIOS())) {
    /* window.getRegistrationID = function (callback){
      callback('')
    } */
    try {
      setTimeout(function () {
        navigator.splashscreen.hide()
        if (window.plugins.jPushPlugin) {
          Jpush._init()
        }
        if (window.JMessagePlugin) {
          Jmessage._init({'isOpenMessageRoaming': true})
        }
      }, 40)
    } catch (e) {
    }
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true)
      cordova.plugins.Keyboard.disableScroll(true)
    }

    if (window.StatusBar) {
      window.StatusBar.styleDefault()
      window.StatusBar.backgroundColorByName('white')
    }

    const winSize = vum.$vumPlatform.getWinSize()
    if (winSize.width) {
      window.localStorage.setItem('width', winSize.width)
    }
    if (winSize.height) {
      window.localStorage.setItem('height', winSize.height)
    }
  }
})

vum.$vumPlatform.registerBackButtonAction(function (e) {
  // let vm = this
  let time
  let path = router.currentRoute.path

  if (path === '/tab/home' || path === '/tab/message' || path === '/tab/my-info') {
    // 进入主界面清除缓存
    if (backButtonPressedOnceToExit === true) {
      vum.Platform.exitApp()
    } else {
      backButtonPressedOnceToExit = true
      hlsPopup.showLongBottom('再次点击返回键退出应用')
      clearTimeout(time)
      time = setTimeout(function () {
        backButtonPressedOnceToExit = false
      }, 1500)
    }
  } else if (path === '/login' || path === '/finger-login' || path === '/gesture') {
    vum.Platform.exitApp()
  } else {
    router.go(-1)
  }
  e.preventDefault()
  return false
}, 101)
// input遮挡
const u = navigator.userAgent
if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { // 安卓手机
  window.addEventListener('resize', function () {
    // Document 对象的activeElement 属性返回文档中当前获得焦点的元素。
    if (document.activeElement.tagName === 'INPUT' || document.activeElement.tagName === 'TEXTAREA') {
      console.log('安卓触发', document.activeElement.tagName)
      window.setTimeout(function () {
        document.activeElement.scrollIntoView(false)
      }, 50)
    }
  })
}
/* eslint-disable no-new */
new Vue({
  data () {
    return {
      pathList: [],
      transitionName: null,
    }
  },
  router,
  watch: { // 监听路由变化
    $route (to, from) {
      document.body.scrollTop = 0
      document.documentElement.scrollTop = 0
    },
  },
  render: h => h(App),
}).$mount('#app-box')
