const MONTHNAME = [ "gennaio", "febbraio", "marzo", "aprile", "maggio", "giugno", "luglio", "agosto", "settembre", "ottobre", "novembre", "dicembre"];
function counters() {

    fetch("https://golem.linux.it/wp/wp-json/wp/v2/posts")
    .then(response => response.json())
    .then(json => {
        let ul = document.getElementById("wp-latest-posts");
        for (let i = 0; i < 3; ++i) {
            let post = json[i];
            let li = document.createElement("li");
            let a = document.createElement("a");
            a.href = post.link;
            a.appendChild(document.createTextNode(post.title.rendered));
            li.appendChild(a);
            li.appendChild(document.createElement("br"));
            let datespan = document.createElement("span");
            datespan.style.fontSize = "smaller";
            let dateobj = new Date(post.date);
            datespan.appendChild(document.createTextNode(dateobj.getDate() + " " + MONTHNAME[dateobj.getMonth()] + " " + dateobj.getFullYear()));
            li.appendChild(datespan);
            ul.appendChild(li);
        }
    })
    .catch(err => console.error(err));

    fetch("https://golem.linux.it/wiki/api.php?action=query&list=recentchanges&rcprop=title&rclimit=50&format=json&origin=*")
    .then(response => response.json())
    .then(json => {
        let recentchanges = json.query.recentchanges;
        let rcs = new Set();
        for (let i = 0; i < recentchanges.length; ++i) {
            rcs.add(recentchanges[i].title);
        }
        let rca = Array.from(rcs);

        let ul = document.getElementById("mw-recent-changes");
        for (let i = 0; i < rca.length; ++i) {
            let title = rca[i];
            let li = document.createElement("li");
            let a = document.createElement("a");
            a.href = "https://golem.linux.it/wiki/" + title;
            a.appendChild(document.createTextNode(title));
            li.appendChild(a);
            ul.appendChild(li);
            if (i >= 4) break;
        }
    })
    .catch(function(error){console.log(error);});



    fetch("https://golem.linux.it/wp/wp-json/wp/v2/posts")
    .then(response => {
        let articles = response.headers.get("X-WP-Total");

        let p = document.getElementById("wordpress-articles");
        p.appendChild(document.createTextNode(articles + " articoli"));
    })
    .catch(err => console.error(err));

    fetch("https://golem.linux.it/wiki/api.php?action=query&meta=siteinfo&siprop=statistics&format=json&origin=*")
    .then(response => response.json())
    .then(json => {
        let pages = json.query.statistics.articles;

        let p = document.getElementById("mediawiki-pages");
        p.appendChild(document.createTextNode(pages + " pagine"));

    })
    .catch(err => console.error(err));

    fetch("https://git.golem.linux.it/api/v1/repos/search")
    .then(response => response.json())
    .then(response => {
        let repos = response.data.length;

        let p = document.getElementById("gitea-repos");
        p.appendChild(document.createTextNode(repos + " repository"));
    })
    .catch(err => console.error(err));

}