/**
* AegisSight Chat-Assistent Widget.
*/
const Chat = {
_conversationId: null,
_isOpen: false,
_isLoading: false,
_hasGreeted: false,
_tutorialHintDismissed: false,
_isFullscreen: false,
init() {
const btn = document.getElementById('chat-toggle-btn');
const closeBtn = document.getElementById('chat-close-btn');
const form = document.getElementById('chat-form');
const input = document.getElementById('chat-input');
if (!btn || !form) return;
btn.addEventListener('click', () => this.toggle());
closeBtn.addEventListener('click', () => this.close());
const resetBtn = document.getElementById('chat-reset-btn');
if (resetBtn) resetBtn.addEventListener('click', () => this.reset());
const fsBtn = document.getElementById('chat-fullscreen-btn');
if (fsBtn) fsBtn.addEventListener('click', () => this.toggleFullscreen());
form.addEventListener('submit', (e) => {
e.preventDefault();
this.send();
});
// Enter sendet, Shift+Enter für Zeilenumbruch
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
this.send();
}
});
// Auto-resize textarea
input.addEventListener('input', () => {
input.style.height = 'auto';
input.style.height = Math.min(input.scrollHeight, 120) + 'px';
});
},
toggle() {
if (this._isOpen) {
this.close();
} else {
this.open();
}
},
open() {
const win = document.getElementById('chat-window');
const btn = document.getElementById('chat-toggle-btn');
if (!win) return;
win.classList.add('open');
btn.classList.add('active');
this._isOpen = true;
if (!this._hasGreeted) {
this._hasGreeted = true;
this.addMessage('assistant', 'Hallo! Ich bin der AegisSight Assistent. Stell mir gerne jede Frage rund um die Bedienung des Monitors, ich helfe dir weiter.');
}
// Tutorial-Hinweis bei jedem Oeffnen aktualisieren (wenn nicht dismissed)
if (typeof Tutorial !== 'undefined' && !this._tutorialHintDismissed) {
var oldHint = document.getElementById('chat-tutorial-hint');
if (oldHint) oldHint.remove();
this._showTutorialHint();
}
// Focus auf Input
setTimeout(() => {
const input = document.getElementById('chat-input');
if (input) input.focus();
}, 200);
},
close() {
const win = document.getElementById('chat-window');
const btn = document.getElementById('chat-toggle-btn');
if (!win) return;
win.classList.remove('open');
win.classList.remove('fullscreen');
btn.classList.remove('active');
this._isOpen = false;
this._isFullscreen = false;
const fsBtn = document.getElementById('chat-fullscreen-btn');
if (fsBtn) {
fsBtn.title = 'Vollbild';
fsBtn.innerHTML = '';
}
},
reset() {
this._conversationId = null;
this._hasGreeted = false;
this._isLoading = false;
const container = document.getElementById('chat-messages');
if (container) container.innerHTML = '';
this._updateResetBtn();
this.open();
},
toggleFullscreen() {
const win = document.getElementById('chat-window');
const btn = document.getElementById('chat-fullscreen-btn');
if (!win) return;
this._isFullscreen = !this._isFullscreen;
win.classList.toggle('fullscreen', this._isFullscreen);
if (btn) {
btn.title = this._isFullscreen ? 'Vollbild beenden' : 'Vollbild';
btn.innerHTML = this._isFullscreen
? ''
: '';
}
},
_updateResetBtn() {
const btn = document.getElementById('chat-reset-btn');
if (btn) btn.style.display = this._conversationId ? '' : 'none';
},
async send() {
const input = document.getElementById('chat-input');
const text = (input.value || '').trim();
if (!text || this._isLoading) return;
input.value = '';
input.style.height = 'auto';
this.addMessage('user', text);
this._showTyping();
this._isLoading = true;
// Tutorial-Keywords abfangen
var lowerText = text.toLowerCase();
if (lowerText === 'rundgang' || lowerText === 'tutorial' || lowerText === 'tour' || lowerText === 'f\u00fchrung') {
this._hideTyping();
this._isLoading = false;
this.close();
if (typeof Tutorial !== 'undefined') Tutorial.start();
return;
}
try {
const body = {
message: text,
conversation_id: this._conversationId,
};
// Aktuelle Lage mitschicken falls geoeffnet
const incidentId = this._getIncidentContext();
if (incidentId) {
body.incident_id = incidentId;
}
const data = await this._request(body);
this._conversationId = data.conversation_id;
this._updateResetBtn();
this._hideTyping();
this.addMessage('assistant', data.reply);
this._highlightUI(data.reply);
} catch (err) {
this._hideTyping();
const msg = err.detail || err.message || 'Etwas ist schiefgelaufen. Bitte versuche es erneut.';
this.addMessage('assistant', msg);
} finally {
this._isLoading = false;
}
},
addMessage(role, text) {
const container = document.getElementById('chat-messages');
if (!container) return;
const bubble = document.createElement('div');
bubble.className = 'chat-message ' + role;
// Einfache Formatierung: Zeilenumbrueche und Fettschrift
const formatted = text
.replace(/&/g, '&')
.replace(//g, '>')
.replace(/\*\*(.+?)\*\*/g, '$1')
.replace(/\n/g, '
');
bubble.innerHTML = '