var questions = {};

var notifyQuestionsComponent = function(type) {
  $(QuestionsEntity).trigger(type)
};

var QuestionsEntity = {

    get: function() {
    return questions
    },

    loadAll: function() {
      var loadRequest = $.ajax({
          type: 'GET',
          url: "/api/getAll/questions",
          dataType: "json"
      });
      loadRequest.done(function (dataFromServer) {
          questions = dataFromServer;
          notifyQuestionsComponent('getAllQuestions');
      });
    },

    loadCurrentQuestions: function(){
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getCurrent/questions",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            questions = dataFromServer;
            notifyQuestionsComponent('getCurrentQuestions');
        });
    },

    loadQM: function() {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getQM",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            questions = dataFromServer;
            notifyQuestionsComponent('getQM');
        });
    },
    saveQM: function (options) {
        var creationRequest = $.ajax({
            type: 'POST',
            url: "/api/saveQM",
            data: JSON.stringify(options)
        });
        creationRequest.done(function () {
            notifyQuestionsComponent("saveQMs");
        });
    },
    updateQM: function (options) {
        var creationRequest = $.ajax({
            type: 'POST',
            url: "/api/updateQM",
            data: JSON.stringify(options)
        });
        creationRequest.done(function () {
            notifyQuestionsComponent("updateQMs");
        });
    },
    deleteQM: function (options) {
        var loadRequest = $.ajax({
            type: 'POST',
            url: "/api/deleteQM",
            data: JSON.stringify(options)
        });
        loadRequest.done(function(dataFromServer) {
            questions = dataFromServer;
            notifyQuestionsComponent("deleteQMs");
        });
    }
}
