var urlPapers = "/api/1.0/papers";
var urlPaperStatuses = "/api/1.0/papers/statuses";
var urlDeletePaper = "/api/1.0/papers/";

function showPapers(papersElement, paperRowClass) {
    getFromRest(urlPapers, function (paperList) {
        paperList.forEach(function (paper, index) {
            $(papersElement).parent().append("<div class='row text-left paper-row'>" +
                "                <div class='col'>" +
                "                <span class='fa-stack fa-1x'>\n" +
                "                  <i class='fa fa-circle fa-stack-2x " + getPaperStatusClass(paper.status) + "'></i>" +
                "                  <i class='fa fa-file-text-o fa-stack-1x fa-inverse'></i>" +
                "                </span>" +
                "                    <a href='paper?id=" + paper.id + "" +
                "'><span>" + (index + 1) + ". " + paper.title + "</span></a>" +
                "<span class='remove-paper d-none pull-right' onclick=\"deletePaper(" + paper.id + ",'" + papersElement + "', '" + paperRowClass + "')\">" +
                "<i class=\"fa fa-trash\" aria-hidden=\"true\"></i></span>" +
                "</div></div>");
        });

        $(paperRowClass).mouseenter(function (event) {
            var paperRow = $(event.target).closest(paperRowClass);
            $(paperRow).css("background-color", "#f8f9fa");
            $(paperRow).find(".remove-paper").removeClass("d-none");

        });
        $(paperRowClass).mouseleave(function (event) {
            var paperRow = $(event.target).closest(paperRowClass);
            $(paperRow).css("background-color", "white");
            $(paperRow).closest(paperRowClass).find(".remove-paper").addClass("d-none");
        });
    });
}

function deletePaper(id, papersElement, paperRowClass) {
    $("#remove-paper-modal").modal('show');

    $("#modal-btn-yes").on("click", function () {
        deleteFromRest(urlDeletePaper + id, function () {
            showFeedbackMessage("Статья удалена");
            $(paperRowClass).remove();
            showPapers(papersElement, paperRowClass);
        });
        $("#remove-paper-modal").modal('hide');
    });

    $("#modal-btn-no").on("click", function () {
        $("#remove-paper-modal").modal('hide');
    });
}

function addPaper(title, status, comment, locked) {
    var paperData = JSON.stringify({
        "title": title,
        "status": status,
        "comment": comment,
        "locked": locked
    });
    postToRest(urlPapers, paperData, function (data) {
        alert(data);
    });
}

function getPaperStatusClass(status) {
    switch (status) {
        case 'DRAFT':
            return "text-draft"
        case 'ON_PREPARATION':
            return "text-primary";
        case 'COMPLETED':
            return "text-success";
        case 'ATTENTION':
            return "text-warning";
        default:
            return "";
    }
}

function showPaperDashboard(dashboardElement) {
    getFromRest(urlPapers, function (paperList) {
        paperList.forEach(function (paper, index) {
            $(dashboardElement).append("<div class=\"col-12 col-sm-12 col-md-12 col-lg-4 col-xl-3 dashboard-card\">" +
                "<div class=\"row\">" +
                "<div class=\"col-2\">" +
                "<span class=\"fa-stack fa-1x\">" +
                "<i class=\"fa fa-circle fa-stack-2x " + getPaperStatusClass(paper.status) + "\"></i>" +
                "<i class=\"fa fa-file-text-o fa-stack-1x fa-inverse\"></i>" +
                "</span>" +
                "</div>" +
                "<div class=\"col col-10 text-right\">" +
                "<h7 class=\"service-heading\">" + paper.title + "</h7>" +
                "<p class=\"text-muted\">" + paper.authorsString + "</p>" +
                "</div>" +
                "</div>" +
                "</div>");
        });
    });
}