let messages = [
    "I wondered why the baseball was getting bigger. Then it hit me.",
    "Police were called to a day care, where a three-year-old was resisting a rest.",
    "Did you hear about the guy whose whole left side was cut off? He’s all right now.",
    "The roundest knight at King Arthur’s round table was Sir Cumference.",
    "To write with a broken pencil is pointless.",
    "When fish are in schools they sometimes take debate.",
    "The short fortune teller who escaped from prison was a small medium at large.",
    "A thief who stole a calendar… got twelve months.",
    "A thief fell and broke his leg in wet cement. He became a hardened criminal.",
    "Thieves who steal corn from a garden could be charged with stalking.",
    "When the smog lifts in Los Angeles , U. C. L. A.",
    "The math professor went crazy with the blackboard. He did a number on it.",
    "The professor discovered that his theory of earthquakes was on shaky ground.",
    "The dead batteries were given out free of charge.",
    "If you take a laptop computer for a run you could jog your memory.",
    "A dentist and a manicurist fought tooth and nail.",
    "A bicycle can’t stand alone; it is two tired.",
    "A will is a dead giveaway.",
    "Time flies like an arrow; fruit flies like a banana.",
    "A backward poet writes inverse.",
    "In a democracy it’s your vote that counts; in feudalism, it’s your Count that votes.",
    "A chicken crossing the road: poultry in motion.",
    "If you don’t pay your exorcist you can get repossessed.",
    "With her marriage she got a new name and a dress.",
    "Show me a piano falling down a mine shaft and I’ll show you A-flat miner.",
    "When a clock is hungry it goes back four seconds.",
    "The guy who fell onto an upholstery machine was fully recovered.",
    "A grenade fell onto a kitchen floor in France and resulted in Linoleum Blownapart.",
    "You are stuck with your debt if you can’t budge it.",
    "Local Area Network in Australia : The LAN down under.",
    "He broke into song because he couldn’t find the key.",
    "A calendar’s days are numbered."
  ];
  
  let nicks = [
    "Edvard Force",
    "Диванный Воин",
    "Yoshka's Cat",
    "XAOSHammer",
    "Kino Konformist" ];
  
const chatwin = document.getElementById("chatwin");

function randomMessage() {
    return messages[(Math.random() * messages.length) | 0];
}

function randomNick() {
    return nicks[(Math.random() * nicks.length) | 0];
}

function randomChat() {
    return Math.random() > 0.5 ? "tg" : "yt";
}

function scrollToBottom() {
    chatwin.scrollTop = chatwin.scrollHeight;
}

function removeOldMessages() {
    const children = Array.from(chatwin.children);
    while (children.length > 15) {
        chatwin.removeChild(children[0]);
    }
}

function createChatLine() {
    const nm = document.createElement("div");
    nm.className = "nameline " + randomChat();
    nm.textContent = randomNick();

    const msg = document.createElement("div");
    msg.className = "msgline";
    msg.textContent = randomMessage();

    const rw = document.createElement("div");
    rw.className = "chatRow";
    rw.appendChild(nm);
    rw.appendChild(msg);

    chatwin.appendChild(rw);
    scrollToBottom();
    removeOldMessages();
}

function createNewLine(json) {
    json.forEach(element => {
        const existing = document.getElementById(element.id);
        if (existing) {
            existing.querySelector('.msgline').textContent = element.msg;
            return;
        }

        const nm = document.createElement("div");
        nm.className = "nameline " + element.type;
        nm.textContent = element.sendr;

        if (element.type === "donate") {
            nm.innerHTML += `<br><p style="color: red">${element.amount}</p>`;
        }

        const msg = document.createElement("div");
        msg.className = "msgline";
        msg.textContent = element.msg;
        msg.id = element.id;

        if (element.type === "donate") {
            msg.style.backgroundColor = "#0000FF20";
        }

        const rw = document.createElement("div");
        rw.className = "chatRow";
        rw.appendChild(nm);
        rw.appendChild(msg);

        chatwin.appendChild(rw);
        scrollToBottom();
        removeOldMessages();
    });
}

function requestNewLines() {
    fetch("http://localhost:8008/")
        .then(response => response.json())
        .then(data => createNewLine(data))
        .catch(error => console.error('Ошибка:', error));
}

setInterval(requestNewLines, 1000);
// setInterval(createChatLine, 3000); 