// AGPL-3.0-only
//
// This is free software for the public good of a permacomputer
// hosted at permacomputer.com, an always-on computer by the people, for the
// people. One which is durable, easy to repair, and distributed like tap water
// for machine learning intelligence.
//
// The permacomputer is community-owned infrastructure optimized around
// four values:
//
//   TRUTH    - First principles, math & science, open source code
//   FREEDOM  - Voluntary partnerships, freedom from tyranny & corporate control
//   HARMONY  - Minimal waste, self-renewing systems with diverse connections
//   LOVE     - Be yourself without hurting others, cooperation through natural law
//
// This software contributes to that vision by making machine learning
// accessible to everyone through a free, open, embeddable chat interface.
// Code is seeds to sprout on any abandoned technology.
//
// Learn more: https://www.permacomputer.com
//
// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND.
//
// Copyright 2025-2026 TimeHexOn & foxhop & russell@unturf
// https://www.permacomputer.com

// Page reading functionality
import { extractWebpageContent } from "./content.js";
import { processContentWithHermes, speakText } from "./tts.js";
import { getUIText } from "./ui-translations.js";

// Function to read the entire page using Hermes
export async function readPageWithHermes(button = null) {
	const content = await extractWebpageContent();

	// Update button tooltip instead of creating separate DOM element
	if (button) {
		button.title = getUIText("processingContent");
	}

	const processedContent = await processContentWithHermes(content);

	// Get voice preference from settings (format: "model:voice", e.g., "tts-1-qwen:onyx")
	let voice = "alloy";
	let model = "tts-1-qwen";
	try {
		const savedVoice = localStorage.getItem("uncloseai_selected_voice");
		if (savedVoice && savedVoice.includes(":")) {
			const parts = savedVoice.split(":");
			model = parts[0];
			voice = parts[1];
		} else if (savedVoice) {
			voice = savedVoice;
		}
	} catch (error) {
		console.warn("Failed to read voice preference:", error);
	}

	// Generate TTS with configured voice and 90% speed immediately
	const { audio, blob } = await speakText(processedContent, voice, 0.9, model);

	if (button) {
		button.title = getUIText("readingPageClickToPause");
	}

	let isPaused = false;

	// Make the button interactive for pause/resume
	if (button) {
		const originalEmoji = button.textContent;
		const originalOnclick = button.onclick;
		
		button.onclick = () => {
			if (isPaused) {
				audio.play();
				button.title = getUIText("readingPageClickToPause");
				button.textContent = originalEmoji;
			} else {
				audio.pause();
				button.title = getUIText("pausedClickToResume");
				button.textContent = "⏸️";
			}
			isPaused = !isPaused;
		};

		// Set up audio end handler
		audio.onended = () => {
			button.title = getUIText("readPage");
			button.textContent = originalEmoji;
			button.onclick = originalOnclick;
		};
	}

	// Start playing immediately
	audio.play();
	
	// Return audio and blob for download functionality
	return { audio, blob };
}
