import { TTS } from '../framework/tts';
import { AriaOutput } from '../framework/tts/outputs/aria';
import Sound from './sound';

export default class Output {
    constructor() {
        this.tts = new TTS(new AriaOutput());
        this.history = document.getElementById("output-area");
        this.sound = new Sound();
    }

    say(string) {
        if (string === "") return;
        this.sound.play(`assets/scroll.wav`);
        const node = document.createElement("p");
        string.split("\n").forEach((line) => {
            node.appendChild(document.createTextNode(line));
            node.appendChild(document.createElement("br"));
            // this.tts.speak(line, false);
        });
        this.history.appendChild(node);
        // this.tts.speak(string);
        
        // Auto-scroll to bottom
        this.scrollToBottom();
    }

    scrollToBottom() {
        // Find the log area container
        const logArea = document.querySelector('.log-area');
        if (logArea) {
            // Use requestAnimationFrame for smooth scrolling
            requestAnimationFrame(() => {
                logArea.scrollTop = logArea.scrollHeight;
            });
        }
    }

    play(file) {
        this.sound.play(file);
    }

    setAmbience(file) {
        return this.sound.setAmbience(file);
    }

    setMusic(file) {
        return this.sound.setMusic(file);
    }

    setImpulse(file) {
        this.sound.setImpulse(file);
    }
}