package com.sixsq.slipstream.action.usage;

import java.util.List;
import java.util.Map;

/**
 * Represents the content of a daily mail sent to one user for a specific day.
 */
public class MailUsage {

    private String date;
    private String email;
    private String userName;
    private List<UsageSummary> usageSummaries;

    public MailUsage(String date, String userName, String email, List<UsageSummary> usageSummaries){
        this.date = date;
        this.email = email;
        this.userName = userName;
        this.usageSummaries = usageSummaries;
    }

    public String to(){
        return email;
    }

    public String body(){
        return intro() + usageCloud() + footer();
    }

    private String intro() {
        return "Hello " + userName + ".\n" +
                "Here is a summary of your cloud usage for the day : " +
                date + ".\n" ;
        // TODO : real text
    }

    private String usageCloud(){

        if(usageSummaries == null || usageSummaries.isEmpty()) {
            return "Your Cloud usage is empty."; // TODO real text
        }

        StringBuilder sb = new StringBuilder();
        for(UsageSummary usageSummary : usageSummaries) {
            sb.append("On Cloud '" + usageSummary.cloud + "'\n");
            // TODO sort by cloud name
            // TODO real text
            for(Map.Entry<String, Double> metric : usageSummary.getMetrics().entrySet()){
                sb.append(metric.getKey()).
                   append(" : ").
                   append(metric.getValue()).
                   append("\n");
            }
        }
        return sb.toString();
    }

    private String footer(){
        // TODO real text
        String unsubcribeLink = "http://localhost:8080/user/"+ userName + "#general";
        return
                "You receive this email because you have enabled" +
                "'Receive a mail with cloud usages' in your profile.\n" +
                "To unsubscribe : " + unsubcribeLink;
    }
}

@@ -664,41 +664,186 @@
 urn 
-intro() + usageCloud() + footer()
+%22%5Cn%22 + intro() + lineSep() + usageCloud() + lineSep() + footer() + lineSep();%0A    %7D%0A%0A    // TODO temp%0A    private String lineSep() %7B%0A        return %22--------------------------%5Cn%22
 ;%0A  
@@ -1833,32 +1833,58 @@
  TODO real text%0A
+        // TODO real link%0A
         String u

improve mail text display for debug.