<script setup lang="ts">
    import dayjs from 'dayjs'
    import { checkUpgrade, closeSplashscreenAndChechUpgrade } from '@/utils/upgrade'
    import { useRuntime } from '@/hooks/app/useRuntime'
    import { useConcealedExit } from '@/hooks/app/useConcealedExit'
    import * as Pages from '@/utils/pages'

    const { exit } = useConcealedExit()
    const { app } = useRuntime()
    const year = ref(dayjs().year())
    const title = ref('Hello World')
    const version = computed(() => app.value.version)

    onLoad(() => {
        // 关闭启动页并检查更新
        closeSplashscreenAndChechUpgrade()

        // test API
        API.example.hello
            .request()
            .then((body) => {
                title.value = body

                console.log('[API]', body, $app.name, $app.version)
                Message.toast(body)
            })
            .catch((err) => {
                console.error('[API]', err)
            })

        // test WebSocket(STOMP)
        Stomp.connect(() => {
            Stomp.send('/app/ping', 'ping')
        })

        // test uni-stat
        uni.report('onLoad', `[Test] onLoad: ${dayjs().format('YYYY-MM-DD HH:mm:ss')}`)
    })

    function surprise() {
        // #ifdef APP-PLUS
        const orientation = plus.navigator.getOrientation()
        if (orientation === 0) {
            plus.screen.lockOrientation('landscape-primary')
        } else if (orientation === 90) {
            plus.screen.lockOrientation('portrait-primary')
        }
        Message.toast('🥳 surprise~')
        // #endif

        // #ifndef APP-PLUS
        Message.toast(`在手机上运行点击有惊喜~ ╰(*°▽°*)╯`)
        // #endif
    }

    // 打开 AI 对话窗口
    function openChat() {
        const link =
            'https://api.coze.cn/open-platform/sdk/chatapp?params=%7B%22chatClientId%22%3A%22Hd1w0dc49QzvaAGlDONiA%22%2C%22chatConfig%22%3A%7B%22bot_id%22%3A%227426295021118488587%22%2C%22user%22%3A%22NmAh7FUtuyO6RPrzYMg4v%22%2C%22conversation_id%22%3A%22cqnuKy6c3lmYTdjF0vslj%22%7D%2C%22componentProps%22%3A%7B%22layout%22%3A%22mobile%22%2C%22lang%22%3A%22zh_CN%22%2C%22uploadable%22%3Atrue%2C%22title%22%3A%22%E6%B9%96%E5%8D%97%E5%A4%A9%E6%B0%94%20AI%20%E6%99%BA%E8%83%BD%E5%8A%A9%E7%90%86%22%2C%22icon%22%3A%22https%3A%2F%2Fhntq-res.oss-cn-shenzhen.aliyuncs.com%2Fhntq-ai-logo.png%22%7D%7D'
        uni.navigateTo({
            url: `/pages/chat/index?link=${encodeURIComponent(link)}`,
            animationType: 'slide-in-bottom',
        })
    }
</script>

<template>
    <view class="content flex-center flex-col">
        <fui-avatar
            src="/static/logo.png"
            radius="14"
            size="large"
            background="transparent"
            class="!mb-3"
            @click="surprise"
        />
        <view class="flex-center flex-col mb-3">
            <text class="title">{{ title }}</text>
            <text class="title">{{ $t('app.hello') }}</text>
            <Icon icon="emojione-robot-face" size="52" class="mt-3" @click="openChat" />

            <fui-divider />

            <fui-button type="link" size="large" @click="Pages.to('/pages/example/index')">
                <Icon icon="material-symbols-arrow-right-alt" size="48" color="gray" class="mr-1" />
                <text class="color-gray">Examples</text>
            </fui-button>
        </view>
        <fui-footer isFixed>
            <template #text>
                <view v-if="version" @tap="checkUpgrade(true)">V{{ version }}</view>
                <view class="mt-1">壹润科技 版权所有</view>
                <view class="mt-1" @tap="exit">Copyright © 2021-{{ year }} Yiring. All Ringhts Reserved</view>
            </template>
        </fui-footer>
    </view>
</template>

<style lang="less" scoped>
    .content {
        height: calc(100vh - 100rpx);
    }

    .title {
        display: flex;
        justify-content: center;
        font-size: 36rpx;
        color: #8f8f94;
    }
</style>
