class LearningPlatform { constructor() { this.currentDay = 1; this.maxDays = 3; this.isInitialized = false; this.components = {}; } async initialize() { try { console.log('🚀 Initializing Learning Platform...'); // مقداردهی اولیه سیستم احراز هویت this.initializeAuth(); // دیباگ اطلاعات احراز هویت this.debugAuth(); // بارگذاری کامپوننتها this.loadComponents(); // راهاندازی event listeners this.setupEventListeners(); // بارگذاری محتوای اولیه await this.loadInitialContent(); // بهروزرسانی UI this.updateUI(); this.isInitialized = true; console.log('✅ Platform initialized successfully'); } catch (error) { console.error('❌ Failed to initialize platform:', error); this.showError('خطا در بارگذاری برنامه. لطفاً صفحه را رفرش کنید.'); } } initializeAuth() { console.log('🔐 Initializing authentication system...'); // مقداردهی اولیه کاربران پیشفرض if (typeof authManager !== 'undefined') { authManager.initializeDefaultUsers(); console.log('👥 Default users initialized'); } else { console.error('❌ authManager is not defined'); } // مقداردهی اولیه میانافزار if (typeof AuthMiddleware !== 'undefined') { AuthMiddleware.initializeApp(); } // اعمال تم ذخیره شده const savedTheme = localStorage.getItem('theme'); if (savedTheme) { document.body.setAttribute('data-theme', savedTheme); } } debugAuth() { console.log('🔍 Auth Debug Info:'); console.log('- Token:', authManager?.getToken()); console.log('- Current User:', authManager?.getCurrentUser()); console.log('- All Users:', authManager?.getUsers()); console.log('- Is Authenticated:', authManager?.isAuthenticated()); } loadComponents() { console.log('🔧 Loading components...'); // بارگذاری کامپوننتهای اصلی this.components = { header: typeof HeaderComponent !== 'undefined' ? new HeaderComponent() : null, modal: typeof ModalComponent !== 'undefined' ? new ModalComponent() : null, navigation: typeof NavigationComponent !== 'undefined' ? new NavigationComponent() : null, lessonUI: typeof LessonUI !== 'undefined' ? new LessonUI() : null, quizUI: typeof QuizUI !== 'undefined' ? new QuizUI() : null, exerciseUI: typeof ExerciseUI !== 'undefined' ? new ExerciseUI() : null }; console.log('✅ Components loaded:', Object.keys(this.components)); // رندر کامپوننتها if (this.components.header) { this.components.header.render(); } if (this.components.navigation) { this.components.navigation.render(); } } setupEventListeners() { console.log('🎯 Setting up event listeners...'); // مدیریت تغییر روز document.addEventListener('dayChanged', (event) => { this.handleDayChange(event.detail.day); }); // مدیریت وضعیت احراز هویت document.addEventListener('authStateChanged', () => { this.handleAuthStateChange(); }); // مدیریت تکمیل درس document.addEventListener('lessonCompleted', (event) => { this.handleLessonCompleted(event.detail); }); // مدیریت ارسال تمرین document.addEventListener('exerciseSubmitted', (event) => { this.handleExerciseSubmitted(event.detail); }); // مدیریت بهروزرسانی پیشرفت document.addEventListener('progressUpdated', () => { this.handleProgressUpdate(); }); // مدیریت کلیک روی دکمههای ناوبری document.addEventListener('click', (e) => { if (e.target.matches('[data-day]')) { const day = parseInt(e.target.getAttribute('data-day')); this.handleDayChange(day); } if (e.target.matches('.next-day')) { this.nextDay(); } if (e.target.matches('.prev-day')) { this.previousDay(); } }); // جلوگیری از ارسال فرمها document.addEventListener('submit', (e) => { if (e.target.tagName === 'FORM') { e.preventDefault(); } }); // مدیریت خطاهای全局 window.addEventListener('error', (event) => { console.error('Global error:', event.error); }); window.addEventListener('unhandledrejection', (event) => { console.error('Unhandled promise rejection:', event.reason); }); } async loadInitialContent() { console.log('📚 Loading initial content...'); // نمایش وضعیت loading this.showLoadingState(); try { // بارگذاری محتوا await this.loadLesson(); await this.loadQuiz(); await this.loadExercise(); } catch (error) { console.error('Error loading content:', error); } finally { // پنهان کردن وضعیت loading this.hideLoadingState(); } } async loadLesson() { try { console.log(`📖 Loading lesson for day ${this.currentDay}...`); // شبیهسازی بارگذاری داده const lessonData = await this.simulateLessonLoad(); const lessonContent = document.getElementById('lessonContent'); if (lessonContent) { lessonContent.innerHTML = this.renderLesson(lessonData); } } catch (error) { console.error('Error loading lesson:', error); this.showContentError('lessonContent', 'خطا در بارگذاری درس'); } } async loadQuiz() { try { console.log(`❓ Loading quiz for day ${this.currentDay}...`); // شبیهسازی بارگذاری داده const quizData = await this.simulateQuizLoad(); const quizForm = document.getElementById('quizForm'); if (quizForm) { quizForm.innerHTML = this.renderQuiz(quizData); } } catch (error) { console.error('Error loading quiz:', error); this.showContentError('quizForm', 'خطا در بارگذاری آزمون'); } } async loadExercise() { try { console.log(`✏️ Loading exercise for day ${this.currentDay}...`); // شبیهسازی بارگذاری داده const exerciseData = await this.simulateExerciseLoad(); const exerciseContent = document.getElementById('exerciseContent'); if (exerciseContent) { exerciseContent.innerHTML = this.renderExercise(exerciseData); } } catch (error) { console.error('Error loading exercise:', error); this.showContentError('exerciseContent', 'خطا در بارگذاری تمرین'); } } // شبیهسازهای بارگذاری داده simulateLessonLoad() { return new Promise((resolve) => { setTimeout(() => { resolve({ title: `درس روز ${this.currentDay}: مقدمات توسعه وب`, content: `این محتوای آموزشی برای روز ${this.currentDay} است. در این درس با مفاهیم پایه توسعه وب آشنا میشوید.`, points: 10 }); }, 500); }); } simulateQuizLoad() { return new Promise((resolve) => { setTimeout(() => { resolve({ question: `سوال روز ${this.currentDay}: HTML چیست؟`, options: [ 'زبان برنامهنویسی', 'زبان نشانهگذاری', 'پایگاه داده', 'فریمورک CSS' ], correctAnswer: 1 }); }, 500); }); } simulateExerciseLoad() { return new Promise((resolve) => { setTimeout(() => { resolve({ title: `تمرین روز ${this.currentDay}`, description: 'یک صفحه HTML ساده ایجاد کنید.', keywords: ['html', 'body', 'title'], points: 15 }); }, 500); }); } // رندررهای ساده renderLesson(data) { return `
${data.content}
${data.question}
${data.description}
برای ذخیره پیشرفت وارد شوید
👋 برای مشاهده پیشرفت خود وارد شوید