import {
    ref,
    onUnmounted,
    onBeforeUnmount,
    onMounted
} from 'vue'
import {
    onHide,
    onUnload
} from '@dcloudio/uni-app'
import WavDecoder from '@/lib/wav-decoder@1.3.0.js'
import config from '@/config'

export function useTTSPlayer() {
    const isSpeaking = ref(false)
    const isPaused = ref(false)
    const isComplete = ref(false)

    const audioContext = new(window.AudioContext || window.webkitAudioContext)()
    let playTime = audioContext.currentTime
    let sourceNodes = []
    let socket = null
    let sampleRate = 16000
    let numChannels = 1
    let isHeaderDecoded = false
    let pendingText = null

    let currentPlayId = 0
    let activePlayId = 0

    const speak = (text) => {
        currentPlayId++
        const myPlayId = currentPlayId
        reset()
        pendingText = text
        activePlayId = myPlayId
    }

    const pause = () => {
        if (audioContext.state === 'running') {
            audioContext.suspend()
            isPaused.value = true
            isSpeaking.value = false
        }
    }

    const resume = () => {
        if (audioContext.state === 'suspended') {
            audioContext.resume()
            isPaused.value = false
            isSpeaking.value = true
        }
    }

    const cancelAudio = () => {
        stop()
    }

    const stop = () => {
        isSpeaking.value = false
        isPaused.value = false
        isComplete.value = false
        playTime = audioContext.currentTime

        sourceNodes.forEach(node => {
            try {
                node.stop()
                node.disconnect()
            } catch (e) {}
        })
        sourceNodes = []

        if (socket) {
            socket.close()
            socket = null
        }

        isHeaderDecoded = false
        pendingText = null
    }

    const reset = () => {
        stop()
        isSpeaking.value = false
        isPaused.value = false
        isComplete.value = false
        playTime = audioContext.currentTime
        initWebSocket()
    }

    const initWebSocket = () => {
        const thisPlayId = currentPlayId
        socket = new WebSocket(config.speechSynthesis)
        socket.binaryType = 'arraybuffer'

        socket.onopen = () => {
            if (pendingText && thisPlayId === activePlayId) {
                const seepdText = extractSpeechText(pendingText)
                console.log(seepdText)
                socket.send(seepdText)
                pendingText = null
            }
        }

        socket.onmessage = async (e) => {
            if (thisPlayId !== activePlayId) return // 忽略旧播放的消息

            if (typeof e.data === 'string') {
                try {
                    const msg = JSON.parse(e.data)
                    if (msg.status === 'complete') {
                        isComplete.value = true
                        setTimeout(() => {
                            if (thisPlayId === activePlayId) {
                                isSpeaking.value = false
                            }
                        }, (playTime - audioContext.currentTime) * 1000)
                    }
                } catch (e) {
                    console.log('[TTSPlayer] 文本消息:', e.data)
                }
            } else if (e.data instanceof ArrayBuffer) {
                if (!isHeaderDecoded) {
                    try {
                        const decoded = await WavDecoder.decode(e.data)
                        sampleRate = decoded.sampleRate
                        numChannels = decoded.channelData.length
                        decoded.channelData.forEach((channel, i) => {
                            const audioBuffer = audioContext.createBuffer(1, channel.length,
                                sampleRate)
                            audioBuffer.copyToChannel(channel, 0)
                            playBuffer(audioBuffer)
                        })
                        isHeaderDecoded = true
                    } catch (err) {
                        console.error('WAV 解码失败:', err)
                    }
                } else {
                    const pcm = new Int16Array(e.data)
                    const audioBuffer = pcmToAudioBuffer(pcm, sampleRate, numChannels)
                    playBuffer(audioBuffer)
                }
            }
        }
    }

    const pcmToAudioBuffer = (pcm, sampleRate, numChannels) => {
        const length = pcm.length / numChannels
        const audioBuffer = audioContext.createBuffer(numChannels, length, sampleRate)
        for (let ch = 0; ch < numChannels; ch++) {
            const channelData = audioBuffer.getChannelData(ch)
            for (let i = 0; i < length; i++) {
                const sample = pcm[i * numChannels + ch]
                channelData[i] = sample / 32768
            }
        }
        return audioBuffer
    }

    const playBuffer = (audioBuffer) => {
        if (!isSpeaking.value) {
            playTime = audioContext.currentTime
        }
        const source = audioContext.createBufferSource()
        source.buffer = audioBuffer
        source.connect(audioContext.destination)
        source.start(playTime)
        sourceNodes.push(source)
        playTime += audioBuffer.duration
        isSpeaking.value = true
    }

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

    // 页面刷新/关闭时
    onMounted(() => {
        if (typeof window !== 'undefined') {
            window.addEventListener('beforeunload', cancelAudio)
        }
    })

    onBeforeUnmount(() => {
        cancelAudio()
        if (typeof window !== 'undefined') {
            window.removeEventListener('beforeunload', cancelAudio)
        }
    })

    onHide(cancelAudio)
    onUnload(cancelAudio)

    initWebSocket()

    return {
        speak,
        pause,
        resume,
        cancelAudio,
        isSpeaking,
        isPaused,
        isComplete
    }
}

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

    // 提取岗位 json 数据及前后位置
    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);
        }
    }

    // 提取引导语（第一个 job-json 之前的文字）
    const guideText = firstJobStartIndex > 0 ?
        markdown.slice(0, firstJobStartIndex).trim() :
        '';

    // 提取结束语（最后一个 job-json 之后的文字）
    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');
}