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";

            // 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;
    }
}
@@ -2353,32 +2353,317 @@
     %3C/div%3E%5Cn%22;%0A%0A
+            output +=%0A                 %22    %3Cdiv class=%5C%22span10%5C%22%3E%5Cn%22 +%0A                    %22       %3Ch3%3EHTML body formatted%3C/h3%3E%5Cn%22 +%0A                    %22       %3Cp class=%5C%22well%5C%22 name=%5C%22bodyHTML_Formatted%5C%22%3E%22 + mockMail.getBodyHtml() + %22%3C/p%3E%5Cn%22 +%0A                    %22    %3C/div%3E%5Cn%22;%0A%0A
             // a

Add html formatted output