<template>
  <div class="voice-selector">
    <div v-if="displayedVoices.length === 0" class="empty-voices">
      还没有配音，可先在"配音管理"中上传
    </div>

    <div v-else class="voice-selector-with-preview">
      <a-select
        v-model:value="selectedVoiceId"
        placeholder="请选择音色"
        class="voice-select"
        :options="voiceOptions"
        @change="handleVoiceChange"
        style="width: calc(100% - 80px)"
      />

      <a-button
        class="preview-button"
        size="small"
        :disabled="!selectedVoiceId"
        :loading="previewLoadingVoiceId === selectedVoiceId"
        @click="handlePreviewCurrentVoice"
      >
        <template #icon>
          <SoundOutlined />
        </template>
        试听
      </a-button>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useVoiceCopyStore } from '@/stores/voiceCopy'
import { useTTS, TTS_PROVIDERS } from '@/composables/useTTS'

const voiceStore = useVoiceCopyStore()

const emit = defineEmits(['select'])

// 使用TTS Hook，默认使用Qwen供应商
const {
  previewLoadingVoiceId,
  playingPreviewVoiceId,
  ttsText,
  speechRate,
  playVoiceSample,
  setText,
  setSpeechRate,
  resetPreviewState
} = useTTS({
  provider: TTS_PROVIDERS.QWEN
})

// 当前选中的音色ID
const selectedVoiceId = ref('')

// 从store数据构建音色列表
const userVoiceCards = computed(() =>
  (voiceStore.profiles || []).map(profile => ({
    id: `user-${profile.id}`,
    rawId: profile.id,
    name: profile.name || '未命名',
    category:'',
    gender: profile.gender || 'female',
    description: profile.note || '我的配音',
    fileUrl: profile.fileUrl,
    transcription: profile.transcription || '',
    source: 'user',
    voiceId: profile.voiceId
  }))
)

const displayedVoices = computed(() => userVoiceCards.value)

// 转换为下拉框选项格式
const voiceOptions = computed(() =>
  displayedVoices.value.map(voice => ({
    value: voice.id,
    label: voice.name,
    data: voice // 保存完整数据
  }))
)

// 音色选择变化处理
const handleVoiceChange = (value, option) => {
  const voice = option.data
  selectedVoiceId.value = value
  emit('select', voice)
}

// 试听当前选中的音色
const handlePreviewCurrentVoice = () => {
  if (!selectedVoiceId.value) return

  const voice = displayedVoices.value.find(v => v.id === selectedVoiceId.value)
  if (!voice) return

  handlePlayVoiceSample(voice)
}

/**
 * 处理音色试听
 * 使用Hook提供的playVoiceSample方法
 */
const handlePlayVoiceSample = (voice) => {
  playVoiceSample(
    voice,
    (audioData) => {
      // 成功回调
      console.log('音频播放成功', audioData)
    },
    (error) => {
      // 错误回调
      console.error('音频播放失败', error)
    }
  )
}

/**
 * 设置要试听的文本（供父组件调用）
 * @param {string} text 要试听的文本
 */
const setPreviewText = (text) => {
  setText(text)
}

/**
 * 设置语速（供父组件调用）
 * @param {number} rate 语速倍率
 */
const setPreviewSpeechRate = (rate) => {
  setSpeechRate(rate)
}

defineExpose({
  setPreviewText,
  setPreviewSpeechRate
})

onMounted(async () => {
  await voiceStore.refresh()
})
</script>

<style scoped>
.voice-selector {
  width: 100%;
}

.empty-voices {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--color-text-secondary);
  background: rgba(0, 0, 0, 0.3);
  border: 1px dashed rgba(59, 130, 246, 0.3);
  border-radius: var(--radius-card);
}

/* 音色选择器和试听按钮的容器 */
.voice-selector-with-preview {
  display: flex;
  gap: 8px;
  align-items: center;
  width: 100%;
}

/* 下拉框样式 */
.voice-select {
  flex: 1;
}

/* 试听按钮样式 */
.preview-button {
  height: 32px;
  white-space: nowrap;
}
</style>
