class ExerciseUI { constructor() { this.currentExercise = null; this.answerSubmitted = false; } async render(exerciseData) { this.currentExercise = exerciseData; this.answerSubmitted = false; const exerciseContent = document.getElementById('exerciseContent'); const exerciseFeedback = document.getElementById('exerciseFeedback'); if (!exerciseContent) return; // پاک کردن فیدبک قبلی if (exerciseFeedback) { exerciseFeedback.innerHTML = ''; } let html = `

${this.escapeHtml(exerciseData.exercise.question)}

${exerciseData.exercise.description ? `

${this.escapeHtml(exerciseData.exercise.description)}

` : ''}
${exerciseData.exercise.hint ? `
💡 نکته: ${this.escapeHtml(exerciseData.exercise.hint)}
` : ''}
📋 انتظارات ما
    ${exerciseData.exercise.expected_keywords.map(keyword => `
  • ${this.escapeHtml(keyword)}
  • ` ).join('')}
🎯 معیارهای ارزیابی

پاسخ شما بر اساس تطابق با کلمات کلیدی بالا ارزیابی می‌شود.

حداقل ${Math.round((exerciseData.exercise.match_threshold || 0.6) * 100)}% تطابق مورد نیاز است.

`; exerciseContent.innerHTML = html; // تنظیم event listener برای textarea const textarea = document.getElementById('exerciseAnswer'); if (textarea && !this.answerSubmitted) { textarea.addEventListener('input', this.debounce(this.autoSaveAnswer.bind(this), 1000)); this.loadSavedAnswer(); } } async submitAnswer() { const answerTextarea = document.getElementById('exerciseAnswer'); const submitButton = document.getElementById('submitExercise'); if (!answerTextarea || !submitButton) return; const answer = answerTextarea.value.trim(); if (!answer) { Utils.showNotification('لطفاً پاسخ خود را وارد کنید', 'error'); return; } // غیرفعال کردن دکمه و نمایش حالت loading submitButton.disabled = true; submitButton.innerHTML = '⏳ در حال بررسی...'; try { const result = await learningLogic.checkExerciseAnswer( this.currentExercise.day, answer ); this.answerSubmitted = true; this.showResult(result); this.clearSavedAnswer(); // ارسال event برای به‌روزرسانی سایر بخش‌ها document.dispatchEvent(new CustomEvent('exerciseSubmitted', { detail: { exercise: this.currentExercise, result: result } })); } catch (error) { console.error('Error submitting exercise:', error); Utils.showNotification('خطا در ارسال پاسخ', 'error'); submitButton.disabled = false; submitButton.innerHTML = 'ارسال پاسخ'; } } showResult(result) { const exerciseFeedback = document.getElementById('exerciseFeedback'); if (!exerciseFeedback) return; const answerTextarea = document.getElementById('exerciseAnswer'); const submitButton = document.getElementById('submitExercise'); if (answerTextarea) { answerTextarea.disabled = true; } if (submitButton) { submitButton.disabled = true; submitButton.innerHTML = '✅ پاسخ ارسال شده'; } let html = `

${result.isCorrect ? '✅ پاسخ صحیح' : '❌ نیاز به بهبود'}

${result.reward} امتیاز

پیام: ${result.feedback.message}

📊 تحلیل کلمات کلیدی:
✅ کلمات یافت شده:
${result.matchedKeywords.length > 0 ? `
    ${result.matchedKeywords.map(kw => `
  • ${this.escapeHtml(kw)}
  • `).join('')}
` : '

هیچ کلمه‌ای یافت نشد

' }
❌ کلمات مفقوده:
${result.expectedKeywords.filter(kw => !result.matchedKeywords.includes(kw)).length > 0 ? `
    ${result.expectedKeywords.filter(kw => !result.matchedKeywords.includes(kw)) .map(kw => `
  • ${this.escapeHtml(kw)}
  • `).join('')}
` : '

همه کلمات یافت شدند

' }
${!result.isCorrect && result.hint ? `
💡 راهنمایی بهبود: ${this.escapeHtml(result.hint)}
` : '' }
`; exerciseFeedback.innerHTML = html; } tryAgain() { this.answerSubmitted = false; this.render(this.currentExercise); } nextExercise() { // در این نسخه ساده، به روز بعد می‌رویم // در نسخه‌های آینده می‌توان منطق پیچیده‌تری پیاده‌سازی کرد const nextDay = this.currentExercise.day + 1; document.dispatchEvent(new CustomEvent('dayChanged', { detail: { day: nextDay } })); } showSolution() { if (!this.currentExercise) return; // در این نسخه ساده، فقط کلمات کلیدی نمایش داده می‌شوند // در نسخه‌های آینده می‌توان راه‌حل کامل را نمایش داد const keywords = this.currentExercise.exercise.expected_keywords; Utils.showNotification( `کلمات کلیدی مورد انتظار: ${keywords.join('، ')}`, 'info' ); } autoSaveAnswer() { const answerTextarea = document.getElementById('exerciseAnswer'); if (!answerTextarea || this.answerSubmitted) return; const answer = answerTextarea.value; const key = `exercise_${this.currentExercise.day}_answer`; Utils.saveToLocalStorage(key, answer); } loadSavedAnswer() { const answerTextarea = document.getElementById('exerciseAnswer'); if (!answerTextarea) return; const key = `exercise_${this.currentExercise.day}_answer`; const savedAnswer = Utils.loadFromLocalStorage(key); if (savedAnswer) { answerTextarea.value = savedAnswer; } } clearSavedAnswer() { const key = `exercise_${this.currentExercise.day}_answer`; localStorage.removeItem(key); } escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } debounce(func, wait) { let timeout; return function executedFunction(...args) { const later = () => { clearTimeout(timeout); func(...args); }; clearTimeout(timeout); timeout = setTimeout(later, wait); }; } } // ایجاد نمونه اصلی const exerciseUI = new ExerciseUI();