<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="System.Net.Mail" %>
<%@ Import Namespace="System.IO" %>
<%
string FROM_ADDRESS = "youremailaddress@yourdomain.org";
string TO_ADDRESS = "youremailaddress@yourdomain.org";
string SUBJECT = "New Stewardship Pledge";
string BODY = "Attached is the pledge information.";
string TOOK_A_STEP = "Took A Step";

string SUCCESS_REDIRECT = "2016success.html";
string ERROR_REDIRECT = "2016error.html";

Dictionary<string, string> CSV_HEADER_TO_FORM_FIELD = new Dictionary<string, string>();
CSV_HEADER_TO_FORM_FIELD.Add("id", "famIDEnv");
CSV_HEADER_TO_FORM_FIELD.Add("recurract", "recurract");
CSV_HEADER_TO_FORM_FIELD.Add("terms", "terms");
CSV_HEADER_TO_FORM_FIELD.Add("begin", "startDate");
CSV_HEADER_TO_FORM_FIELD.Add("end", "endDate");
CSV_HEADER_TO_FORM_FIELD.Add("rate", "rate");
CSV_HEADER_TO_FORM_FIELD.Add("total", "pledgeTotal");
CSV_HEADER_TO_FORM_FIELD.Add("Name", "famName");
CSV_HEADER_TO_FORM_FIELD.Add("Address", "famAddress");
CSV_HEADER_TO_FORM_FIELD.Add("City", "famCity");
CSV_HEADER_TO_FORM_FIELD.Add("State", "famState");
CSV_HEADER_TO_FORM_FIELD.Add("Zip", "famZip");
CSV_HEADER_TO_FORM_FIELD.Add("Phone", "famPhone");
CSV_HEADER_TO_FORM_FIELD.Add("Email", "famEmail");
CSV_HEADER_TO_FORM_FIELD.Add("Took A Step", "tookAStep");

Dictionary<string, int> TERM_TO_NUMBER = new Dictionary<string, int>();
TERM_TO_NUMBER.Add("weekly", 52);
TERM_TO_NUMBER.Add("biweekly", 27);
TERM_TO_NUMBER.Add("monthly", 12);
TERM_TO_NUMBER.Add("bimonthly", 6);
TERM_TO_NUMBER.Add("semimonthly", 24);
TERM_TO_NUMBER.Add("quarterly", 4);
TERM_TO_NUMBER.Add("semiannually", 2);
TERM_TO_NUMBER.Add("annually", 1);
TERM_TO_NUMBER.Add("special", 1);

MailMessage message = new MailMessage();
message.From = new MailAddress(FROM_ADDRESS);
message.To.Add(new MailAddress(TO_ADDRESS));
message.Subject = SUBJECT;
message.Body = BODY;

string csvHeaders = "";
string csvUserData = "";

try {
  foreach (KeyValuePair<string, string> kvp in CSV_HEADER_TO_FORM_FIELD) {
    string formValue = Request[kvp.Value];

    if (String.Equals(kvp.Key, "Took A Step")) {
      formValue = String.Equals(formValue, "on") ? "TRUE" : "FALSE";
    }

    if (String.Equals(kvp.Key, "total")) {
      string pledgeRate = Request[CSV_HEADER_TO_FORM_FIELD["rate"]];
      string pledgeTerms = Request[CSV_HEADER_TO_FORM_FIELD["terms"]];
      float parsedPledgeRate = 0;
      int parsedPledgeTerms = 0;

      try {
        parsedPledgeRate = Single.Parse(pledgeRate);
        parsedPledgeTerms = TERM_TO_NUMBER[pledgeTerms];
      } catch (Exception e) {}

      formValue = (parsedPledgeRate * parsedPledgeTerms).ToString("0.00");
    }

    csvHeaders += "\"" + kvp.Key + "\",";
    csvUserData += "\"" + formValue + "\",";
  }

  Stream csvContentStream = new MemoryStream(
    Encoding.UTF8.GetBytes(csvHeaders + '\n' + csvUserData));
  Attachment csvAttachment = new Attachment(csvContentStream, "submission.csv", "text/csv");
  message.Attachments.Add(csvAttachment);
  SmtpClient client = new SmtpClient("localhost");
  client.Send(message);

  Response.Redirect(SUCCESS_REDIRECT, false);
} catch (Exception ex) {
  Response.Redirect(ERROR_REDIRECT, false);
}
%>
