<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="home">
{
  layout: 'tabbar',
  style: {
    navigationStyle: 'custom',
    navigationBarTitleText: '首页',
  },
}
</route>

<script lang="ts" setup>
import Card from '@/components/card.vue'
import HotActivity from '@/components/hot-activity.vue'
import MomentItem from '@/components/moment-item.vue'
import useRequest from '../../hooks/useRequest'
import Menus from './components/menus.vue'
import { getCircles, getSetIndexConfigs, shareCircle } from '../../core/libs/requests'
import { logo } from '../../core/libs/svgs'
import PageHelper from '@/components/page-helper.vue'
import { ComponentExposed } from 'vue-component-type-helpers'
import { usePermissions } from '../../composables/permissions'
import { storeToRefs } from 'pinia'
import { messages } from '../../core/libs/messages'
import { handleUpvoteClick } from '../../core/libs/actions'
import { useUserStore } from '../../store'

defineOptions({
  name: 'Home',
})
const instance = getCurrentInstance()
const userStore = useUserStore()
const { userInfo } = storeToRefs(userStore)
const { isLogined, isDesigner } = usePermissions()
const pageHelperRef = ref<ComponentExposed<typeof PageHelper>>()
const { data: indexConfigsData, run: setIndexConfigsData } = useRequest(
  () => getSetIndexConfigs(),
  { initialData: { list: [] } },
)
const swiperData = ref<{ data: any; videoContext: UniNamespace.VideoContext; playing: boolean }[]>()
const swiperCurrent = ref(0)

onShow(async () => {
  pageHelperRef.value?.refresh()
})
onLoad(async () => {
  await setIndexConfigsData()
  swiperData.value = indexConfigsData.value.list.map((it) => ({
    data: it,
    videoContext: uni.createVideoContext(`video-${it.id}`, instance),
    playing: false,
  }))
})
const toAbout = () => {
  uni.navigateTo({ url: '/pages/home/about/index' })
}
const handleSwiperChange = ({ detail: { current } }) => {
  swiperCurrent.value = current
}
const handlePlay = () => {
  swiperData.value[swiperCurrent.value].videoContext.play()
  swiperData.value[swiperCurrent.value].playing = true
}
const handleLike = async (options) => {
  await handleUpvoteClick({
    ...options,
    userId: userInfo.value.userId,
    userName: userInfo.value.nickname,
  })
  pageHelperRef.value?.refresh()
}
onShareAppMessage(async ({ from, target }) => {
  const res: Page.CustomShareContent = {}
  if (from === 'button') {
    await shareCircle(target.id)
    res.path = `/pages/home/moment/index?id=${target.id}`
    res.imageUrl = target.dataset.options.bannerUrls[0]
    res.title = `${target.dataset.options.stylistName}: ${target.dataset.options.circleDesc}`
  }
  if (from === 'menu') {
    res.title = messages.home.shareTitle
  }
  return res
})
</script>

<template>
  <view class="">
    <view class="bg-black w-full relative aspect-[1.26/1]">
      <swiper @change="handleSwiperChange">
        <template
          v-for="{
            data: { id, coverVideoImage, indexPromotionalVideoImage },
            playing,
          } of swiperData"
          :key="id"
        >
          <swiper-item>
            <div class="w-full h-full relative">
              <video
                class="w-full h-full"
                :id="`video-${id}`"
                :src="indexPromotionalVideoImage"
              ></video>
              <div v-if="!playing" class="absolute left-0 top-0 w-full h-full bg-black">
                <wd-img width="100%" height="100%" :src="coverVideoImage" />
              </div>
              <div
                v-if="!playing"
                class="w-[375px] h-[90px] bg-gradient-to-t from-black to-black/0 absolute left-0 bottom-0 w-full flex items-center"
              >
                <view class="mx-7">
                  <wd-button
                    plain
                    custom-class="bg-transparent! border-white! text-white!"
                    icon="play"
                    @click="handlePlay"
                  >
                    02:30
                  </wd-button>
                </view>
              </div>
            </div>
          </swiper-item>
        </template>
      </swiper>
    </view>
    <view class="bg-[#f6f6f6] relative bottom-4 rounded-t-2xl py-1">
      <!-- <ScheduleCard custom-class="my-6 mx-3.5"></ScheduleCard> -->
      <menus></menus>
      <!-- <view class="my-6 mx-3.5">
        <HotActivity></HotActivity>
      </view> -->
      <view v-if="isDesigner" class="my-6 mx-3.5" @click="toAbout()">
        <Card>
          <div class="flex items-center gap-2">
            <wd-img width="28" height="28" :src="logo"></wd-img>
            <div class="text-black/90 text-base font-normal font-['PingFang_SC'] leading-[10.18px]">
              1分钟快速了解筑巢荟
            </div>
            <div class="flex-1"></div>
            <wd-icon name="help-circle" size="22px" custom-class="text-black/60"></wd-icon>
          </div>
        </Card>
      </view>
      <view class="mx-3.5 text-5 font-400">设计圈</view>
      <view class="mx-3.5">
        <PageHelper ref="pageHelperRef" :request="getCircles" :query="{}">
          <template #default="{ source }">
            <template v-for="it of source.list" :key="it.id">
              <view class="my-6">
                <MomentItem :options="it" @like="handleLike"></MomentItem>
              </view>
            </template>
          </template>
        </PageHelper>
      </view>
    </view>
  </view>
</template>

<style></style>
