/*
    Parses the blog script into something pretty.
*/
const pageName = location.href.split("/").slice(-1);
let maxView = 0; //Determines how many posts we can see preloaded
//See if we're on home page or blog viewer (only places we can see posts)
if(pageName[0] == 'news.html') {
    maxView = 5;
    document.querySelector('.bar-tag').classList.remove('hidden');
    document.querySelector('.bar-date').classList.remove('hidden');
} else {
    maxView = 1;
}

let lineCount = 0; //Keeps track of where we left off
let blogCount = 0;
const seeMoreLinks = document.querySelector('.see-more');

function createBlogEntries(lines) {
    let blogRef = null;
    let postElement = null;
    let currString = "";
    for(let i=lineCount; i<lines.length; i++) {
        let line = lines[i];
        lineCount = i;

        switch(line) {
            case '/<>/----------': //Create 
                blogCount+=1;
                blogRef = document.createElement("div");
                blogRef.classList.add('post');
                break;
            case 'Date:=':
                postElement = document.createElement('p');
                postElement.classList.add('postDate');
                break;
            case 'Title:=':
                postElement = document.createElement('p');
                postElement.classList.add('postTitle');
                break;
            case 'Content:=':
                postElement = document.createElement('p');
                postElement.classList.add('postContent');
                break;
            case 'Tag:=':
                postElement = document.createElement('p');
                postElement.classList.add('postTag');
                currString = "Tags: ";
                break;
            case '<->': //We reached an end of a specific section of a blog post
                postElement.innerHTML = currString; //Oh look it's innerHTML, but it's for my own stuff so whatever--no theoretical risk here.
                blogRef.appendChild(postElement);
                document.querySelector('.blog').appendChild(blogRef);
                currString = "";
                break;
            default: //We simply append to our postElement
                if(line.substring(0,4) != "<ul>")
                    currString += line+'<br>'; 
                else
                    currString += line;   
        }

        if(blogCount > maxView) {
            lineCount-=1; //We don't count this line
            count = 0;
            if(pageName[0]=='news.html') //Since we know there are more posts after the five we can view at most, we can display the option to show more links.
                seeMoreLinks.classList.remove('hidden'); //Implement later: When we click on 'see more', re-add the 'hidden' class by default.
            break;
        }
    }
}

fetch('blog.txt')
    .then(response => response.text())
    .then(function(text) {
        //We have our entire text and stored it in a reference variable
        const reference = text;
        //console.log(reference);

        //Now we split up our text into an array of strings with length equal to number of lines
        const lines = reference.split('\r\n');

        //We iterate through the lines and create elements for each detected blog post
        createBlogEntries(lines);
    })
    .catch(err => console.log("Couldn't find the blog file!  Where is it?")
);

/*
fetch('blog.txt')
  .then(response => response.text())
  .then(text => console.log(text))
*/