var Grades = {};

var notifyGComponents = function() {
    $(GradeEntity).trigger('GradeHasChanged');
};

var GradeEntity = {

    doneLoading: false,
    getGrade: function() {
        return Grades;
    },

    loadGrades: function() {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getGradesInfo",
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyGComponents();
            }
        });
    },

    loadGrade: function(studentid, judgeid) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getGrade/"+studentid+"/"+judgeid,
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyGComponents();
            }
        });
    },
    toggleGrade: function(id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/toggleGrade/" + id,
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
        });
    },
    shuffleGrade: function(id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/shuffleStudents/" + id,
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyGComponents();
            }
        });
    },
    assignStudents: function(judgeid) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/assignStudents/" + judgeid,
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
        });
    },
    UpdateGrade: function(info) {
        if(info.studentid != undefined)
        {
        var loadRequest = $.ajax({
            type: 'PUT',
            url: "/api/updateGrades/" + info.judgeid + "/" + info.studentid + "/" + info.termid + "/" + info.id,
            dataType: "json",
            data: '{"value":' + info.value + ',"graded":' + 1 + ',"comment":' + '"' + info.comment + '"}'
        });
        loadRequest.done(function(dataFromServer) {
            Grades = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
        });
        }
    }
};

///api/updateGrades/:judgeid/:studentid/:termid/:questionid