'use strict'

const querystring = require('querystring')

const httpValidation = require('../../lib/http/validation')
const httpEncryption = require('../../lib/http/encryption')
const httpRoute = require('../../lib/http/route')

module.exports.getParams = function (event) {
  return Object.assign({}, querystring.parse(event.body), event.pathParameters, event.queryStringParameters)
}

module.exports.isValid = function (data, callback) {
  return checkHoneyPot(data, callback) && checkToParam(data, callback)
}

module.exports.hasEncryptedToEmail = function (data) {
  return httpValidation.isEmail(httpEncryption.decrypt(data['_to']))
}

function checkHoneyPot (data, callback) {
  if ('_honeypot' in data && data['_honeypot'] !== '') {
    httpRoute.render('receive-honeypot', data, callback)
    return false
  }
  return true
}

function checkToParam (data, callback) {
  if (!('_to' in data)) {
    httpRoute.render('receive-no-email', data, callback)
    return false
  }
  if ('_to' in data && !httpValidation.isEmail(data['_to']) && !module.exports.hasEncryptedToEmail(data)) {
    httpRoute.render('receive-bad-email', data, callback)
    return false
  }
  return true
}

@@ -270,22 +270,26 @@
 nt) %7B%0A  
-return
+let data =
  Object.
@@ -381,16 +381,123 @@
 meters)%0A
+  if (hasEncryptedToEmail(data)) %7B%0A    data%5B'_to'%5D = httpEncryption.decrypt(data%5B'_to'%5D)%0A  %7D%0A  return data%0A
 %7D%0A%0Amodul
@@ -618,31 +618,25 @@
 ack)%0A%7D%0A%0A
-module.exports.
+function 
 hasEncry
@@ -647,27 +647,16 @@
 ToEmail 
-= function 
 (data) %7B
@@ -664,16 +664,33 @@
   return
+ '_to' in data &&
  httpVal
@@ -1147,53 +1147,8 @@
 o'%5D)
- && !module.exports.hasEncryptedToEmail(data)
 ) %7B%0A

move hasEncryptedEmail check into request.js