
package de.ivu.elect.presentation.system;

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

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import de.ivu.elect.business.CurrentCustomer;
import de.ivu.elect.business.security.control.ElectCommonPrincipal;

/**
 * https://confluence.atlassian.com/display/JIRA/Creating+Issues+via+direct+HTML+links
 */
@Named(JiraUrl.COMPONENT_NAME)
public class JiraUrl {

  public static final String COMPONENT_NAME = "jiraUrl";

  private static final String JIRA_URL = "https://support.elect-it.com/ticket";

  private static final String CREATE_ISSUE = "/secure/CreateIssueDetails!init.jspa?";

  private static final String PID = "pid=";

  private static final String ISSUE_TYPE_ERROR = "&issuetype=1";

  private static final String PRIORITY = "&priority=3";

  private static final String ENVIRONMENT = "&environment=";

  private static final String REPORTER = "&customfield_10210="; // gemeldet von

  private static final String DESCRIPTION = "&description=";

  private static final String SUMMARY = "&summary=";

  private static final int MAX_LENGTH = 5000; // Jira akzeptiert nicht beliebig viele Zeichen...daher beschränken

  @Inject
  private transient CurrentCustomer currentCustomer;

  @Inject
  private transient ElectCommonPrincipal principal;

  public JiraUrl() {
    super();
  }

  public String getJiraUrl() {
    try {
      String jiraProjektId = currentCustomer.getCustomer().getJiraProjektId();

      if (jiraProjektId == null) {
        return JIRA_URL;
      }

      return JIRA_URL + CREATE_ISSUE + PID + jiraProjektId + ISSUE_TYPE_ERROR + PRIORITY + REPORTER + encode(getUserName()) + SUMMARY + encode(getSummary()) + ENVIRONMENT
          + encode(getEnvironment()) + DESCRIPTION + encode(getDescription());
    } catch (Exception e) {
      // ignorieren, wenn irgendwas schief läuft, einfach Jira-URL liefern
      return JIRA_URL;
    }
  }

  private String getSummary() {
    return "<!!!HIER ALLGEMEINE BESCHREIBUNG EINFÜGEN, DIE DAS PROBLEM KURZ, ABER DETAILLIERT, BESCHREIBT!!!>";
  }

  private String encode(String s) {
    try {
      return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
      // CANNOT HAPPEN
      return s;
    }
  }

  public String getEnvironment() {
    return getRequestURL() + "\n" + getViewId() + "\n" + new Date().toString();
  }

  public String getViewId() {
    try {
      return FacesContext.getCurrentInstance().getViewRoot().getViewId();
    } catch (Exception e) {
      // unwichtig
      return "";
    }
  }

  public String getRequestURL() {
    Object request = FacesContext.getCurrentInstance().getExternalContext().getRequest();
    if (request instanceof HttpServletRequest) {
      return ((HttpServletRequest) request).getRequestURL().toString();
    } else {
      return "";
    }
  }

  public String getUserName() {
    return principal.getName();
  }

  public String getDescription() {
    StringBuilder sb = new StringBuilder();

    sb.append("<!!!HIER DETAILLIERTE BESCHREIBUNG DES PROBLEMS EINFÜGEN, DIE MÖGLICHST VIELE INFORMATIONEN ENTHÄLT, UM DAS PROBLEM NACHSTELLEN ZU KÖNNEN!!!>\n\n");
    sb.append("\n");

    for (FacesMessage facesMessage : FacesContext.getCurrentInstance().getMessageList()) {
      sb.append("*******************************************************************\n");
      sb.append(facesMessage.getDetail());
    }

    String result = sb.toString();
    if (result.length() > MAX_LENGTH) {
      return result.substring(0, MAX_LENGTH);
    }
    return result;
  }
}
