#!/bin/bash
# 自動演示腳本

set -e

echo "🎬 Integrated Player - Auto Demo"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# 檢查編譯
if [ ! -f ./target/debug/integrated_player ]; then
	echo "⏳ 編譯整合播放器..."
	cargo build --bin integrated_player
fi

echo "✓ 播放器已編譯"
echo

# 設置測試數據
VIDEO=/tmp/charade_audio.wav
ASRX=/tmp/asrx_charade_optimized.json
FACE=/tmp/face_long.json

if [ ! -f "$VIDEO" ] || [ ! -f "$ASRX" ]; then
	echo "⚠️  未找到測試數據"
	echo "正在生成測試數據..."

	cd scripts/asrx_self
	python3 test_long_movie.py
	cd ../..
fi

# 運行演示
echo "🎯 開始自動演示..."
echo

# 檢查是否要顯示視頻
SHOW_VIDEO=""
if [ "$1" = "--video" ] || [ "$2" = "--video" ]; then
	SHOW_VIDEO="--show-video"
	echo "📺 視頻模式：將顯示視頻畫面"
	echo
fi

# 檢查是否是連續模式
if [ "$1" = "--continuous" ] || [ "$2" = "--continuous" ]; then
	echo "🎬 連續演示模式：從頭到尾播放"
	echo "⏸️  按 SPACE 暫停/恢復"
	echo "⏹️  按 Q 退出"
	echo

	./target/debug/integrated_player \
		--video "$VIDEO" \
		--asrx "$ASRX" \
		--continuous-demo \
		$SHOW_VIDEO
elif [ "$1" = "--quick" ] || [ "$2" = "--quick" ]; then
	# 快速演示（每個說話人 1 個片段）
	echo "⚡ 快速模式：每個說話人演示 1 個片段"
	echo

	./target/debug/integrated_player \
		--video "$VIDEO" \
		--asrx "$ASRX" \
		--demo \
		--demo-segments-per-speaker 1 \
		--demo-speed 3.0 \
		$SHOW_VIDEO
else
	# 標準演示（每個說話人 3 個片段）
	echo "📺 標準模式：每個說話人演示 3 個片段"
	echo

	./target/debug/integrated_player \
		--video "$VIDEO" \
		--asrx "$ASRX" \
		--demo \
		--demo-segments-per-speaker 3 \
		--demo-speed 2.0 \
		$SHOW_VIDEO
fi

echo
echo "✅ 演示完成！"
echo
echo "💡 提示："
echo "  - ./run_demo.sh --continuous     # 從頭到尾連續播放"
echo "  - ./run_demo.sh --video           # 顯示視頻畫面"
echo "  - ./run_demo.sh --quick           # 快速演示"
