/**
 * Created by MCardero on 6/26/2015.
 */
var emailTemplate = {};
var emailTemplates = [];
var emailTemplatesInfo = [];
var emailTemplatesSubInfo = [];
var resetData = {};

var notifyTComponents = function () {
    $(EmailTemplateEntity).trigger('EmailTemplatesHasChanged')
};

var notifyTemplateComponents = function (type) {
    $(EmailTemplateEntity).trigger(type)
};

var findEmailTemplateById = function (id) {
    return emailTemplates.filter(function (_emailTemplates) {
        return _emailTemplates.id === id
    })[0]
}

var EmailTemplateEntity = {

    getEmailTemplate: function () {
        //console.log("GET EMAIL TEMPLATE");
        //console.log(emailTemplate);
        return emailTemplate
    },

    getEmailTemplates: function () {
        //console.log("GETMETHOD: ", emailTemplates);
        return emailTemplates
    },

    getEmailTemplatesInfo: function () {
        return emailTemplatesInfo
    },
    getEmailTemplatesSubInfo: function () {
        return emailTemplatesSubInfo
    },

    getEmailResetData: function () {
        return resetData;
    },

    loadEmailTemplate: function (emailTemplateId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/get/emailtemplates/" + emailTemplateId,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            //console.log("RESPONSE");
            //console.log(dataFromServer);
            emailTemplate = dataFromServer;
            notifyTComponents();
        });
    },

    loadEmailTemplates: function () {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAll/emailtemplates",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            emailTemplates = dataFromServer;
            notifyTComponents();
        });
    },

    loadDeleteTemplates: function () {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getAll/emailtemplates",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            emailTemplates = dataFromServer;
            //console.log("Finished Deleting");
            notifyTemplateComponents('FinishedDeleting');
        });
    },

    loadEmailTemplatesInfo: function () {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getEmailTemplatesInfo",
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            emailTemplatesInfo = dataFromServer;
            notifyTComponents();
        });
    },

    addEmailTemplate: function (options) {
        x = options;
        //console.log(options);
        var loadRequest = $.ajax({
            type: 'POST',
            url: "/api/save/emailtemplates",
            data: JSON.stringify(options)
        });
        loadRequest.done(function (dataFromServer) {
            emailTemplate = dataFromServer;
            notifyTComponents();
        });
    },

    updateEmailTemplate: function (options, emailTemplateId) {
        x = options;
        //console.log(options, "UPDATE METHOD");
        var creationRequest = $.ajax({
            type: 'PUT',
            url: "/api/update/emailtemplates/" + emailTemplateId,
            data: JSON.stringify(options)
        })
    },

    deleteEmailTemplate: function (id) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/deleteTemplate/" + id,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            //console.log('it triggers delete');
            notifyTemplateComponents("TemplatesHasDeleted");
        });
    },

    sendEmail: function (id, templateId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/sendEmail/" + id + "/" + templateId,
            dataType: "json"
        });
        loadRequest.done(function (dataFromServer) {
            emailTemplatesInfo.push(dataFromServer);
            notifyTComponents();
        });
    },

    sendEmailNoId: function (email, templateId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/sendEmailNoId/" + email + "/" + templateId,
            dataType: "json"
        })
        loadRequest.done(function (dataFromServer) {
            emailTemplatesInfo.push(dataFromServer);
            resetData = dataFromServer;
            notifyTComponents();
        });
    },

    getPreview: function (id, templateId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getPreview/" + id + "/" + templateId,
            dataType: "html"
        })
        loadRequest.done(function (dataFromServer) {
            emailTemplatesInfo = dataFromServer;
            notifyTComponents();
        });
    },

    getPreviewSub: function (id, templateId) {
        var loadRequest = $.ajax({
            type: 'GET',
            url: "/api/getPreviewSub/" + id + "/" + templateId,
            dataType: "html"
        })
        loadRequest.done(function (dataFromServer) {
            emailTemplatesSubInfo = dataFromServer;
            notifyTemplateComponents('EmailSubjectHasChanged');
        });
    },

    toggleCompleteness: function (itemId) {
    }
}