class LessonUI { constructor() { this.currentLesson = null; this.lessonStartTime = null; this.interactionCount = 0; } async render(lessonData) { this.currentLesson = lessonData; this.lessonStartTime = Date.now(); this.interactionCount = 0; const lessonContent = document.getElementById('lessonContent'); if (!lessonContent) return; let html = `

${this.escapeHtml(lessonData.title)}

روز ${lessonData.day} ⏱️ حدود ${lessonData.estimated_duration || '10'} دقیقه
`; lessonData.sections.forEach((section, index) => { html += this.renderSection(section, index); }); html += `
0% مطالعه شده
`; lessonContent.innerHTML = html; this.setupInteractions(); this.startProgressTracking(); } renderSection(section, index) { let sectionHtml = `
`; switch (section.type) { case 'text': sectionHtml += `
${this.formatTextContent(section.content)}
`; break; case 'image': sectionHtml += `
${this.escapeHtml(section.alt)} ${section.caption ? `
${this.escapeHtml(section.caption)}
` : ''}
`; break; case 'code': sectionHtml += `
${this.escapeHtml(section.code)}
${section.explanation ? `
${this.formatTextContent(section.explanation)}
` : ''}
`; break; case 'video': sectionHtml += `
🎥 محتوای ویدئویی: ${this.escapeHtml(section.title)}
${section.description ? `
${this.formatTextContent(section.description)}
` : ''}
`; break; default: sectionHtml += `
نوع محتوای ناشناخته
`; } sectionHtml += `
`; return sectionHtml; } formatTextContent(content) { // تبدیل مارک‌داون ساده به HTML return content .replace(/\*\*(.*?)\*\*/g, '$1') .replace(/\*(.*?)\*/g, '$1') .replace(/`(.*?)`/g, '$1') .replace(/\n/g, '
'); } escapeHtml(unsafe) { return unsafe .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """) .replace(/'/g, "'"); } setupInteractions() { // ردیابی اسکرول برای پیشرفت let scrollTimeout; const lessonSections = document.querySelector('.lesson-sections'); if (lessonSections) { lessonSections.addEventListener('scroll', () => { clearTimeout(scrollTimeout); scrollTimeout = setTimeout(() => { this.trackInteraction('scroll'); this.updateProgress(); }, 100); }); } // ردیابی کلیک document.addEventListener('click', (e) => { if (e.target.closest('.lesson-section')) { this.trackInteraction('click'); } }); } startProgressTracking() { this.progressInterval = setInterval(() => { this.updateProgress(); }, 2000); } updateProgress() { const sections = document.querySelectorAll('.lesson-section'); const lessonSections = document.querySelector('.lesson-sections'); if (!sections.length || !lessonSections) return; let visibleSections = 0; const containerRect = lessonSections.getBoundingClientRect(); sections.forEach(section => { const rect = section.getBoundingClientRect(); if (rect.top >= containerRect.top && rect.bottom <= containerRect.bottom) { visibleSections++; } }); const progress = (visibleSections / sections.length) * 100; this.updateProgressBar(progress); } updateProgressBar(progress) { const progressFill = document.querySelector('.progress-fill'); const progressText = document.querySelector('.progress-text'); if (progressFill && progressText) { progressFill.style.width = `${progress}%`; progressText.textContent = `${Math.round(progress)}% مطالعه شده`; } } trackInteraction(type) { this.interactionCount++; // ذخیره تعاملات مهم const interactions = Utils.loadFromLocalStorage('lesson_interactions') || {}; const lessonId = this.currentLesson?.day; if (lessonId) { if (!interactions[lessonId]) { interactions[lessonId] = []; } interactions[lessonId].push({ type: type, timestamp: new Date().toISOString() }); Utils.saveToLocalStorage('lesson_interactions', interactions); } } async completeLesson() { if (!this.currentLesson) return; const duration = Math.round((Date.now() - this.lessonStartTime) / 1000 / 60); // دقیقه const result = learningLogic.completeLesson( this.currentLesson.day, duration, this.interactionCount ); // نمایش نتیجه Utils.showNotification( `درس تکمیل شد! 🎉 ${result.reward} امتیاز دریافت کردید.`, 'success' ); // به‌روزرسانی UI this.updateProgressBar(100); // غیرفعال کردن دکمه const completeBtn = document.querySelector('.lesson-actions .btn-primary'); if (completeBtn) { completeBtn.disabled = true; completeBtn.textContent = '✅ تکمیل شده'; } // ارسال event برای به‌روزرسانی سایر بخش‌ها document.dispatchEvent(new CustomEvent('lessonCompleted', { detail: { lesson: this.currentLesson, reward: result.reward, duration: duration } })); } toggleBookmark() { if (!this.currentLesson) return; const bookmarks = Utils.loadFromLocalStorage('bookmarked_lessons') || []; const lessonId = this.currentLesson.day; const index = bookmarks.indexOf(lessonId); let isBookmarked; if (index > -1) { bookmarks.splice(index, 1); isBookmarked = false; Utils.showNotification('نشان درس حذف شد', 'info'); } else { bookmarks.push(lessonId); isBookmarked = true; Utils.showNotification('درس نشان شده', 'success'); } Utils.saveToLocalStorage('bookmarked_lessons', bookmarks); // به‌روزرسانی دکمه const bookmarkBtn = document.querySelector('.lesson-actions .btn-secondary'); if (bookmarkBtn) { bookmarkBtn.innerHTML = isBookmarked ? '📌 نشان شده' : '📌 نشان کردن'; } } destroy() { if (this.progressInterval) { clearInterval(this.progressInterval); } } } // ایجاد نمونه اصلی const lessonUI = new LessonUI();