

angular.module('mandrill', [])
.service('Mandrill', [ '$rootScope', '$http', function($rootScope, $http) {

  "use strict";

  this.sendMail = function(msgData) {

    var messagePost = {
      key: "m14Wkh0PhF74K2cTvxWPHA",
      message:  {
        subject: msgData.name + " enviou uma mensagem",
        html: msgData.message,
        text: msgData.message,
        from_email: msgData.email,
        from_name: msgData.name,
        to: [
          {
            email: "james.peret@gmail.com",
            name: "James Peret",
            type: 'to'
          }
        ]
      }
    };
    console.log(messagePost);
    $http.post('https://mandrillapp.com/api/1.0/messages/send.json', { key: messagePost.key, message: messagePost.message })
    .success(function(data, status, headers, config){
      $rootScope.$broadcast('email_sent:success');
      console.log("> Message sent succesfully!");
      console.log(data);
    }).
    error(function(data, status, headers, config) {
      $rootScope.$broadcast('email_sent:error');
      console.log("> There was an error whule sending the Message.");
      console.log(data);
    });
  };



}]);
