import dayjs from "dayjs";

// 判断题目是否为多选题
export function isMultipleChoice(question) {
	// 只有选择题才有多选/单选的概念
	if (question.typeAnswer !== 0) return false;
	if (!question.answerList || !question.answerList.length) return false;
	const correctAnswers = question.answerList.filter(option => option.isTrue === 1 || option.isTrue === true);
	return correctAnswers.length > 1;
}

// 判断选项是否被选中
export function isOptionSelected(question, optionId) {
	// 只有选择题才有选项的概念
	if (question.typeAnswer !== 0) return false;
	if (!question.userAnswerBaseList || !question.userAnswerBaseList.length) return false;
	return question.userAnswerBaseList.some(answer => answer.answerId === optionId);
}

// 判断答案是否正确
export function isAnswerCorrect(question) {
	if (question.typeAnswer === 0) {
		// 选择题逻辑
		if (!question.userAnswerBaseList || !question.userAnswerBaseList.length) return false;

		// 获取所有正确答案的ID
		const correctOptionIds = question.answerList
			.filter(option => option.isTrue === 1 || option.isTrue === true)
			.map(option => option.id);

		// 获取用户选择的答案ID
		const userAnswerIds = [...new Set(question.userAnswerBaseList.map(answer => answer.answerId))];

		// 判断用户选择的答案数量是否与正确答案数量相同
		if (userAnswerIds.length !== correctOptionIds.length) {
			return false;
		}

		// 判断用户选择的答案是否都是正确的
		return userAnswerIds.every(answerId => correctOptionIds.includes(answerId));
	} else if (question.typeAnswer === 1) {
		// 填空题逻辑
		if (!question.userAppletAnswerTrain || !question.userAppletAnswerTrain.answer) return false;
		
		// 如果有标准答案，进行比较
		if (question.answer) {
			// 简单的字符串比较，可以根据实际需求调整比较逻辑
			return question.userAppletAnswerTrain.answer.trim().toLowerCase() === question.answer.trim().toLowerCase();
		}
		
		// 如果没有标准答案，只要有内容就认为是正确的
		return question.userAppletAnswerTrain.answer.trim() !== '';
	}
	
	return false;
}

export function isQuestionAnswered(question) {
	if (question.typeAnswer === 1) {
		// 填空题：检查是否有答案且答案不为空
		return question.userAppletAnswerTrain &&
			question.userAppletAnswerTrain.answer &&
			question.userAppletAnswerTrain.answer.trim() !== '';
	} else if (question.typeAnswer === 0) {
		// 选择题：检查是否有选择的答案
		if (!question.userAnswerBaseList || !question.userAnswerBaseList.length) return false;
		
		// 对于多选题和单选题，都要求至少有一个答案
		return question.userAnswerBaseList.length > 0;
	}
	
	return false;
}

// 获取题目的用户答案数组（支持单选和多选）
export function getUserAnswerIds(question) {
	// 只有选择题才有answerId的概念
	if (question.typeAnswer !== 0) return [];
	if (!question.userAnswerBaseList || !question.userAnswerBaseList.length) return [];
	return [...new Set(question.userAnswerBaseList.map(answer => answer.answerId))];
}

// 获取题目的正确答案数组
export function getCorrectAnswerIds(question) {
	// 只有选择题才有answerId的概念
	if (question.typeAnswer !== 0) return [];
	if (!question.answerList || !question.answerList.length) return [];
	return question.answerList
		.filter(option => option.isTrue === 1 || option.isTrue === true)
		.map(option => option.id);
}

// 获取填空题的用户答案
export function getUserTextAnswer(question) {
	if (question.typeAnswer !== 1) return '';
	if (!question.userAppletAnswerTrain || !question.userAppletAnswerTrain.answer) return '';
	return question.userAppletAnswerTrain.answer.trim();
}

// 获取填空题的正确答案
export function getCorrectTextAnswer(question) {
	if (question.typeAnswer !== 1) return '';
	return question.answer || '';
}

// 判断全部题目答案是否达到指定的准确率（只判断选择题）
export function isSuccessPrecision(list, number = 100){
	
	let errNumber = 0
	let totalChoiceQuestions = 0
	
	for (var index = 0; index < list.length; index++) {
		var question = list[index];
		
		// 只判断选择题（typeAnswer === 0），无论是基本题还是培训题
		if (question.typeAnswer === 0) {
			totalChoiceQuestions++
			if (!isAnswerCorrect(question)) {
				errNumber++
			}
		}
	}
	
	// 如果没有选择题，返回true
	if (totalChoiceQuestions === 0) {
		console.log('没有选择题需要判断');
		return true
	}
	
	const accuracy = (totalChoiceQuestions - errNumber) / totalChoiceQuestions * 100
	
	console.log('选择题总数:', totalChoiceQuestions, '答错题目数:', errNumber, '正确率:', accuracy);    
	
	if(accuracy < number){
		return false
	}
	
	return true
}

export default {
	isMultipleChoice,
	isOptionSelected,
	isAnswerCorrect,
	isQuestionAnswered,
	getUserAnswerIds,
	getCorrectAnswerIds,
	getUserTextAnswer,
	getCorrectTextAnswer,
	isSuccessPrecision,
}