###

Mails functions for the App. This is where the mail template is built and then
send out with functions provided by the library.

###

uuid = require 'node-uuid'
path = require 'path'
fs = require 'fs'
jade = require 'jade'

Email = require('sendgrid').Email
SendGrid = require('sendgrid').SendGrid

###
Create a Client we will use to send the Mail.

Gives us a single location where all clients are created.
###
exports.get_client = (username, password_or_key) ->
	return new SendGrid username, password_or_key

###
Send the E-Mail with the given filename.
###
exports.send = (template, to, bcc, from, subject, files, options, fn) ->

	options.to = to
	options.bcc = bcc
	options.from = from
	options.subject = subject
	options.attachments = files

	options.current_year = new Date().getFullYear()

	mailer.get_template template, options, (content) ->

		if content

			email = new Email {
				to: to,
				from: from,
				subject: subject,
				html: content
			}

			for file in files
				if file.indexOf('http://') > 0 or file.indexOf('https://') > 0

					email.addFile {
						url: file
					}

				else

					email.addFile {
						path: file
					}

			mailer.client.send email, (success, message) ->
				if success
					fn true
				else
					fn false

		else
			fn false

###
Send the Minimum Options.

Like no CC or Attachments
###
exports.send_short = (template, to, from, subject, options, fn) ->
	mailer.send template, to, [], from, subject, [], options, fn

