// ==UserScript==
// @name         CF Get Problems
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add contest problems altogether with rating.
// @author       Dhanraj Chaurasia (coderdhanraj) / Fixed by KNN-07
// @license      GPLv3
// @match        https://codeforces.com/*
// ==/UserScript==
(function () {
    const url = window.location.href;

    const match1 = url.match(/contest\/(\d+)/);
    const match2 = url.match(/problemset\/problem\/(\d+)/);
    var contestNumber = null

    if (match1) contestNumber = match1[1];
    else if (match2) contestNumber = match2[1];
    else contestNumber = -1

    if (contestNumber != -1) {
        const requestURL = `https://codeforces.com/api/contest.standings?contestId=${contestNumber}&from=1&count=1`;
        var userHandle = document.querySelector(".lang-chooser").childNodes[3].children[0].textContent;
        var requestURL2 = `https://codeforces.com/api/contest.status?contestId=${contestNumber}&handle=${userHandle}`;;
        fetchData();
        async function fetchData() {
            const Verdict = {
                AC: "rgb(64, 255, 64)",
                WA: "rgb(255, 0, 0)",
                NA: "#0d9aff",
            };
            const problems = [], verdicts = {};
            try {
                const response = await fetch(requestURL);
                const data = await response.json();
                const problemsList = data.result.problems;
                const response2 = await fetch(requestURL2);
                const data2 = await response2.json();
                if (data2.status === "OK") {
                    const res = data2.result;
                    for (var k = 0; k < res.length; k++) {
                        const index = res[k].problem.index;
                        const curVerdict = (res[k].verdict == "OK" ? Verdict["AC"] : Verdict["WA"]);
                        if (!(index in verdicts) || (index in verdicts && verdicts[index] != Verdict["AC"])) verdicts[index] = curVerdict;
                    }
                }
                if (data.status == "OK") {
                    for (var i = 0; i < problemsList.length; i++) {
                        const index = problemsList[i].index;
                        const title = index + " - " + problemsList[i].name;
                        const rating = problemsList[i].rating;
                        const verdict = (index in verdicts ? verdicts[index] : Verdict["NA"])
                        const problemUrl = `https://codeforces.com/contest/${contestNumber}/problem/${index}`;
                        problems.push({ index: index, url: problemUrl, title: title, rating: rating, verdict: verdict });
                    }
                }
            }
            catch (e) {
            }
            var toInsert;
            if (problems) {
                toInsert = `
                <div class="roundbox sidebox" style="">
                    <div class="roundbox-lt">&nbsp;</div>
                    <div class="roundbox-rt">&nbsp;</div>
                    <div class="caption titled">→ Contest Problems
                        <i class="sidebar-caption-icon las la-angle-down" onclick="
                            if (this.classList.contains('la-angle-right')) {
                                document.getElementById('Tagblock').style.display = 'block';
                                this.classList.add('la-angle-down');
                                this.classList.remove('la-angle-right');
                            } else {
                                document.getElementById('Tagblock').style.display = 'none';
                                this.classList.add('la-angle-right');
                                this.classList.remove('la-angle-down');
                            }">
                        </i>
                        <div class="top-links"></div>
                    </div>
                    <div id="Tagblock" style="display: block;">
                    <div style="display: flex; margin: 8px auto; flex-wrap: wrap; justify-content: center; align-items: center; text-align: center;">
                `
                problems.forEach(e => {
                    toInsert += `
                <span style="width:3em; margin: 2px; text-align: center; box-sizing: border-box;">
                    <a title="${e.title}" href="${e.url}" style="color:${e.verdict}">${e.index}</a>
                    <br><span class="small" title="Problem Rating">${e.rating == null ? '-' : e.rating}</span>
                </span>
                `
                });
                toInsert += '</div></div>'
            }
            const getProblemBox = document.createElement("div");
            getProblemBox.innerHTML = toInsert;
            document.querySelector("#sidebar").prepend(getProblemBox);
        }
    }
})();