import {
    ref,
    onUnmounted,
    onMounted,
    watch
} from 'vue'
import {
    onHide,
    onUnload
} from '@dcloudio/uni-app'
import config from '@/config'
// 请确保 piper-sdk.js 已经正确 export class PiperTTS
import {
    PiperTTS
} from './piper-sdk.js'

export function useTTSPlayer() {
    // UI 状态
    const isSpeaking = ref(false)
    const isPaused = ref(false)
    const isLoading = ref(false)

    // SDK 实例
    let piper = null

    /**
     * 初始化 SDK 实例
     * 每次 stop 后 piper 会被置空，这里会重新创建
     */
    const initPiper = () => {
        if (piper) return

        let baseUrl = config.speechSynthesis2 || ''
        baseUrl = baseUrl.replace(/\/$/, '')

        piper = new PiperTTS({
            baseUrl: baseUrl,
            onStatus: (msg, type) => {
                if (type === 'error') {
                    console.error('[TTS Error]', msg)
                    // 出错时不重置状态，交给用户手动处理或结束事件处理
                    resetState()
                }
            },
            onStart: () => {
                isLoading.value = false
                isSpeaking.value = true
                isPaused.value = false
            },
            onEnd: () => {
                resetState()
            }
        })
    }

    /**
     * 核心朗读方法
     */
    const speak = async (text) => {
        if (!text) return

        const processedText = extractSpeechText(text)
        if (!processedText) return

        // 1. 【关键修改】先彻底停止并销毁旧实例
        // 这会断开 socket 并且 close AudioContext，确保上一个声音立即消失
        await stop()

        // 2. 初始化新实例 (因为 stop() 把 piper 设为了 null)
        initPiper()

        // 3. 更新 UI 为加载中
        isLoading.value = true
        isPaused.value = false
        isSpeaking.value = true // 预先设为 true，防止按钮闪烁

        try {
            // 4. 激活音频引擎 (移动端防静音关键)
            await piper.init()

            // 5. 发送请求
            piper.speak(processedText, {
                speakerId: 0,
                noiseScale: 0.667,
                lengthScale: 1.0
            })
        } catch (e) {
            console.error('TTS Speak Error:', e)
            resetState()
        }
    }

    /**
     * 暂停
     */
    const pause = async () => {
        if (piper && piper.audioCtx && piper.audioCtx.state === 'running') {
            await piper.audioCtx.suspend()
            isPaused.value = true
        }
    }

    /**
     * 恢复
     */
    const resume = async () => {
        if (piper && piper.audioCtx && piper.audioCtx.state === 'suspended') {
            await piper.audioCtx.resume()
            isPaused.value = false
            isSpeaking.value = true
        }
    }

    /**
     * 停止并重置 (核打击模式)
     */
    const stop = async () => {
        if (piper) {
            // 1. 断开 WebSocket
            piper.stop()

            // 2. 【关键】关闭 AudioContext
            // Web Audio API 中，已经 schedule 的 buffer 很难单独取消
            // 最直接的方法是关闭整个 Context
            if (piper.audioCtx && piper.audioCtx.state !== 'closed') {
                try {
                    await piper.audioCtx.close()
                } catch (e) {
                    console.warn('AudioContext close failed', e)
                }
            }

            // 3. 销毁实例引用
            piper = null
        }
        resetState()
    }

    // UI 状态重置
    const resetState = () => {
        isSpeaking.value = false
        isPaused.value = false
        isLoading.value = false
    }

    // === 生命周期 ===
    onMounted(() => {
        // 预初始化可以不做，等到点击时再做，避免空闲占用 AudioContext 资源
        // initPiper() 
    })

    onUnmounted(() => {
        stop()
    })

    // Uniapp 生命周期
    if (typeof onHide === 'function') onHide(stop)
    if (typeof onUnload === 'function') onUnload(stop)

    return {
        speak,
        pause,
        resume,
        stop,
        cancelAudio: stop,
        isSpeaking,
        isPaused,
        isLoading
    }
}

/**
 * 提取文本逻辑 (保持不变)
 */
function extractSpeechText(markdown) {
    if (!markdown || markdown.indexOf('job-json') === -1) {
        return markdown;
    }

    const jobRegex = /``` job-json\s*({[\s\S]*?})\s*```/g;
    const jobs = [];
    let match;
    let lastJobEndIndex = 0;
    let firstJobStartIndex = -1;

    while ((match = jobRegex.exec(markdown)) !== null) {
        const jobStr = match[1];
        try {
            const job = JSON.parse(jobStr);
            jobs.push(job);
            if (firstJobStartIndex === -1) {
                firstJobStartIndex = match.index;
            }
            lastJobEndIndex = jobRegex.lastIndex;
        } catch (e) {
            console.warn('JSON 解析失败', e);
        }
    }

    const guideText = firstJobStartIndex > 0 ?
        markdown.slice(0, firstJobStartIndex).trim() : '';

    const endingText = lastJobEndIndex < markdown.length ?
        markdown.slice(lastJobEndIndex).trim() : '';

    const jobTexts = jobs.map((job, index) => {
        return `第 ${index + 1} 个岗位，岗位名称是：${job.jobTitle}，公司是：${job.companyName}，薪资：${job.salary}，地点：${job.location}，学历要求：${job.education}，经验要求：${job.experience}。`;
    });

    const finalTextParts = [];
    if (guideText) finalTextParts.push(guideText);
    finalTextParts.push(...jobTexts);
    if (endingText) finalTextParts.push(endingText);

    return finalTextParts.join('\n');
}