package selfservice.service.impl;

import static com.google.common.base.Preconditions.checkNotNull;
import static selfservice.domain.Action.Type.LINKREQUEST;
import static selfservice.domain.Action.Type.QUESTION;
import static selfservice.domain.Action.Type.UNLINKREQUEST;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.common.base.MoreObjects;

import selfservice.domain.Action;
import selfservice.domain.LicenseStatus;

class JiraTicketSummaryAndDescriptionBuilder {

  static SummaryAndDescription build(final Action action) {
    checkNotNull(action);

    StringBuilder description = new StringBuilder();

    final StringBuilder summary = new StringBuilder();

    if (action.getType().equals(QUESTION)) {
      description.append("Question: ").append(action.getBody()).append("\n");
      summary.
        append("Question about ").
        append(action.getSpId());
    } else if (LINKREQUEST.equals(action.getType())) {
      description.append("Request: Create a new connection").append("\n");
      summary.
        append("New connection for IdP ").
        append(action.getIdpId()).
        append(" to SP ").
        append(action.getSpId());
    } else if (UNLINKREQUEST.equals(action.getType())) {
      description.append("Request: terminate a connection").append("\n");
      summary.
        append("Disconnect IdP ").
        append(action.getIdpId()).
        append(" and SP ").
        append(action.getSpId());
    } else {
      throw new IllegalArgumentException("Don't know how to handle tasks of type " + action.getType());
    }

    description.append("Applicant name: ").append(action.getUserName()).append("\n");
    description.append("Applicant email: ").append(action.getUserEmail()).append("\n");
    description.append("Identity Provider: ").append(action.getIdpId()).append("\n");
    description.append("Service Provider: ").append(action.getSpId()).append("\n");
    description.append("License required: ").append(action.getService().getLicenseStatus().getName()).append("\n");
    description.append("License secured: ").append(licenseSecured(action)).append("\n");
    description.append("Time: ").append(new SimpleDateFormat("HH:mm dd-MM-yy").format(new Date())).append("\n");

    if (!action.getType().equals(QUESTION)) {
      description.append("Remark from user: ").append(action.getBody()).append("\n");
    }

    return new SummaryAndDescription(summary.toString(), description.toString());
  }

  static String licenseSecured(Action action) {
    LicenseStatus status = action.getService().getLicenseStatus();

    switch(status) {
      case HAS_LICENSE_SP:
        return "Unknown";
      case HAS_LICENSE_SURFMARKET:
        if (!action.getService().isHasCrmLink()) {
          return "Unknown";
        } else if (action.getService().getLicense() != null) {
          return "Yes";
        }
        return "No";
      case NOT_NEEDED:
        return "n/a";
      default:
        return "Unknown";
    }
  }

  static class SummaryAndDescription {
    public final String summary;
    public final String description;

    public SummaryAndDescription(String summary, String description) {
      this.summary = summary;
      this.description = description;
    }

    @Override
    public String toString() {
      return MoreObjects.toStringHelper(SummaryAndDescription.class)
          .add("summary", summary)
          .add("description", description).toString();
    }
  }
}

@@ -1920,32 +1920,123 @@
 ).append(%22%5Cn%22);%0A
+    if (action.getService() != null && action.getService().getLicenseStatus() != null) %7B%0A  
     description.
@@ -2131,32 +2131,34 @@
 pend(%22%5Cn%22);%0A    
+  
 description.appe
@@ -2218,32 +2218,38 @@
 ).append(%22%5Cn%22);%0A
+    %7D%0A
     description.

Make service for an action optional