package com.mockmock.htmlbuilder;

import com.mockmock.mail.MockMail;
import org.apache.commons.lang.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MailViewHtmlBuilder implements HtmlBuilder
{
    private MailViewHeadersHtmlBuilder headersBuilder;
    private AddressesHtmlBuilder addressesHtmlBuilder;

    private MockMail mockMail;

    public void setMockMail(MockMail mockMail)
    {
        this.mockMail = mockMail;
    }

    public String build()
    {
        headersBuilder.setMockMail(mockMail);

        addressesHtmlBuilder.setMockMail(mockMail);

        String subjectOutput;
        if(mockMail.getSubject() == null)
        {
            subjectOutput = "<em>No subject given</em>";
        }
        else
        {
            subjectOutput = StringEscapeUtils.escapeHtml(mockMail.getSubject());
        }

		subjectOutput += " <small class=\"deleteLink\"><a href=\"/delete/" + mockMail.getId() + "\">Delete</a></small>";

        String output = "<div class=\"container\">\n";

        output +=
                "<h2>" + subjectOutput + "</h2>\n" +
                "  <div class=\"row\">\n";

        output +=
                "    <div class=\"span10\">\n" +
                "       <h3>Addresses</h3>\n" +
                "       " + addressesHtmlBuilder.build() +
                "    </div>\n";

        output +=
                "    <div class=\"span10\">\n" +
                "       <h3>Mail headers</h3>\n" +
                "       " + headersBuilder.build() +
                "    </div>\n";

        if(mockMail.getBody() != null)
        {
            output +=
                    "    <div class=\"span10\">\n" +
                    "       <h3>Plain text body</h3>\n" +
                    "       <p class=\"well\" name=\"bodyPlainText\">" + StringEscapeUtils.escapeHtml(mockMail.getBody()) + "</p>\n" +
                    "    </div>\n";
        }

        if(mockMail.getBodyHtml() != null)
        {
            output +=
                    "    <div class=\"span10\">\n" +
                    "       <h3>HTML body unformatted</h3>\n" +
                    "       <p class=\"well\" name=\"bodyHTML_Unformatted\">" + StringEscapeUtils.escapeHtml(mockMail.getBodyHtml()) + "</p>\n" +
                    "    </div>\n";

            output +=
                 "    <div class=\"span10\">\n" +
                    "       <h3>HTML body formatted</h3>\n" +
                    "       <p class=\"well\" name=\"bodyHTML_Formatted\">" + mockMail.getBodyHtml() + "</p>\n" +
                    "    </div>\n";

            // also show a parsed version via an iframe
            output +=
                    "    <div class=\"span10\">\n" +
                    "        <iframe src=\"/view/html/" + mockMail.getId() + "\" style=\"width: 780px; height: 700px; overflow: scroll;\" style=\"\" name=\"bodyHTML_iFrame\">\n" +
                    "        </iframe>\n" +
                    "    </div>";
        }

		// just output the raw mail so we're sure everything is on the screen
		if(mockMail.getRawMail() != null)
		{
			// output complete raw mail
			output +=
					"    <div class=\"span10\">\n" +
							"       <h3>Complete raw mail output</h3>\n" +
							"       <p class=\"well\">" + StringEscapeUtils.escapeHtml(mockMail.getRawMail()) + "</p>\n" +
							"    </div>\n";
		}

        output +=
                "  </div>\n";

        output +=
                "</div>\n";

        return output;
    }

    @Autowired
    public void setMailViewHeadersHtmlBuilder(MailViewHeadersHtmlBuilder mailViewHeadersHtmlBuilder) {
        this.headersBuilder = mailViewHeadersHtmlBuilder;
    }

    @Autowired
    public void setAddressesHtmlBuilder(AddressesHtmlBuilder addressesHtmlBuilder) {
        this.addressesHtmlBuilder = addressesHtmlBuilder;
    }
}
@@ -1263,32 +1263,51 @@
 class=%5C%22span10%5C%22
+ name=%5C%22addresses%5C%22
 %3E%5Cn%22 +%0A         
@@ -1489,32 +1489,49 @@
 class=%5C%22span10%5C%22
+ name=%5C%22headers%5C%22
 %3E%5Cn%22 +%0A         
@@ -1767,32 +1767,55 @@
 class=%5C%22span10%5C%22
+ name=%5C%22bodyPlainText%5C%22
 %3E%5Cn%22 +%0A         
@@ -1884,33 +1884,35 @@
        %22       %3C
-p
+div
  class=%5C%22well%5C%22 
@@ -1910,39 +1910,16 @@
 %5C%22well%5C%22
- name=%5C%22bodyPlainText%5C%22
 %3E%22 + Str
@@ -1961,33 +1961,35 @@
 getBody()) + %22%3C/
-p
+div
 %3E%5Cn%22 +%0A         
@@ -2139,32 +2139,62 @@
 class=%5C%22span10%5C%22
+ name=%5C%22bodyHTML_Unformatted%5C%22
 %3E%5Cn%22 +%0A         
@@ -2269,33 +2269,35 @@
        %22       %3C
-p
+div
  class=%5C%22well%5C%22 
@@ -2299,38 +2299,8 @@
 ll%5C%22
- name=%5C%22bodyHTML_Unformatted%5C%22
 %3E%22 +
@@ -2354,25 +2354,27 @@
 tml()) + %22%3C/
-p
+div
 %3E%5Cn%22 +%0A     
@@ -2462,32 +2462,60 @@
 class=%5C%22span10%5C%22
+ name=%5C%22bodyHTML_Formatted%5C%22
 %3E%5Cn%22 +%0A         
@@ -2588,33 +2588,35 @@
        %22       %3C
-p
+div
  class=%5C%22well%5C%22 
@@ -2618,36 +2618,8 @@
 ll%5C%22
- name=%5C%22bodyHTML_Formatted%5C%22
 %3E%22 +
@@ -2643,25 +2643,27 @@
 Html() + %22%3C/
-p
+div
 %3E%5Cn%22 +%0A     
@@ -2814,24 +2814,40 @@
 s=%5C%22span10%5C%22
+ name=%5C%22iFrame%5C%22
 %3E%5Cn%22 +%0A     
@@ -3297,16 +3297,35 @@
 span10%5C%22
+ name=%5C%22rawOutput%5C%22
 %3E%5Cn%22 +%0A%09
@@ -3393,17 +3393,19 @@
        %3C
-p
+div
  class=%5C
@@ -3473,17 +3473,19 @@
 )) + %22%3C/
-p
+div
 %3E%5Cn%22 +%0A%09

Change mail view html for clearer xpaths