var judge = {};
var judges = [];
var judgesInfo = [];
var studentsInfo = [];
var student = {};
var students = [];
var adminHome = [];
var StudentInfo = [];
var user = {};

// var notifyJComponents = function () {
//     $(UserEntity).trigger('JudgesHasChanged')
//     $(UserEntity).trigger('StudentsHasChanged')
// }

var notifyJUComponents = function () {
    $(UserEntity).trigger('JudgesHasChanged');
};

var notifySUComponents = function () {
    $(UserEntity).trigger('StudentsHasChanged');
}

var notifyUsersComponents = function(type)
{
    $(UserEntity).trigger(type);
};

var findJudgeById = function (id) {
    return judges.filter(function (_judges) {
        return _judges.id === id
    })[0]
}

var UserEntity = {

    getJudge: function () {
        ////console.log("GET JUDGE");
        ////console.log(judge);
        return judge
    },

    getJudges: function () {
        return judges
    },

    getStudents: function () {
        return students
    },

    getFullStudentsInfo: function () {
        return studentsInfo
    },

    getJudgesInfo: function () {
        return judgesInfo
    },

    getFullJudgesInfo: function () {
        return judgesInfo
    },

    getAdminHome: function () {
        return adminHome
    },

    get: function () {
        return users
    },

    loadJudge: function (judgeId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/get/users/" + judgeId,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            judge = dataFromServer;
            notifyJUComponents();
        });
    },

    loadStudentsPerJudge: function(judgeId){
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getStudentsJudge/" + judgeId,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            students = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifySUComponents();            
            }

        });
    },

    loadJudgeInvite: function (judgeId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/invite/users/" + judgeId,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            judge = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyJUComponents();
            }
        });
    },

    loadJudges: function (type) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAll/judges/" + type,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            judges = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents(type);
            }
        });
    },

    loadStudents: function (type) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAll/students/" + type,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            students = dataFromServer;
            notifyUsersComponents(type);
        });
    },

    loadStudentHome: function (id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getStudentHome/" + id,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            ////console.log("type: ", type, " LOADED: ", dataFromServer);
            users = dataFromServer;
            notifyUsersComponents('StudentHome');
        });
    },

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

    loadStudentsInfo: function () {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getStudentsInfo",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            students = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('StudentsHasChanged');
            }
        });
    },

    loadGradesFilter: function (id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getGradesFilter/" + id,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            students = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('FilterHasChanged');
            }
        });
    },

    loadFullJudgesInfo: function (termid, statename) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getFullJudgesInfo/" + termid + "/" + statename,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            judgesInfo.push.apply(judgesInfo, dataFromServer);
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('StudentsData');
            }
        });
    },

    loadFullStudentsInfo: function (termid, statename) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getFullStudentsInfo/" + termid + "/" + statename,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            studentsInfo.push.apply(studentsInfo, dataFromServer);
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('StudentsData');
            }
        });
    },

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

    loadAllUsers: function () {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAllUsers",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            users = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('GetAllUsers');
            }
        });
    },

    addJudge: function (options) {
        x = options;
        ////console.log(options);
        var creationRequest = $.ajax({
            type: 'POST',
            url: "/api/save/users",
            data: JSON.stringify(options)
        })
        creationRequest.done(function (dataFromServer) {
            users = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
        });
    },

    saveUser: function (options) {
        var loadRequest = $.ajax({
            type: 'POST',
            url: "/api/saveUser",
            data: JSON.stringify(options),
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            users = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('SaveUser');
            }
        });
    },
    deleteUser: function (options) {
        var loadRequest = $.ajax({
            type: 'POST',
            url: "/api/deleteUser",
            data: JSON.stringify(options)
        });
        loadRequest.done(function (dataFromServer) {
            users = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
            else
            {
                notifyUsersComponents('DeleteUser');
            }
        });
    },

    updateJudge: function (options, judgeId) {
        x = options;
        ////console.log(options, "UPDATE METHOD");
        var creationRequest = $.ajax({
            type: 'PUT',
            url: "/api/update/users/" + judgeId,
            data: JSON.stringify(options)
        })
        creationRequest.done(function (dataFromServer) {
            users = dataFromServer;
            if(dataFromServer['response'] == 'qwe122e223ergrew323')
            {
                window.location.href = "/sessionExpired";
            }
        });
    },

    loginUser: function (user, pass) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/authenticate/" + user + "/" + pass,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            user = dataFromServer;
            notifyJComponents();
        });
    },

    toggleCompleteness: function (itemId) {
    }
}
