/**
 * Created by DennysO on 6/27/2015.
 */
var term = {};

var notifyTermComponents = function(type) {
    switch(type)
    {
        case "loadTerms":
            $(TermEntity).trigger('TermsHasChanged');
            break;
        case "setNewActive":
            $(TermEntity).trigger('ActiveTermsHasChanged');
            break;
        case "createNewTerm":
            $(TermEntity).trigger('CreatedNewTerm');
            break;
        case "UpdateTerm":
            $(TermEntity).trigger('UpdateTerm');
            break;
        case "deleteTerm":
            $(TermEntity).trigger('deleteTerm');
            break;
        default:
            $(TermEntity).trigger('TermsHasChanged');
            break;
    }

};


var TermEntity = {

    doneLoading: false,

    getTerm: function() {
        return term;
    },

    loadActiveTerm: function() {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getActiveTerm",
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            term = dataFromServer;
            notifyTermComponents();
        });
    },

    loadTerms: function() {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAll/terms",
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            ////console.log('LOADED Terms: ', dataFromServer);
            term = dataFromServer;
            notifyTermComponents("loadTerms");
        });
    },

    setNewActive: function (options) {
        x = options;
        //console.log(options);
        var creationRequest = $.ajax({
            type: 'PUT',
            url: "/api/setNewActive",
            data: JSON.stringify(options)
        });
        creationRequest.done(function() {
            notifyTermComponents("setNewActive");
        });
    },
    createNewTerm: function (options) {
        x = options;
        ////console.log(options);
        var creationRequest = $.ajax({
            type: 'POST',
            url: "/api/save/terms",
            data: JSON.stringify(options),
            dataType: "json"
        });
        creationRequest.done(function (dataFromServer) {
            term = dataFromServer;
            notifyTermComponents("createNewTerm");
        });
    },
    updateTerm: function (options) {
        x = options;
        ////console.log(options);
        var creationRequest = $.ajax({
            type: 'PUT',
            url: "/api/update/terms/" + options.id,
            data: JSON.stringify(options)
        });
        creationRequest.done(function (dataFromServer) {
            term = dataFromServer;
            notifyTermComponents("UpdateTerm");
        });
    },
    deleteTerm: function (id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/delete/terms/" + id,
            dataType: "json"
        });
        loadRequest.done(function(dataFromServer) {
            term = dataFromServer;
            notifyTermComponents("deleteTerm");
        });
    }
};