"use strict";

var DEFAULT_TITLE = "Salesforce Object";
var DOCUMENT_TYPE_ID = "5252ce4ce4cfcd16f55cfa3c";

function sfdcTypeToDocumentType(sfdcType) {
  switch(sfdcType) {
    // case "Contact":
    // case "Lead":
    // case "Opportunity":
    // TODO: special case for contacts
    default:
      return DOCUMENT_TYPE_ID;
  }
}

function filterRecordKeys(record) {
  return function(key) {
    return (
      record[key] &&
      typeof record[key] === "string" &&
        ! /id/i.test(key) &&
        ! /date/i.test(key) &&
        ! /system/i.test(key)
      );
  };
}

function genAttributeTable(record) {
  return "<table>" +
    Object.keys(record)
      .filter(filterRecordKeys(record))
      .map(function mapRecordKeys(key) {
        return "<tr>" +
          "<td><strong>" + key + "</strong></td>" +
          "<td>" + record[key] + "</td>" +
          "</tr>";
      }) +
    "</table>";
}

function genText(record) {
  return Object.keys(record)
    .filter(filterRecordKeys(record))
    .map(function mapRecordKeys(key) {
      return key + ": " + record[key];
    })
    .join("; ");
}

module.exports = function formatDocument(sobject, record) {
  var url = "https://emea.salesforce.com/" + sobject.Id; // TODO: NA addresses support
  var documentType = sfdcTypeToDocumentType(sobject.attributes.type);
  var documentTitle = record.Name || record.Title || DEFAULT_TITLE;
  var res = {
    "identifier": sobject.attributes.type + '/' + sobject.Id,
    "creation_date": record.CreatedDate,
    "modification_date": record.LastModifiedDate,
    "actions": {
      "show": url
    },
    "document_type": documentType,
    "data": {
      "title": documentTitle,
      "path": sobject.attributes.type + "/" + documentTitle,
      "html": genAttributeTable(record),
      "text": genText(record)
    }
  };

  // TODO: Special case for contacts

  return res;
};

@@ -1633,16 +1633,20 @@
 e,%0A    %22
+meta
 data%22: %7B
@@ -1748,40 +1748,30 @@
    %22
-html
+text
 %22: gen
-AttributeTable
+Text
 (record)
 ,%0A  
@@ -1766,39 +1766,69 @@
 (record)
-,
 %0A    
-  %22text%22: genText
+%7D,%0A    %22data%22: %7B%0A      %22html%22: genAttributeTable
 (record)

move stuff to metadata except html