<script>
import {
    useAppStore
} from '@/store/useAppStore.js'
import {
    useLangStore
} from '@/store/useLangStore.js'
import {
    useUserStore
} from '@/store/useUserStore.js'
import {
    useThemeStore
} from '@/store/useThemeStore.js'
import {
    useI18n
} from 'vue-i18n'
// #ifdef APP-PLUS
import { getAdid } from '@/module/utils/util.js'
import { getAndroidId } from "@/uni_modules/x-uniq-id"
// #endif
import md5 from "crypto-js/md5"
export default {
    onLaunch: function (options) {
        // #ifdef APP-PLUS
        plus.navigator.hideSystemNavigation()
        // #endif
        console.log('App Launch', options)
        console.log('Process.env', process.env)
        this.handleAppLaunch(options);
    },
    onShow: function (options) {
        console.log('App Show', options)
        uni.onWindowResize(this.handleWindowResize)
    },
    onHide: function () {
        console.log('App Hide')
        uni.offWindowResize(this.handleWindowResize)
    },
    methods: {
        handleAppLaunch(options) {
            this.appDataInit(options)
            // this.setupTabbar()
            this.setupTheme()

            // setInterval(() => {
            // 	uni.getNetworkType({
            // 		success: (res) => {
            // 			uni.showToast({
            // 				title: '无网络'
            // 			})
            // 			plus.runtime.quit()
            // 		}
            // 	})
            // }, 3000)
        },
        handleWindowResize() {
            const appStore = useAppStore()
            appStore.windowSizeChange()
        },
        async appDataInit(options) {
            const appStore = useAppStore()
            appStore.asyncFromLocal()

            const userStore = useUserStore()
            userStore.asyncFromLocal()
            const langStore = useLangStore()
            langStore.asyncFromLocal()
            const res = uni.getSystemInfoSync()
            const dInfo = uni.getStorageSync('DEVICE_INFO')
            console.log("DEVICE_INFO:", dInfo);
            
            // #ifdef APP-PLUS
            if (!dInfo) {
                let s = ''
                try {
                    const gaid = await getAdid()
                    appStore.androidId = gaid
                    s = 'g_'
                } catch (error) {
                    appStore.androidId = await getAndroidId()
                    s = 'a_'
                    if (!appStore.androidId) {
                        appStore.androidId = res.deviceId
                    }
                }
                appStore.deviceId = s + md5(appStore.packageName + appStore.androidId).toString()
                uni.setStorageSync('DEVICE_INFO', JSON.stringify({
                    androidId: appStore.androidId,
                    s
                }))
            } else {
                const info = JSON.parse(dInfo)
                appStore.androidId = info.androidId
                appStore.deviceId = info.s + md5(appStore.packageName + info.androidId).toString()
            }
            console.log("androidId:", appStore.androidId);
            // #endif

            //系统信息
            // #ifdef WEB
            appStore.deviceId = res.deviceId
            // #endif
            appStore.systemInfo = res
            appStore.isNative = res.uniPlatform === 'app'
            appStore.appStatusBarHeight = res.statusBarHeight
            // 设备信息
            const deviceInfo = uni.getDeviceInfo()
            appStore.deviceInfo = deviceInfo
            console.log('deviceInfo', deviceInfo)

            if (options.query) {
                console.log('query:', options.query);

                if (options.query.cid) {
                    appStore.channelId = options.query.cid
                }
                if (options.query.h5No) {
                    console.log('h5No:', options.query.h5No);

                    appStore.h5No = Number(options.query.h5No)
                }
                if (options.query.invite_code) {
                    appStore.inviteCode = options.query.invite_code
                }
                if (options.query.fbp) {
                    appStore.fbp = options.query.fbp
                }
                if (options.query.fbc) {
                    appStore.fbc = options.query.fbc
                }
                if (options.query.uni_key) {
                    appStore.uniKey = options.query.uni_key
                } else {
                    appStore.uniKey = '' + Date.now() + Math.floor(Math.random() * 1000)
                }
                if (options.query.mcId) {
                    appStore.mcId = options.query.mcId
                }
                // NOTE: for 殼包
                if (options.query.isCoat && options.query.isCoat === '1') {
                    appStore.isCoat = true

                    // NOTE: for 殼包
                    const msg = {
                        eventName: 'coat-version',
                        version: process.env.COAT_VER || '0',
                        url: process.env.COAT_URL || 'https://www.google.com/'
                    };

                    parent.postMessage(JSON.stringify(msg), '*');
                }
                if (options.query.link_id) {
                    appStore.roibestLinkId = options.query.link_id
                }

                if (options.query.lk7data) {
                    appStore.lk7data = options.query.lk7data
                }

                appStore.saveToLocal()
            }

            if (options.path !== 'pages/index/index') {
                // if (!userStore.isLogin) {
                uni.reLaunch({
                    url: '/pages/index/index'
                })
                // }
            }

            appStore.getInfo()
            userStore.getServiceInfo()

        },
        setupTabbar() {
            const themeStore = useThemeStore()
            const {
                t
            } = useI18n()
            uni.setTabBarStyle({
                backgroundColor: themeStore.theme.tabbar.bgColor,
                color: themeStore.theme.tabbar.textColor,
                selectedColor: themeStore.theme.tabbar.textSelectColor
            })

            const tabBarMap = [{
                text: t('app.tabbar.home'),
                normal: themeStore.imageUrl.tabbar1,
                select: themeStore.imageUrl.tabbar1Select
            },
            {
                text: t('app.tabbar.promo'),
                normal: themeStore.imageUrl.tabbar2,
                select: themeStore.imageUrl.tabbar2Select
            },
            {
                text: t('app.tabbar.vip'),
                normal: themeStore.imageUrl.tabbar3,
                select: themeStore.imageUrl.tabbar3Select
            },
            {
                text: t('app.tabbar.earnMoney'),
                normal: themeStore.imageUrl.tabbar4,
                select: themeStore.imageUrl.tabbar4Select
            },
            {
                text: t('app.tabbar.account'),
                normal: themeStore.imageUrl.tabbar5,
                select: themeStore.imageUrl.tabbar5Select
            },
            ]

            tabBarMap.map((el, index) => {
                uni.setTabBarItem({
                    index,
                    text: el.text,
                    iconPath: el.normal,
                    selectedIconPath: el.select
                })
            })
        },
        setupTheme() {
            const themeStore = useThemeStore()
            // #ifdef H5
            document.body.style.backgroundColor = themeStore.theme.bgColor
            document.body.style.color = themeStore.theme.text.normal
            document.querySelector('meta[name="theme-color"]').setAttribute('content', themeStore.theme.bgColor)
            // #endif

            // uni.setBackgroundColor({
            //     backgroundColor: themeStore.theme.bgColor
            // })

        }
    }
}
</script>

<style lang="scss">
@import '@/uni_modules/uni-scss/index.scss';

// #chat-widget-container{
//     position: fixed;
//     bottom: 30% !important;
//     right: 0;
//     z-index: 9999;
// }
page {
    width: 100%;
    min-height: 100%;
    /* font-size: $font-14; */
    line-height: 1.35;
    // --safe-area-inset-top: 0px;  
    // --safe-area-inset-right: 0px;  
    // --safe-area-inset-bottom: 0px;  
    // --safe-area-inset-left: 0px;
    // background-color: #610606;
    // color: #ffffff;
    // background-color: #ffffff;
    // color: #211e21;
    font-weight: 500;
}

body {
    overscroll-behavior-y: contain;
}

view {
    box-sizing: border-box;
}

img {
    pointer-events: none;
    display: flex;
}

image {
    display: block;
}

div {
    box-sizing: border-box;
}

.safe-area {
    padding-bottom: constant(safe-area-inset-bottom) !important;
    padding-bottom: env(safe-area-inset-bottom) !important;
}

.uni-page-body {
    height: 100%;
}

.color-linear {
    background-image: -webkit-linear-gradient(1.95deg, #6B0916, #BC282D);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

// image{
// 	width: 100%;
// }
/*每个页面公共css */
.w-full {
    width: 100%;
}

.relative {
    position: relative;
}

.pos-absolute {
    position: absolute;
}

.p-1 {
    padding: 10rpx;
}

.p-2 {
    padding: 20rpx;
}

.px-1 {
    padding-left: 10rpx;
    padding-right: 10rpx;
}

.pt28 {
    padding-top: 28rpx;
}

.py-1 {
    padding-top: 10rpx;
    padding-bottom: 10rpx;
}

.mr-1 {
    margin-right: 10rpx;
}

.ml-1 {
    margin-left: 10rpx;
}

.ml-2 {
    margin-left: 20rpx;
}

.ml-1_5 {
    margin-left: 15rpx;
}

.mt-1 {
    margin-top: 10rpx;
}

.mt-5 {
    margin-top: 20rpx;
}

.mt-8 {
    margin-top: 40rpx;
}

.mb-1 {
    margin-bottom: 10rpx;
}

.mt30 {
    margin-top: 30rpx;
}

.pt30 {
    padding-top: 20rpx;
}

.flex {
    display: flex;
}

.flex-1 {
    flex: 1;
}

.flex-center {
    display: flex;
    align-items: center;
    justify-content: center;
}

.row-center {
    display: flex;
    justify-content: center;
}

.col-center {
    display: flex;
    align-items: center;
}

.flex-end {
    justify-content: flex-end;
}

.flex-column {
    display: flex;
    flex-direction: column;
}

.shrink-0 {
    flex-shrink: 0;
}

.text-center {
    text-align: center;
}

.grow-0 {
    flex-grow: 0;
}

.justify-between {
    justify-content: space-between;
}

.justify-center {
    justify-content: center;
}

.items-center {
    align-items: center;
}

.items-end {
    align-items: flex-end;
}

.justify-end {
    justify-content: flex-end;
}

.justify-around {
    justify-content: space-around;
}

.items-start {
    align-items: flex-start;
}

.uni-modal {
    background-color: #610606 !important;

    .uni-modal__hd {
        color: #fff !important;
    }

    .uni-modal__btn_default {
        color: rgba(255, 255, 255, 0.76) !important;
    }
}

.uni-popup {
    z-index: 9999 !important;
}
</style>