var Promise = require('bluebird')
var rollbar = require('rollbar')
var sails = require('sails')

rollbar.init(process.env.ROLLBAR_SERVER_TOKEN)

var Solicitor = function (community, startTime, endTime, debug) {
  this.community = community
  this.communityName = community.get('name')
}

var mailtoLink = function (communityId, userId, type) {
  var address = Email.postCreationAddress(communityId, userId, Post.Type.OFFER)
  var subject
  var body = 'More details here...'

  switch (type) {
    case Post.Type.OFFER:
      subject = 'I\'d like to share [finish this sentence!]'
      break
    case Post.Type.REQUEST:
      subject = 'I\'m looking for [finish this sentence!]'
      break
    case Post.Type.INTENTION:
      subject = 'I\'d like to create [finish this sentence!]'
      break
  }
  return 'mailto:' + address + '?subject=' + subject + '&amp;body=' + body
}

var unsubscribeLink = function (user, community) {
  return 'http://google.com'
}

var emailData = function (user, community) {
  return {
    email: user.get('email'),
    sender: {
      name: format('%s (via Hylo)', community.get('name')),
      reply_to: 'hello@hylo.com'
    },
    data: {
      community_name: community.get('name'),
      user_name: user.get('name'),
      community_url: Frontend.Route.community(community) + '?ctt=solicitation_email',
      community_avatar_url: community.get('avatar_url'),
      unsubscribe_link: unsubscribeLink(user, community),
      mailto_offer: mailtoLink(community.id, user.id, Post.Type.OFFER),
      mailto_request: mailtoLink(community.id, user.id, Post.Type.REQUEST),
      mailto_intention: mailtoLink(community.id, user.id, Post.Type.INTENTION)
    }
  }
}

var queueEmail = function (emailData) {
  return Queue.classMethod('Email', 'sendPostSolicitation', emailData)
}

Solicitor.sendToUser = function (user, community) {
  return queueEmail(emailData(user, community))
}

Solicitor.sendToCommunity = function (communityId) {
  return Community.find(communityId, {withRelated: ['users']})
  .then(community => {
    sails.log.debug('Solicitor - sending emails to ', community.get('name'))
    return Promise.map(community.relations.users.models, user =>
      Solicitor.sendToUser(user, community))
  })
}

Solicitor.sendWeekly = function () {
  return Community.query('whereRaw', "settings->>'email_solicitation' = 'true'")
  .fetchAll()
  .then(communities => Promise.map(communities.models, community =>
      Solicitor.sendToCommunity(community.get('id'))))
}

module.exports = Solicitor

@@ -910,51 +910,134 @@
 user
-, community) %7B%0A  return 'http://google.com'
+) %7B%0A  return Frontend.Route.tokenLogin(user, user.generateTokenSync(), Frontend.Route.userSettings() + '?expand=solicitation')
 %0A%7D%0A%0A
@@ -1515,27 +1515,16 @@
 ink(user
-, community
 ),%0A     
@@ -1916,32 +1916,94 @@
 r, community) %7B%0A
+  if (user.get('settings').email_solicitation_preference) %7B%0A  
   return queueEm
@@ -2034,16 +2034,60 @@
 unity))%0A
+  %7D else %7B%0A    return Promise.resolve()%0A  %7D%0A
 %7D%0A%0ASolic
@@ -2220,16 +2220,16 @@
 ty =%3E %7B%0A
+
     sail
@@ -2270,17 +2270,16 @@
 mails to
- 
 ', commu

add unsubscribe link