let selectedSports = []
let paused = false;

let filters = {
    searchedWords: [],
    blockedWords: [],
    sorted: "time",
    order: "desc",
    sports: []
}

// smartly removes one of the feeds when the window is too narrow for both feeds to be shown properly
// it will swap between the "recent" and "filtered" feeds depending on if a filter is active
function showFeeds() {
    if ($(window).width() < 1280) {
        if(filters.sports.length === 0 && filters.blockedWords.length === 0 && filters.searchedWords.length === 0 && (filters.sorted == "time" && filters.order == "desc")) {
            $(".leftcol").show()
            $(".centercol").hide()
        } else {
            $(".leftcol").hide()
            $(".centercol").show()
        }
    } else {
        $(".leftcol").show()
        $(".centercol").show()
    }
}

// outputs HTML of a tweet based on the tweet that gets given as input
function tweetCell(tweet) {
    const date = new Date(tweet.created_at);
    let minutes = date.getMinutes();

    if (minutes < 10) {
        minutes = "0" + minutes
    }


    var cell = document.createElement("div");
    cell.innerHTML =
    `<div class="tweet">
        <a href ="https://www.twitter.com/${tweet.user.screen_name}"><img src="${tweet.user.profile_image_url}" onerror="this.src='media/logo.png';" class="tweet-profilepicture"></a>
        <div class="tweet-content"> 
            <div class="tweet-name">
                <div>
                    <a class="tweet-nickname" href = "https://www.twitter.com/${tweet.user.screen_name}" target = "_blank">${tweet.user.name}</a>
                    <a class="tweet-username" href = "https://www.twitter.com/${tweet.user.screen_name}" target = "_blank">@${tweet.user.screen_name}</a>
                </div>
                <span class="tweet-date"><em>${date.getHours()}:${minutes}</em></span> 
            </div>
            <a class="tweet-text" href = "https://www.twitter.com/${tweet.user.screen_name}/status/${tweet.id_str}" target = "_blank">${tweet.text}</a>
            <div class="tweet-interactions">
                <div class="tweet-interaction tweet-likes">
                    <span class="material-symbols-outlined">favorite</span>
                    <span>${tweet.favorite_count}</span>
                </div>
                <div class="tweet-interaction tweet-retweets">
                    <span class="material-symbols-outlined">repeat</span>
                    <span>${tweet.retweet_count}</span>
                </div>
                <div class="tweet-interaction tweet-replies">
                    <span class="material-symbols-outlined">chat_bubble</span>
                    <span>${tweet.reply_count}</span>
                </div>
            </div>
        </div>
    </div>`
    return cell
}

// applies all filters to a list of tweets
function filterTweets(tweets, bannedWords=[], searchWords=[], sorted, order) {
    if(bannedWords.length == 1 && bannedWords[0] == ""){
        bannedWords = []
        filters.blockedWords = []
    }
    if(searchWords.length == 1 && searchWords[0] == ""){
        searchWords = []
        filters.searchedWords = []
    }
    if(bannedWords.length > 0){
        tweets = filterBannedWords(bannedWords, tweets)
    }
    if(searchWords.length > 0){
        tweets = searchForWords(searchWords, tweets)
    }
    return tweets
}

// iterates through each tweet in the list and removes any tweets that contain any of the given words in their text
function filterBannedWords(bannedWords, tweets){
    for (let i = 0; i < tweets.length; i++) {
        var tweet = tweets[i]
        for(let j=0; j < bannedWords.length; j++){
            if (tweet.text.toLowerCase().includes(bannedWords[j].toLowerCase())){
                tweets.splice(i, 1)
                break
            }
        }
    }
    return tweets
}

// iterates through each tweet in the list and removes any tweets that do not contain any of the given words in their text
function searchForWords(searchWords, tweets){
    var filteredTweets = []
    for (let i = 0; i < tweets.length; i++) {
        var tweet = tweets[i]
        for(let i=0; i < searchWords.length; i++){
            var text = tweet.text.toLowerCase()
            var word = searchWords[i].toLowerCase()
            if (text.includes(word)) {
                filteredTweets.push(tweet)
                break
            }
        }
    }
    return filteredTweets.reverse()
}

// sorts through a list of tweets based on an array with options (sort)
function sortTweets(tweets, sort){
    if(typeof sort.sortby == 'undefined'){
        return tweets
    }
    if(typeof sort.order == 'undefined'){
        sort.order = 'asc'
    }

    if(sort.sortby == "likes"){
        if(sort.order == 'asc'){
            tweets.sort(function(a,b){return b.favorite_count-a.favorite_count})
        } else{
            tweets.sort(function(a,b){return a.favorite_count-b.favorite_count})
        }
    }
    if(sort.sortby == "retweets"){
        if(sort.order == 'asc'){
            tweets.sort(function(a,b){return b.retweet_count-a.retweet_count})
        } else{
            tweets.sort(function(a,b){return a.retweet_count-b.retweet_count})
        }
    }
    if(sort.sortby == "replies"){
        if(sort.order == 'asc'){
            tweets.sort(function(a,b){return b.reply_count-a.reply_count})
        } else {
            tweets.sort(function(a,b){return a.reply_count-b.reply_count})
        }
    }
    if (sort.sortby == "time"){
        if(sort.order == 'asc'){
            tweets.reverse()
        } else{
            tweets.sort(function(a,b){return Date(a.created_at)-Date(b.reply_count)})
        }
    }
    return tweets
}
