'use strict';

/* globals angular */

/**
 * @ngdoc service
 * @name brandDashboardApp.user
 * @description
 * # user
 * Service in the brandDashboardApp.
 */
angular.module('EnterpriseDashboardApp.ErrorMailer', ['EnterpriseDashboardApp.HostFactory'])
    .service('ErrorMailer', function user($window, $location, $q, $http, $log, HostFactory) {
        var Service;

        Service = {
            send: function(data) {
                var emailContent;

                emailContent = Service._enrichDataWithInformation(data);
                return Service._sendEmail(emailContent);

            },
            _enrichDataWithInformation: function(data) {
                var emailContent = {};

                emailContent.data = data;
                emailContent.time = new Date();
                emailContent.userAgent = $window.navigator.userAgent;
                emailContent.path = $location.path();
                emailContent.search = $location.search();

                return emailContent;
            },
            _sendEmail: function(content) {
                var def;
                var from, to, subject, text, data;

                def = $q.defer();
                from = 'wonderboard@wonderflow.co';
                to = 'giovanni@wonderflow.co';
                subject = 'Error in Wonderboard';
                text = JSON.stringify(content, null, 4);

                data = {
                    from: from,
                    to: to,
                    subject: subject,
                    text: text
                };
                $log.info('Sending email ', data);

                $http
                    .post('http://wonderflow.co/site/mail', data)
                    .success(function(result) {
                        def.resolve(result);
                    }).error(function(err) {
                        def.reject(err);
                    });
                return def.promise;
            }

        };

        return Service;

    });