<script lang="ts" setup>
import { type InferType, number, object, string } from 'yup'
import ModalDigitalHumanSelect from '~/components/ModalDigitalHumanSelect.vue'
import type { FormSubmitEvent } from '#ui/types'
import { useFetchWrapped } from '~/composables/useFetchWrapped'

const emit = defineEmits(['success'])

const slide = useSlideover()
const toast = useToast()
const loginState = useLoginState()

const creationForm = ref<HTMLFormElement>()
const creationPending = ref(false)
const isDigitalSelectorOpen = ref(false)

const createCourseSchema = object({
  title: string()
    .trim()
    .min(4, '标题必须大于4个字符')
    .max(20, '标题不能超过20个字符')
    .required('请输入视频标题'),
  content: string()
    .trim()
    .min(4, '内容必须大于4个字符')
    .max(1000, '内容不能超过1000个字符')
    .required('请输入驱动文本内容'),
  digital_human_id: number().not([0], '请选择数字人'),
  source_type: number().default(0).required(),
  speed: number().default(1.0).min(0.5).max(1.5).required(),
  bg_img: string().optional(),
})

type CreateCourseSchema = InferType<typeof createCourseSchema>

const createCourseState = reactive({
  title: undefined,
  content: undefined,
  digital_human_id: 0,
  source_type: 0,
  speed: 1.0,
  bg_img: 'https://service1.fenshenzhike.com/default_background.png',
})

const selected_digital_human = ref<DigitalHumanItem | null>(null)
const selected_bg_img = ref<File | undefined>()

watchEffect(() => {
  if (selected_digital_human.value) {
    // 2025.02.26 使用内部数字人 ID
    createCourseState.digital_human_id =
      selected_digital_human.value.digital_human_id ??
      selected_digital_human.value.id ??
      0
    createCourseState.source_type = selected_digital_human.value.type!
  }
})

const onCreateCourseGreenSubmit = async (
  event: FormSubmitEvent<CreateCourseSchema>
) => {
  creationPending.value = true

  let payload: {
    token: string
    user_id: number
    title: string
    content: string
    digital_human_id: any
    speed: number
    device_id: string
    source_type: 1 | 2 | undefined
    bg_img?: string
  } = {
    token: loginState.token!,
    user_id: loginState.user.id,
    title: event.data.title,
    content: event.data.content,
    digital_human_id: event.data.digital_human_id,
    speed: 2 - event.data.speed,
    device_id: 'XSHAssistant Web',
    source_type: event.data.source_type as 1 | 2 | undefined,
    bg_img: event.data.bg_img,
  }

  useFetchWrapped<
    req.gen.GBVideoCreate & AuthedRequest,
    BaseResponse<resp.gen.GBVideoCreate>
  >('App.Digital_VideoTask.Create', payload)
    .then((res) => {
      if (!!res.data.task_id) {
        toast.add({
          title: '创建成功',
          description: '视频已加入生成队列',
          color: 'green',
          icon: 'i-tabler-check',
        })
        emit('success')
        slide.close()
      } else {
        toast.add({
          title: '创建失败',
          description: res.msg || '未知错误',
          color: 'red',
          icon: 'i-tabler-alert-triangle',
        })
      }
      creationPending.value = false
    })
    .catch((e) => {
      creationPending.value = false
      toast.add({
        title: '创建失败',
        description: e.message || '未知错误',
        color: 'red',
        icon: 'i-tabler-alert-triangle',
      })
    })
}
</script>

<template>
  <USlideover prevent-close>
    <UCard
      :ui="{
        body: { base: 'flex-1' },
        ring: '',
        divide: 'divide-y divide-gray-100 dark:divide-gray-800',
      }"
      class="flex flex-col flex-1"
    >
      <template #header>
        <div class="flex items-center justify-between">
          <h3
            class="text-base font-semibold leading-6 text-gray-900 dark:text-white"
          >
            新建绿幕视频
          </h3>
          <UButton
            class="-my-1"
            color="gray"
            icon="i-tabler-x"
            variant="ghost"
            @click="slide.close()"
          />
        </div>
      </template>

      <UForm
        ref="creationForm"
        :schema="createCourseSchema"
        :state="createCourseState"
        class="space-y-4"
        @submit="onCreateCourseGreenSubmit"
      >
        <div class="flex justify-between gap-2 *:flex-1">
          <UFormGroup
            label="视频标题"
            name="title"
            required
          >
            <UInput
              v-model="createCourseState.title"
              placeholder="请输入视频标题"
            />
          </UFormGroup>
        </div>

        <div class="grid grid-cols-1 sm:grid-cols-2 gap-2">
          <UFormGroup
            label="数字人"
            name="digital_human_id"
            required
          >
            <div
              :class="{ 'shadow-inner': !!selected_digital_human }"
              class="flex items-center gap-2 bg-neutral-100 dark:bg-neutral-800 p-2 rounded-md cursor-pointer select-none transition-all"
              @click="isDigitalSelectorOpen = true"
            >
              <div
                class="w-12 aspect-square border dark:border-neutral-700 rounded-md flex justify-center items-center overflow-hidden"
              >
                <UIcon
                  v-if="!selected_digital_human"
                  class="text-2xl opacity-50"
                  name="i-tabler-user-screen"
                />
                <NuxtImg
                  v-else
                  :src="selected_digital_human?.avatar"
                />
              </div>
              <div class="flex flex-col text-neutral-400 text-sm font-medium">
                <span
                  :class="!!selected_digital_human ? 'text-neutral-600' : ''"
                >
                  {{ selected_digital_human?.name || '点击选择数字人' }}
                </span>
                <span
                  v-if="selected_digital_human?.description"
                  class="text-2xs"
                >
                  {{ selected_digital_human?.description }}
                </span>
              </div>
            </div>
          </UFormGroup>
        </div>

        <!-- <UFormGroup label="背景图片" name="bg_img" help="可以上传图片作为视频背景，留空则为绿幕背景">
          <UInput type="file" accept="image/jpg,image/png" placeholder="选择背景图片" @change="selected_bg_img = $event?.[0] || undefined"/>
        </UFormGroup> -->

        <UFormGroup
          label="驱动内容"
          name="content"
          required
        >
          <UTextarea
            v-model="createCourseState.content"
            :rows="6"
            autoresize
            placeholder="请输入驱动文本内容"
          />
        </UFormGroup>

        <UAlert
          icon="tabler:background"
          color="sky"
          variant="subtle"
          title="自定义背景图片"
          description="背景图片可在视频生成完毕后，在下载选单中选择合成。默认为绿幕背景。"
        />

        <UAccordion
          :items="[{ label: '高级选项' }]"
          color="gray"
          size="lg"
        >
          <template #item>
            <div
              class="border dark:border-neutral-700 rounded-lg space-y-4 p-4 pb-6"
            >
              <UFormGroup
                :label="`视频倍速：${createCourseState.speed}`"
                name="speed"
              >
                <URange
                  v-model="createCourseState.speed"
                  :max="1.5"
                  :min="0.5"
                  :step="0.1"
                  class="pt-4"
                  size="sm"
                />
              </UFormGroup>
            </div>
          </template>
        </UAccordion>
      </UForm>

      <template #footer>
        <div class="flex justify-end space-x-4">
          <UButton
            color="gray"
            label="取消"
            size="lg"
            variant="ghost"
            @click="slide.close()"
          />
          <UButton
            :loading="creationPending"
            color="primary"
            label="提交"
            size="lg"
            variant="solid"
            @click="creationForm?.submit()"
          />
        </div>
      </template>
    </UCard>
    <ModalDigitalHumanSelect
      :is-open="isDigitalSelectorOpen"
      @close="isDigitalSelectorOpen = false"
      @select="digitalHumans => {
        selected_digital_human = (digitalHumans as DigitalHumanItem)
      }"
    />
  </USlideover>
</template>

<style scoped></style>
