package com.aquarellian.genar.data.converter;

import com.aquarellian.genar.data.entity.Issue;
import com.aquarellian.genar.data.entity.Severity;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;


/**
 * Project: genar
 * Author:  Tatiana Didik
 * Created: 03.09.2015 19:39
 * <p/>
 * $Id$
 */
public class BasicIssueFormatter implements IssueFormatter {
    private Issue issue;
    private String sonarUrl;

    public BasicIssueFormatter(Issue issue, String sonarUrl) {
        this.issue = issue;
        this.sonarUrl = sonarUrl;
    }

    @Override
    public String getMessage() {
        StringBuilder sb = new StringBuilder();
        sb.append(formatMessage());
        sb.append(formatSeverity());
        sb.append(formatRule());
        return sb.toString();
    }

    protected String formatMessage() {
        StringBuilder sb = new StringBuilder();
        String message = issue.getMessage();
        if (message != null) {
            sb.append(message);
            if (!message.endsWith(".")) {
                sb.append(".");
            }
            sb.append("\n");
        }
        return sb.toString();
    }

    protected String formatSeverity() {
        StringBuilder sb = new StringBuilder();
        Severity severity = issue.getSeverity();
        if (severity != null) {
            sb.append("Severity: ").append(severity.name());
            sb.append("\n");
        }
        return sb.toString();
    }

    protected String formatRule() {
        StringBuilder sb = new StringBuilder();
        String rule = issue.getRule();
        if (rule != null) {
            sb.append("Read more: ").append(getRuleLink(rule));
            sb.append("\n");
        }
        return sb.toString();
    }

    protected String getRuleLink(String rule){
        if (sonarUrl != null){
            StringBuilder sb = new StringBuilder();
            String url = sonarUrl.trim();
            if (!(url.startsWith("http://") || sonarUrl.startsWith("https://"))){
                sb.append("http://");
            }
            sb.append(url);
            if (!(url.endsWith("/"))){
                sb.append("/");
            }
            sb.append("coding_rules#q=");
            sb.append(escapeHttp(rule));      // squid%3AS1319
            return sb.toString();
        }
        return rule;
    }


    protected String escapeHttp(String query) {
//        return StringEscapeUtils.escapeHtml(query);     // todo this method does not escape semicolon. but is URLEncoder.encode a correct way to do so?
        try {
            return URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return query;
        }
    }
}

@@ -679,23 +679,24 @@
 d(format
-Message
+Severity
 ());%0A   
@@ -712,32 +712,31 @@
 ppend(format
-Severity
+Message
 ());%0A       
@@ -1098,32 +1098,36 @@
    sb.append(%22%5Cn
+%5Cn%5Cn
 %22);%0A        %7D%0A  
@@ -1356,20 +1356,23 @@
 end(
-%22S
+s
 everity
-: %22
+.name()
 ).ap
@@ -1376,31 +1376,35 @@
 .append(
-severity.name()
+%22 Sonar violation:%22
 );%0A     
@@ -1415,32 +1415,36 @@
    sb.append(%22%5Cn
+%5Cn%5Cn
 %22);%0A        %7D%0A  

Reformat comment #2