let url = "https://jsonplaceholder.typicode.com/posts/1/comments";
let eredmeny = document.getElementById("eredmeny");

async function adatlekeres(url) {
    try{
        let response = await fetch(url);
        if(!response.ok){
            throw new Error("HIBA, a response nem OK");
        } else{
            let data = await response.json();
            return data;
        }
    } catch{
        console.log("Hiba történt!");
    }    
}

function createDomElement(element) {
    let line = document.createElement("div");
    line.innerHTML = `ID: ${element.id}, Email: ${element.email}`;
    eredmeny.appendChild(line);
}

async function selectSpecifiedData(dataset){
    dataset.forEach(element => {
        if (element.id == 1 || element.id == 3 || element.id == 5){
            createDomElement(element);
        }
    });
}

function createDomElementFullDescription(element){
    let line = document.createElement("pre");
    line.innerHTML = element.commentInfok();
    eredmeny.appendChild(line);
}


class Comment{
    constructor(id, nev, email, tartalom){
        this.id = id;
        this.nev = nev;
        this.email = email;
        this.tartalom = tartalom;
    }

    commentInfok(){
        return `A komment azonosítója: ${this.id}, neve: ${this.nev}, hozzátársuló email cím: ${this.email}, komment tartalma: ${this.tartalom}\n`;
    }


}

class CommentGyujtemeny{
    commentek = [];

    constructor(commentek){
        this.commentek = commentek;
    }

    commentBegyujtes(comment){
        this.commentek.push(comment);
    }

    megjelenites() {
        this.commentek.forEach(element => {
            createDomElementFullDescription(element);
        })
    }
}

function returnAllCommentsAsObject(dataset){
    let commentek = [];
    dataset.forEach(element => {
        if (element.id == 1 || element.id == 3 || element.id == 5){
            commentek.push(new Comment(element.id, element.name, element.email, element.body));
        }
    })
    return commentek;
}


async function main(){
    let adatok = await adatlekeres(url);
    //await selectSpecifiedData(adatok);
    let commentek = returnAllCommentsAsObject(adatok);
    let commentGyujtemeny  = new CommentGyujtemeny(commentek);
    commentGyujtemeny.megjelenites();    
}


main();