import { createApp } from 'vue';
import App from './App.vue';
import './registerServiceWorker';
import router from './router';
import store from './store';
import Vant from 'vant';
import { ConfigProvider } from 'vant';
import 'vant/lib/index.css';
import './styles/app.less';
import './styles/font.less';
import http from './plugins/http';
import colors from './plugins/colors';
// import ElementUI from 'element-ui';
// import 'element-ui/lib/theme-chalk/index.css';
import PageTitle from './components/PageTitle';
import LikeButton from './components/LikeButton.vue';
import Driver from './components/Driver.vue';
import common from './mixins/common';
import VueClipboard from 'vue-clipboard2';
import queryString from 'query-string';
import PageBar from './components/PageBar';
import eruda from 'eruda';

let showConsole = localStorage.getItem('showConsole');
if (showConsole && parseInt(showConsole) > new Date().getTime()) {
    eruda.init();
}
store.commit('setFirstUrl', location.href);

import ImgContent from './components/ImgContent.vue';

createApp(App)
    .use(Vant)
    .use(http)
    .use(colors)
    // .use(ElementUI)
    .use(ConfigProvider)
    .use(VueClipboard)
    .mixin(common)
    .component('page-title', PageTitle)
    .component('like-button', LikeButton)
    .component('driver', Driver)
    .component('van-image', ImgContent)
    .component('page-bar', PageBar)
    .use(store)
    .use(router)
    .mount('#app');

let query = queryString.parse(location.search);
if (query.code) {
    http.http.post('/user/code2openId', { code: query.code }).then(res => {
        localStorage.setItem('openId', res);
    });
} else {
    if (/micromessenger/i.test(navigator.userAgent) && !/localhost|(192\.168)/i.test(location.host)) {
        document.location.replace('https://nft.9space.vip/wx/redirect?redirectUrl=' + location.href);
    }
}
if (query.invitor) {
    store.commit('setInvitor', query.invitor);
}

if (window.cordova) {
    document.addEventListener(
        'deviceready',
        function () {
            window.codePush.sync(
                function (syncStatus) {
                    switch (syncStatus) {
                        // Result (final) statuses
                        case SyncStatus.UPDATE_INSTALLED:
                            console.log(
                                'The update was installed successfully. For InstallMode.ON_NEXT_RESTART, the changes will be visible after application restart. '
                            );
                            break;
                        case SyncStatus.UP_TO_DATE:
                            console.log('The application is up to date.');
                            break;
                        case SyncStatus.UPDATE_IGNORED:
                            console.log('The user decided not to install the optional update.');
                            break;
                        case SyncStatus.ERROR:
                            console.log('An error occured while checking for updates');
                            break;

                        // Intermediate (non final) statuses
                        case SyncStatus.CHECKING_FOR_UPDATE:
                            console.log('Checking for update.');
                            break;
                        case SyncStatus.AWAITING_USER_ACTION:
                            console.log('Alerting user.');
                            break;
                        case SyncStatus.DOWNLOADING_PACKAGE:
                            console.log('Downloading package.');
                            break;
                        case SyncStatus.INSTALLING_UPDATE:
                            console.log('Installing update');
                            break;
                    }
                },
                {
                    installMode: InstallMode.IMMEDIATE,
                    updateDialog: false
                },
                function (downloadProgress) {
                    console.log(
                        'Downloading ' +
                            downloadProgress.receivedBytes +
                            ' of ' +
                            downloadProgress.totalBytes +
                            ' bytes.'
                    );
                }
            );
        },
        false
    );
}
