
/**
 * Module dependencies.
 */

var postmark = require('postmark')
  , fs = require('fs');

/**
 * Initialize notifier with config
 *
 * @param {Object} config
 * @return {Notifier}
 * @api public
 */

function Notifier (config) {
  var self = this;

  if (!config) {
    throw new Error('Please provide a config file.');
    return;
  }

  if (config && !config.templates) {
    throw new Error('Please provide the path to templates.');
  }

  var actions = config.actions;
  if (actions) {
    actions.forEach(function (action) {
      self[action] = function (options) {
        if (config.APN) {
          // send apn
          self.APN(options);
        }

        if (config.mails) {
          // send mails
          self.mail(options)
        }

        if (config.facebook) {
          // send to facebook
          self.postToFacebook(options);
        }
      };
    });
  }
}

module.exports = Notifier;

Notifier.prototype = {

  /**
   * Mailer
   *
   * @param {Object} options
   * @api public
   */

  mail: function (options) {
    var submit = function(to) {
      var subject = mail.subject;

      var pm = {
        'From': options.from,
        'To': options.to,
        'Subject': options.subject,
        'HtmlBody': processTemplate(options.tplPath, options.locals)
      };

      // send emails only if emailing is enabled
      if (config.email) {
        postmark.send(pm);
      } else {
        if (env !== 'test') console.log(pm);
      }
    }

    if(/\@/.test(mail.to)) {
      return submit(mail.to);
    }
  },

  /**
   * Process the template
   *
   * @param {String} path
   * @param {Object} locals
   * @api public
   */

  processTemplate: function (tplPath, locals) {
    var Jade = require('jade')

    tplPath = tplPath + '.jade';

    var tpl = require('fs').readFileSync(tplPath, 'utf8');
    var html = Jade.compile(tpl, { filename: tplPath });

    return html(locals);
  },

  /**
   * Sends Apple Push Notification
   *
   * @param {Object} obj
   * @api public
   */

  APN: function (obj) {
    // send apn
  },

  /**
   * Post to facebook
   *
   * @param {Object} obj
   * @api public
   */

  postToFacebook: function (obj) {
    // post to facebook
  }

}


/**
 * Module dependencies.
 */

var postmark = require('postmark')
  , fs = require('fs')
  , path = require('path');


/**
 * Initialize notifier
 *
 * @api public
 */

var Notifier = exports = module.exports = function (argument) {}

/**
 * Notifier methods
 */

Notifier.prototype = {

  /**
   * Notifier config
   *
   * @param {Object} config
   * @api public
   */

  use: function (config) {
    var self = this;
    this.config = config;

    if (!config) {
      throw new Error('Please provide a config.');
    }

    if (config.email && !config.tplPath && !fs.exists(config.tplPath)) {
      throw new Error('Please provide correct path to the templates.');
    }
  },

  /**
   * Send the specified notification
   *
   * @param {String} action
   * @param {Object} notification
   * @api public
   */

  send: function (action, notification) {
    var config = this.config;

    if (this.config.actions.indexOf(action) === -1) {
      throw new Error('The action \'' + action + '\' is not specified');
    }

    // check if the object specified has all the required fields
    if (config.email) {
      var filePath = config.tplPath + '/' + action + '.jade';
      var exists = fs.existsSync(filePath);
      if (!exists) {
        throw new Error('Please specify a path to the template');
      }
    }

    // send apn
    if (config.APN) {
      this.APN(notification);
    }

    // send mails
    if (config.email) {
      this.mail(notification, filePath);
    }

    // send to facebook
    if (config.facebook) {
      this.postToFacebook(notification);
    }
  },

  /**
   * Process the template
   *
   * Note that this method can be overridden so that you can use your preffered
   * templating language. Default is jade
   *
   * @param {String} tplPath
   * @param {Object} locals
   * @return {String}
   * @api public
   */

  processTemplate: function (tplPath, locals) {
    var Jade = require('jade');

    var tpl = require('fs').readFileSync(tplPath, 'utf8');
    var html = Jade.compile(tpl, { filename: tplPath });

    return html(locals);
  },

  /**
   * Send email via postmark
   *
   * @param {Object} obj
   * @api public
   */

  mail: function (obj, filePath) {
    if(!/\@/.test(obj.to) || !/\@/.test(obj.from)) {
      throw new Error('Please specify proper to and from address');
    }

    var pm = {
      'From': obj.from,
      'To': obj.to,
      'Subject': obj.subject,
      'HtmlBody': this.processTemplate(filePath, obj.locals)
    };
    console.log(pm);
    // postmark.send(pm);
  },

  /**
   * Send Apple Push Notification
   *
   * @param {Object} obj
   * @api public
   */

  APN: function (obj) {
    // send apn
  },

  /**
   * Post to facebook
   *
   * @param {Object} obj
   * @api public
   */

  postToFacebook: function (obj) {
    // post to facebook
  }

}

add notifier methods