const rawText = `Erik Miller is a travel Journalist. He works during the whole year traveling to different places around the world. He works for an important magazine. He takes pictures and writes about the places he visits.

He has a meeting with his boss at 7:30 a.m. in the office. They talk about the next destinations and plan the trip, then the secretary buys the tickets and he is ready to pack his clothes and go to the airport. He usually takes a taxi to go to the airport and then he waits for the flight. On the plane he thinks about his trip, reads and listens to music.

When he is traveling, he wakes up at 7:00 a.m. and has breakfast in the hotel where he is staying. He never leaves his camera when he goes out. He visits places people recommend and takes pictures of everything. He has lunch at 1:00 p.m. in the most typical places. He travels during weeks or months.

After the trip, when he is back at the office, he starts working at 8:00 a.m. He has meetings with his co-workers and chooses the pictures for the magazine. He also writes articles to help other travelers. He writes tips about how to get to different places, how to order food, what and where the best restaurants and hotels are.

Erik always feels happy when he sees his articles on the magazines and newspapers.`;

const verbs = [
  "is", "works", "traveling", "takes", "writes", "visits", "has", "talk", "plan", "buy", "pack", "go", "waits",
  "thinks", "reads", "listens", "wakes", "staying", "leaves", "goes", "recommend", "start", "chooses", "help",
  "feels", "sees"
];

const adverbs = ["usually", "never", "always"];

const prepositions = ["to", "at", "in", "on", "with", "about", "during", "after", "of", "where"];

const textContainer = document.getElementById("text-content");

function getCategory(word) {
  const cleanWord = word.toLowerCase().replace(/[.,!?]/g, "");
  if (verbs.includes(cleanWord)) return "verb";
  if (adverbs.includes(cleanWord)) return "adverb";
  if (prepositions.includes(cleanWord)) return "preposition";
  return null;
}

function createWordSpan(word) {
  const span = document.createElement("span");
  span.classList.add("word");
  span.textContent = word;

  span.onclick = () => {
    const category = getCategory(word);
    if (category) {
      span.classList.toggle(category);
    }
  };

  return span;
}

function renderText() {
  const paragraphs = rawText.trim().split(/\n\s*\n/);
  paragraphs.forEach(paragraph => {
    const p = document.createElement("p");
    const words = paragraph.split(/(\s+)/); // Keep spaces
    words.forEach(word => {
      if (word.trim().length === 0) {
        p.appendChild(document.createTextNode(word));
      } else {
        p.appendChild(createWordSpan(word));
      }
    });
    textContainer.appendChild(p);
  });
}

renderText();


const allAnswers = {
  // Sección de verdadero/falso
  q1: "T", j1: "Erik travels during weeks or months",
  q2: "F", j2: "He travels alone",
  q3: "T", j3: "He feels happy when he sees his articles in magazines",
  q4: "F", j4: "He takes a taxi to go to the airport",
  q5: "F", j5: "He has breakfast at the hotel",

  // Sección de verbos bajo imágenes
  exercise541: "pack",
  exercise542: "listen",
  exercise543: "buy",
  exercise544: "read",
  exercise545: "choose",

  // Sección de preguntas con presente simple
  exercise551: "does",
  exercise552: "visit",
  exercise553: "do",
  exercise554: "travel",
  exercise555: "does",
  exercise556: "take",
  exercise557: "does",
  exercise558: "do",
  exercise559: "does",
  exercise5510: "have",
  exercise5511: "do",
  exercise5512: "buy"
};
function validateInput(input, correctValue) {
  const userValue = input.value.trim().toLowerCase();
  const expected = correctValue.trim().toLowerCase();

  if (userValue === expected) {
    input.classList.add("correct");
    input.classList.remove("incorrect");
  } else {
    input.classList.add("incorrect");
    input.classList.remove("correct");
  }
}
Object.keys(allAnswers).forEach(id => {
  const input = document.getElementById(id);
  if (input) {
    input.addEventListener("input", () => {
      validateInput(input, allAnswers[id]);
    });
  }
});


function playHoverSound() {
  const hoverSound = document.getElementById("hover-sound");
  hoverSound.currentTime = 0;
  hoverSound.play();
}

