import { createApp, ref } from 'vue'
import { createPinia } from 'pinia'
import App from './App.vue'
import router from './router'
import http from '@/plugins/http'
import { IonicVue } from '@ionic/vue'
import * as IonComponents from '@ionic/vue'
import i18n from './locales'
import Vant from 'vant'
import toast from '@/utils/toast'
import setDefaultOptions from 'date-fns/setDefaultOptions'
import zhTW from 'vant/es/locale/lang/zh-TW'
import { StatusBar, Style } from '@capacitor/status-bar'
import { Capacitor } from '@capacitor/core'
import { useBackButton } from '@ionic/vue'
import { SplashScreen } from '@capacitor/splash-screen'
// import { Openinstall } from 'capacitor-openinstall'
import { Locale as VantLocale, Lazyload } from 'vant'
import vantEnUS from 'vant/es/locale/lang/en-US'
import { useStorage } from '@vueuse/core'
import { init as initEruda } from '@/utils/console'
import { Network } from '@capacitor/network'
import { SafeArea } from 'capacitor-plugin-safe-area'
import qs from 'qs'
import { App as AppPlugin } from '@capacitor/app'
import { emitter } from '@/utils/eventBus'
import { checkUpdate } from '@/plugins/updater'

import 'normalize.css/normalize.css'

/* Core CSS required for Ionic components to work properly */
import '@ionic/vue/css/core.css'

/* Basic CSS for apps built with Ionic */
import '@ionic/vue/css/normalize.css'
import '@ionic/vue/css/structure.css'
import '@ionic/vue/css/typography.css'

/* Optional CSS utils that can be commented out */
import '@ionic/vue/css/padding.css'
import '@ionic/vue/css/float-elements.css'
import '@ionic/vue/css/text-alignment.css'
import '@ionic/vue/css/text-transformation.css'
import '@ionic/vue/css/flex-utils.css'
import '@ionic/vue/css/display.css'

import 'vant/lib/index.css'
import './styles/main.less'
import './styles/fonts.less'
import './styles/theme/variables.less'
import './styles/tailwind.css'

import common from './mixins/common'

const invitor = useStorage('invitor', null, localStorage)
const urlParams = qs.parse(location.search.slice(1))
console.log('urlParams', urlParams)
if (urlParams.invitor) {
    invitor.value = urlParams.invitor
}

const app = createApp(App)
app.use(i18n)
app.use(IonicVue)
app.use(router)
app.use(createPinia())
app.use(http)
VantLocale.use('zh-TW', zhTW)
app.use(Vant)
app.use(toast)
app.mixin(common)

app.use(Lazyload)

// ionic components
Object.keys(IonComponents).forEach(key => {
    if (/^Ion[A-Z]\w+$/.test(key)) {
        app.component(key, IonComponents[key])
    }
})

router.isReady().then(() => {
    app.mount('#app')
})
// if (navigator.language === 'zh-CN') {
//     setDefaultOptions({ locale: enUS })
// } else {
//     setDefaultOptions({ locale: enUS })
// }
const firstRun = useStorage('firstRun', true)

const initOpeninstall = () => {
    // Openinstall.init()
    // const handleData = data => {
    //     if (data && data.data) {
    //         let params = JSON.parse(data.data)
    //         if (params.invitor) {
    //             localStorage.setItem('invitor', params.invitor)
    //             invitor.value = params.invitor
    //         }
    //     }
    // }
    // Openinstall.addListener('wakeUp', data => {
    //     console.log('wakeUp data:', data)
    // })
    // Openinstall.getInstallCanRetry(data => {
    //     console.log('install data:', data)
    //     handleData(data)
    // })
    // Network.addListener('networkStatusChange', status => {
    //     if (status.connected) {
    //         //checkUpdate()
    //     }
    // })
}
if (Capacitor.isNativePlatform()) {
    StatusBar.setStyle({ style: Style.Dark })
    if (Capacitor.getPlatform() === 'android') {
        StatusBar.setOverlaysWebView({ overlay: true })
        const style = document.documentElement.style
        if (Capacitor.isPluginAvailable('SafeArea')) {
            SafeArea.getSafeAreaInsets().then(({ insets }) => {
                style.setProperty('--ion-safe-area-top', insets.top + 'px')
                // style.setProperty('--ion-safe-area-bottom', insets.bottom + 'px')
            })
        }
    }
    // initAF()
    Network.getStatus().then(status => {
        console.log('networkStatus:', status)
        if (status.connected) {
            checkUpdate()
        }
    })
}

if (useStorage('showConsole', 0).value > new Date().getTime()) {
    initEruda()
}
if (location.origin.includes('http://192.168')) {
    initEruda()
}

firstRun.value = false
