class AICoachCard extends HTMLElement { constructor() { super(); this.defaultMessage = "วันนี้คุณฟื้นตัวเต็มที่! เหมาะสำหรับการออกกำลังกายระดับกลาง-หนัก อย่าลืมทานอาหารให้ครบ 5 หมู่นะคะ"; this.currentMessage = this.defaultMessage; } connectedCallback() { this.attachShadow({ mode: 'open' }); this.render(); this.startTypingAnimation(); } render() { this.shadowRoot.innerHTML = `
AI Coach พิมพ์
อัจฉริยะ
${this.currentMessage}
`; } // Public API method updateMessage(message, actions = []) { try { this.currentMessage = message; this.isTyping = true; this.render(); // Simulate typing delay setTimeout(() => { try { this.isTyping = false; this.render(); this.updateTimestamp(); this.renderActions(actions); } catch (error) { console.error('AI Coach render error:', error); } }, 1000 + Math.random() * 1500); } catch (error) { console.error('AI Coach update message error:', error); } } renderActions(actions) { const container = this.shadowRoot.getElementById('actionButtons'); if (actions.length === 0) { container.innerHTML = ''; return; } container.innerHTML = actions.map(action => ` `).join(''); // Add event listeners container.querySelectorAll('.action-btn').forEach(btn => { btn.addEventListener('click', (e) => { const actionId = e.target.dataset.action; this.handleAction(actionId); }); }); } updateTimestamp() { const now = new Date(); const timestamp = this.shadowRoot.getElementById('timestamp'); timestamp.textContent = `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`; } generateSmartMessage() { try { const templates = [ { condition: () => AppState && AppState.currentUser && AppState.currentUser.recovery > 80, message: "วันนี้คุณฟื้นตัวเต็มที่! เหมาะสำหรับการออกกำลังกายระดับกลาง-หนัก", actions: [ { id: 'workout', label: 'เริ่มออกกำลัง', type: 'primary' }, { id: 'nutrition', label: 'วางแผนมื้ออาหาร', type: 'secondary' } ] }, { condition: () => AppState && AppState.currentUser && AppState.currentUser.recovery < 50, message: "วันนี้ร่างกายของคุณยังฟื้นตัวไม่เต็มที่ ควรพักผ่อนเพิ่มเติม", actions: [ { id: 'meditation', label: 'นั่งสมาธิ', type: 'primary' }, { id: 'sleep', label: 'ดูคุณภาพการนอน', type: 'secondary' } ] }, { condition: () => AppState && AppState.currentUser && AppState.currentUser.strain > 15, message: "ความเครียดสะสมอยู่ในระดับสูง แนะนำให้ทำกิจกรรมผ่อนคลาย", actions: [ { id: 'breathing', label: 'หายใจลึกๆ', type: 'primary' }, { id: 'yoga', label: 'โยคะคลายเครียด', type: 'secondary' } ] } ]; const match = templates.find(t => { try { return t.condition(); } catch (e) { return false; } }); if (match) { this.updateMessage(match.message, match.actions); } } catch (error) { console.error('Generate smart message error:', error); } } handleAction(actionId) { switch(actionId) { case 'workout': window.location.href = 'activity.html'; break; case 'nutrition': window.location.href = 'nutrition.html'; break; case 'meditation': showNotification('กำลังเปิดการฝึกสมาธิ...', 'success'); break; case 'sleep': window.location.href = 'sleep.html'; break; case 'breathing': this.startBreathingExercise(); break; case 'yoga': showNotification('กำลังเปิดวิดีโอโยคะ...', 'success'); break; default: showNotification('ฟีเจอร์นี้กำลังพัฒนา...', 'info'); } } startBreathingExercise() { const modal = document.createElement('div'); modal.className = 'fixed inset-0 bg-black/50 z-50 flex items-center justify-center p-4'; modal.innerHTML = `

ฝึกหายใจลึกๆ

เริ่ม

หายใจเข้าลึกๆ 4 วินาที กลั้น 4 วินาที หายใจออก 4 วินาที

`; document.body.appendChild(modal); // Breathing animation logic would go here this.runBreathingCycle(modal); } runBreathingCycle(modal) { const circle = modal.querySelector('#breathingCircle'); const text = modal.querySelector('#breathingText'); const phases = ['หายใจเข้า', 'กลั้น', 'หายใจออก', 'กลั้น']; let phase = 0; const interval = setInterval(() => { text.textContent = phases[phase]; circle.style.transform = phase === 0 ? 'scale(1.3)' : phase === 2 ? 'scale(0.8)' : 'scale(1)'; phase = (phase + 1) % 4; }, 4000); modal.addEventListener('remove', () => clearInterval(interval)); } connectedCallback() { this.attachShadow({ mode: 'open' }); this.currentMessage = this.defaultMessage; this.isTyping = false; this.render(); this.updateTimestamp(); this.startTypingAnimation(); this.setupEventListeners(); } startTypingAnimation() { // Simulate AI thinking periodically setInterval(() => { if (Math.random() > 0.7) { this.generateSmartMessage(); } }, 30000); // Every 30 seconds } setupEventListeners() { // Listen for health data updates window.addEventListener('health-data-updated', (e) => { this.generateSmartMessage(); }); } } // Register the custom element customElements.define('ai-coach-card', AICoachCard);