<template>
	<view class="camera-container">
		<!-- 顶部标题栏 -->
		<!-- <view class="header">
			<view class="back-button" @click="back">
				<text class="iconfont icon-back">&#xe679;</text>
			</view>
			<view class="title">中国传统文化测试</view>
			<view class="controls">
				<text class="iconfont icon-mic">&#xe677;</text>
        <text class="iconfont icon-settings" @click="toggleSettings">&#xe678;</text>
			</view>
		</view> -->

		<!-- 主要内容区域 -->
		<view class="content">
			<!-- 数字人头像 -->
			<view class="digital-avatar">
				<image src="/static/avatar.png" mode="aspectFill" class="avatar-image"></image>
			</view>

			<!-- 进度指示器 -->
			<!-- <view class="progress-container">
        <view class="progress-step" v-for="(step, index) in 3" :key="index">
          <view class="step-circle" :class="{'active': currentStep >= index+1}">{{index+1}}</view>
          <view class="step-text">{{stepTexts[index]}}</view>
        </view>
        <view class="progress-bar">
          <view class="progress-fill" :style="{width: progressWidth + '%'}"></view>
        </view>
      </view> -->

			<!-- 面试内容区域 -->
			<view class="interview-content">
				<!-- 问题编号和进度 -->
				<view class="question-number">
					<text>{{currentQuestionIndex + 1}}-{{currentQuestion.id}}/{{questions.length}}</text>
				</view>
				
				<!-- 问题和选项区域 -->
				<view class="question-area">
					<view class="question-importance">
						<text v-if="currentQuestion.isImportant">重点题</text> {{currentQuestion.text}}
					</view>

					<view class="options">
						<view v-for="(option, index) in currentQuestion.options" :key="index" class="option-item"
							:class="{
                'option-selected': selectedOption === index,
                'option-correct': showResult && index === currentQuestion.correctAnswer,
                'option-wrong': showResult && selectedOption === index && index !== currentQuestion.correctAnswer
              }" 
              @click="selectOption(index)">
							<text class="option-text">{{option}}</text>
						</view>
					</view>

					<view class="timer-container">
						<text class="timer-text">本题剩余时间 {{remainingTime}}</text>
					</view>
					
					<button class="next-button" @click="nextQuestion" :disabled="selectedOption === null">
						下一题
					</button>
				</view>
			</view>
		</view>

		<!-- 底部控制栏 -->
		<!-- <view class="footer">
			<view class="timer">本题剩余 {{remainingTime}}</view>
			<view class="next-step">进入下一题</view>
		</view> -->

		<!-- 面试结束弹窗 -->
		<view class="interview-end-modal" v-if="showEndModal">
			<view class="modal-content">
				<view class="modal-title">测试已完成</view>
				<view class="score-display">{{score}}/{{totalQuestions}}</view>
				<view class="modal-message">您在本次中国传统文化测试中答对了{{score}}道题目，共{{totalQuestions}}道题目。</view>
				<view class="modal-buttons">
					<button type="default" class="modal-button" @click="restartTest">重新测试</button>
					<button type="primary" class="modal-button" @click="back">返回首页</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cameraContext: null,
				devicePosition: 'front',
				interviewStarted: true,
				showEndModal: false,
				currentQuestionIndex: 0,
				currentStep: 1,
				stepTexts: ['测试准备', '回答问题', '测试结束'],
				progressWidth: 50,
				remainingTime: '00:27',
				selectedOption: null,
				showResult: false,
				isAnswerCorrect: false,
				questions: [{
						id: 6,
						text: '以下不属于中国传统节日的是（ ）。',
						options: [
							'A. 春节',
							'B. 端午节',
							'C. 重阳节',
							'D. 元旦'
						],
						correctAnswer: 3,
						isImportant: true,
						explanation: '元旦是公历新年，属于现代节日，而春节、端午节和重阳节都是中国传统节日。'
					},
					{
						id: 7,
						text: '下列哪个是中国四大名著之一（ ）。',
						options: [
							'A. 聊斋志异',
							'B. 西游记',
							'C. 世说新语',
							'D. 聊斋志异'
						],
						correctAnswer: 1,
						isImportant: false,
						explanation: '中国四大名著是《红楼梦》、《西游记》、《水浒传》和《三国演义》。'
					},
					{
						id: 8,
						text: '中国传统文化中"仁义礼智信"五常不包括（ ）。',
						options: [
							'A. 忠',
							'B. 孝',
							'C. 礼',
							'D. 信'
						],
						correctAnswer: 1,
						isImportant: true,
						explanation: '儒家的五常是"仁、义、礼、智、信"，不包括"孝"。'
					}
				],
				useVideo: false,
				timerInterval: null,
				score: 0,
				totalQuestions: 0
			}
		},
		computed: {
			currentQuestion() {
				return this.questions[this.currentQuestionIndex];
			}
		},
		onReady() {
			// 创建相机上下文
			this.cameraContext = uni.createCameraContext();
			// 创建AI视频上下文
			if (this.useVideo) {
				this.aiVideoContext = uni.createVideoContext('aiInterviewer');
			}
			// 启动倒计时
			this.startTimer();
			// 设置总题目数
			this.totalQuestions = this.questions.length;
		},
		methods: {
			startTimer() {
				// 模拟倒计时
				let seconds = 30; // 每题30秒
				this.timerInterval = setInterval(() => {
					seconds--;
					if (seconds <= 0) {
						clearInterval(this.timerInterval);
						// 时间到，自动提交答案
						if (!this.showResult) {
							this.checkAnswer();
						}
					}
					const min = Math.floor(seconds / 60).toString().padStart(2, '0');
					const sec = (seconds % 60).toString().padStart(2, '0');
					this.remainingTime = `${min}:${sec}`;
				}, 1000);
			},

			resetTimer() {
				clearInterval(this.timerInterval);
				this.startTimer();
			},

			selectOption(index) {
				if (this.showResult) return; // 已显示结果时不能再选择
				
				this.selectedOption = index;
				this.playAiSpeaking();
			},
			
			checkAnswer() {
				clearInterval(this.timerInterval); // 停止计时
				
				// 确保当前问题存在
				if (!this.currentQuestion) {
					console.error('当前问题不存在');
					return;
				}
				
				// 检查答案是否正确
				this.isAnswerCorrect = this.selectedOption === this.currentQuestion.correctAnswer;
				
				// 更新分数
				if (this.isAnswerCorrect) {
					this.score++;
				}
				
				// 直接进入下一题，不显示结果
				this.goToNextQuestion();
			},

			nextQuestion() {
				// 如果还没有显示结果，先检查答案
				if (this.selectedOption !== null) {
					this.checkAnswer();
					return;
				}
			},

			// 新增方法，处理进入下一题的逻辑
			goToNextQuestion() {
				// 重置状态
				this.showResult = false;
				this.selectedOption = null;
				
				// 前往下一题
				this.currentQuestionIndex++;
				
				// 检查是否已完成所有题目
				if (this.currentQuestionIndex >= this.questions.length) {
					// 显示测试结束弹窗
					this.showEndModal = true;
					return;
				}
				
				// 重置计时器
				this.resetTimer();
				
				// 更新进度
				this.progressWidth = (this.currentQuestionIndex + 1) / this.questions.length * 100;

				// 播放AI介绍下一题
				this.playAiSpeaking();

				setTimeout(() => {
					this.pauseAiSpeaking();
				}, 2000);
			},

			toggleSettings() {
				// 打开设置面板
				uni.showToast({
					title: '设置功能开发中',
					icon: 'none'
				});
			},

			back() {
				// 返回上一页
				clearInterval(this.timerInterval); // 确保清除计时器
				uni.navigateBack();
			},

			error(e) {
				// 相机错误事件
				console.error(e.detail);
				uni.showToast({
					title: '相机启动失败，请检查权限设置',
					icon: 'none'
				});
			},

			playAiSpeaking() {
				if (this.useVideo && this.aiVideoContext) {
					this.aiVideoContext.play();
				}
			},

			pauseAiSpeaking() {
				if (this.useVideo && this.aiVideoContext) {
					this.aiVideoContext.pause();
				}
			},
			
			// 重新开始测试
			restartTest() {
				this.currentQuestionIndex = 0;
				this.score = 0;
				this.showEndModal = false;
				this.showResult = false;
				this.selectedOption = null;
				this.resetTimer();
			}
		},
		// 添加生命周期钩子，确保在组件销毁时清除计时器
		beforeDestroy() {
			if (this.timerInterval) {
				clearInterval(this.timerInterval);
			}
		}
	}
</script>

<style>
	.camera-container {
		position: relative;
		width: 100%;
		height: 100vh;
		background-color: #ffffff;
		display: flex;
		flex-direction: column;
	}

	.header {
		height: 90rpx;
		/* background-color: #000000; */
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		/* color: #ffffff; */
	}

	.back-button {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.title {
		flex: 1;
		text-align: center;
		font-size: 32rpx;
		font-weight: bold;
	}

	.controls {
		display: flex;
		gap: 30rpx;
	}

	.iconfont {
		font-size: 40rpx;
	}

	.content {
		flex: 1;
		display: flex;
		flex-direction: column;
		position: relative;
		padding: 50rpx 20rpx 0;
	}

	.progress-container {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		margin-bottom: 20rpx;
	}

	.progress-step {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.step-circle {
		width: 40rpx;
		height: 40rpx;
		border-radius: 50%;
		background-color: #e0e0e0;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 24rpx;
		margin-right: 10rpx;
	}

	.step-circle.active {
		background-color: #ff9500;
		color: #ffffff;
	}

	.step-text {
		font-size: 24rpx;
		color: #666666;
	}

	.progress-bar {
		height: 10rpx;
		background-color: #e0e0e0;
		border-radius: 5rpx;
		overflow: hidden;
		margin-bottom: 20rpx;
	}

	.progress-fill {
		height: 100%;
		background-color: #00c853;
		transition: width 0.3s;
	}

	.interview-content {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.video-area {
		display: flex;
		/* flex-direction: column; */
		justify-content: space-between;
		height: auto;
		margin-bottom: 20rpx;
	}

	.video-camera {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
		width: 65%;
	}

	.interviewer {
		background-color: #f5f5f5;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		width: 48% !important;
		height: 300rpx;
	}

	.interviewer-video {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.interviewer-avatar {
		width: 80%;
		height: 80%;
		object-fit: contain;
	}

	.user-camera-container {
		width: 48%;
		height: 300rpx;
		border-radius: 10rpx;
		overflow: hidden;
		border: 4rpx solid #ffffff;
		box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.2);
	}

	.question-area {
		flex: 1;
		display: flex;
		flex-direction: column;
	}

	.question-number {
		font-size: 28rpx;
		color: #666;
		margin-bottom: 20rpx;
	}
	
	.question-importance {
		background-color: #f9f9f9;
		padding: 20rpx;
		border-radius: 10rpx;
		margin-bottom: 20rpx;
		font-size: 28rpx;
		color: #333;
	}
	
	.question-importance text {
		color: #ff0000;
		margin-right: 10rpx;
	}

	.options {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		margin-bottom: 30rpx;
	}

	.option-item {
		padding: 20rpx;
		background-color: #f5f5f5;
		border-radius: 10rpx;
		font-size: 28rpx;
		transition: all 0.3s;
		border: 1px solid #e0e0e0;
	}

	.option-selected {
		background-color: #e6f7ff;
		border: 1px solid #1890ff;
	}
	
	.option-correct {
		background-color: #d4f7d4;
		border: 1px solid #52c41a;
	}
	
	.option-wrong {
		background-color: #fff1f0;
		border: 1px solid #ff4d4f;
	}

	.option-text {
		color: #333;
	}
	
	.timer-container {
		text-align: center;
		margin: 20rpx 0;
	}
	
	.timer-text {
		font-size: 24rpx;
		color: #666;
	}

	.next-button {
		background-color: #6c5ce7;
		color: #ffffff;
		border-radius: 10rpx;
		font-size: 30rpx;
		margin: 20rpx auto;
		height: 80rpx;
		line-height: 80rpx;
		width: 90%;
		text-align: center;
	}
	
	.next-button[disabled] {
		background-color: #cccccc;
		color: #999999;
	}

	.footer {
		height: 100rpx;
		background-color: #000000;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 30rpx;
		color: #ffffff;
	}

	.timer {
		font-size: 28rpx;
	}

	.next-step {
		font-size: 28rpx;
	}

	.interview-end-modal {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
		z-index: 999;
	}

	.modal-content {
		width: 80%;
		background-color: #ffffff;
		border-radius: 20rpx;
		padding: 40rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.modal-title {
		font-size: 36rpx;
		font-weight: bold;
		margin-bottom: 30rpx;
	}

	.score-display {
		font-size: 48rpx;
		font-weight: bold;
		color: #6c5ce7;
		margin: 20rpx 0;
	}
	
	.modal-message {
		font-size: 28rpx;
		color: #666;
		text-align: center;
		margin-bottom: 40rpx;
	}
	
	.modal-buttons {
		display: flex;
		justify-content: space-between;
		width: 100%;
		margin-top: 20rpx;
	}
	
	.modal-button {
		width: 45%;
	}

	@keyframes speaking {
		0% {
			opacity: 0.8;
		}

		50% {
			opacity: 1;
		}

		100% {
			opacity: 0.8;
		}
	}

	.speaking {
		animation: speaking 1.5s infinite;
	}

	.digital-avatar {
		/* position: absolute;
		top: 20rpx;
		right: 20rpx; */
		width: 120rpx;
		height: 120rpx;
		z-index: 10;
	}
	
	.avatar-image {
		width: 120rpx;
		height: 120rpx;
		border-radius: 20rpx;
		border: 2rpx solid #e0e0e0;
	}
</style>