package audio

import (
	"bytes"
	"context"
	"fmt"
	"io"
	"time"

	"github.com/hajimehoshi/go-mp3"
	"github.com/youpy/go-wav"
	"go.uber.org/zap"
)

// monoToStereoReader 将单声道音频转换为立体声
type monoToStereoReader struct {
	src io.Reader
	buf []byte
}

func (m *monoToStereoReader) Read(p []byte) (int, error) {
	maxSamples := len(p) / 4
	if maxSamples == 0 {
		return 0, nil
	}

	// 按需分配缓冲区
	if cap(m.buf) < maxSamples*2 {
		m.buf = make([]byte, maxSamples*2)
	}

	// 读取单声道数据
	n, err := m.src.Read(m.buf[:maxSamples*2])
	if n == 0 {
		return 0, err
	}

	// 单声道→立体声：复制每个样本到左右声道
	samples := n / 2
	for i := range samples {
		base := i * 4
		mono := i * 2
		p[base] = m.buf[mono]     // 左声道低字节
		p[base+1] = m.buf[mono+1] // 左声道高字节
		p[base+2] = m.buf[mono]   // 右声道低字节
		p[base+3] = m.buf[mono+1] // 右声道高字节
	}

	if err == io.EOF {
		return samples * 4, io.EOF
	}
	return samples * 4, nil
}

// PlayWav 播放 WAV 文件(阻塞),直到完成或 context 取消
func PlayWav(ctx context.Context, r io.ReadCloser) error {
	// Read the entire file into memory since wav.NewReader needs ReadAt
	data, err := io.ReadAll(r)
	if err != nil {
		r.Close()
		return fmt.Errorf("读取 WAV 文件失败: %w", err)
	}
	r.Close()

	// Create a reader from the buffered data
	dec := wav.NewReader(bytes.NewReader(data))

	// 获取音频格式信息
	format, err := dec.Format()
	if err != nil {
		return fmt.Errorf("获取 WAV 格式失败: %w", err)
	}

	duration, _ := dec.Duration()
	sourceRate := int(format.SampleRate)
	channels := int(format.NumChannels)

	zap.S().Infof("WAV 音频: %d ch, %d Hz, 时长: %v", channels, sourceRate, duration)

	// 构建处理管线：单声道转换 → 重采样
	reader := io.Reader(dec)
	if channels == 1 {
		zap.S().Infof("单声道转立体声: 1 ch → 2 ch")
		reader = &monoToStereoReader{src: dec}
		channels = DefaultChannelCount
	}

	if needsResampling(sourceRate) {
		zap.S().Infof("Sinc 重采样: %d Hz → %d Hz, %d ch", sourceRate, UniversalSampleRate, channels)
		reader = newSincResampler(reader, sourceRate, UniversalSampleRate, channels)
	}

	otoCtx, err := initContext()
	if err != nil {
		return fmt.Errorf("音频上下文初始化失败: %w", err)
	}

	player := otoCtx.NewPlayer(reader)
	defer player.Close()

	player.Play()

	// 等待播放完成
	done := make(chan struct{})
	go func() {
		for !player.IsPlaying() {
			time.Sleep(10 * time.Millisecond)
		}
		for player.IsPlaying() {
			time.Sleep(10 * time.Millisecond)
		}
		time.Sleep(200 * time.Millisecond)
		close(done)
	}()

	select {
	case <-done:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	}
}

// PlayMP3 播放 MP3 文件(阻塞),直到完成或 context 取消
func PlayMP3(ctx context.Context, r io.ReadCloser) error {
	dec, err := mp3.NewDecoder(r)
	if err != nil {
		r.Close()
		return fmt.Errorf("MP3 解码失败: %w", err)
	}
	defer r.Close()

	// MP3 解码器信息
	sampleRate := int(dec.SampleRate())
	sampleCount := dec.Length()
	channels := 2 // MP3 通常是立体声
	duration := time.Duration(float64(sampleCount)/float64(sampleRate)*1000) * time.Millisecond

	zap.S().Infof("MP3 音频: %d Hz → %d Hz, 时长约: %v",
		sampleRate, UniversalSampleRate, duration)

	// 需要重采样（使用 Sinc 高质量重采样）
	var reader io.Reader = dec
	if needsResampling(sampleRate) {
		zap.S().Infof("Sinc 重采样: %d Hz → %d Hz", sampleRate, UniversalSampleRate)
		reader = newSincResampler(dec, sampleRate, UniversalSampleRate, channels)
	}

	otoCtx, err := initContext()
	if err != nil {
		return fmt.Errorf("音频上下文初始化失败: %w", err)
	}

	player := otoCtx.NewPlayer(reader)
	defer player.Close()

	player.Play()

	// 等待播放完成
	done := make(chan struct{})
	go func() {
		for !player.IsPlaying() {
			time.Sleep(10 * time.Millisecond)
		}
		for player.IsPlaying() {
			time.Sleep(10 * time.Millisecond)
		}
		time.Sleep(200 * time.Millisecond)
		close(done)
	}()

	select {
	case <-done:
		return nil
	case <-ctx.Done():
		return ctx.Err()
	}
}
