package minhhai2209.jirapluginconverter.plugin.render;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.text.StrSubstitutor;

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;

public class ParameterContextBuilder {

  private Map<String, String> contextMapper;

  public ParameterContextBuilder() {
    buildContextMapper();
  }

  private void buildContextMapper() {
    contextMapper = new HashMap<String, String>();
    contextMapper.put("issue.id", "");
    contextMapper.put("issue.key", "");
    contextMapper.put("issuetype.id", "");
    contextMapper.put("project.id", "");
    contextMapper.put("project.key", "");
    contextMapper.put("version.id", "");
    contextMapper.put("component.id", "");
  }

  @SuppressWarnings("unchecked")
  public void buildContextParams(HttpServletRequest request, Map<String, String> context) {
    Map<String, String[]> contextParams = request.getParameterMap();
    ProjectManager projectManager = ComponentAccessor.getProjectManager();
    IssueManager issueManager = ComponentAccessor.getIssueManager();
    String issueKey = contextParams.get("issueKey")[0];
    context.putAll(contextMapper);
    if (!issueKey.contains("$")) {
      context.put("issue.key", issueKey);
      MutableIssue issue = issueManager.getIssueByCurrentKey(issueKey);
      if (issue != null) {
        context.put("issue.id", issue.getId().toString());
        context.put("issuetype.id", issue.getIssueTypeId());
      }
    }

    String projectKey = contextParams.get("projectKey")[0];
    if (!projectKey.contains("$")) {
      context.put("project.key", projectKey);
      Project project = projectManager.getProjectByCurrentKey(projectKey);
      if (project != null) {
        context.put("project.id", project.getId().toString());
      }
    }

    String versionId = contextParams.get("versionId")[0];
    if (!versionId.contains("$")) {
      context.put("version.id", versionId);
    }

    String componentId = contextParams.get("componentId")[0];
    if (!componentId.contains("$")) {
      context.put("component.id", componentId);
    }
  }

  public String buildUrl(HttpServletRequest request, String url) {
    Map<String, String> contextParams = new HashMap<String, String>();
    buildContextParams(request, contextParams);
    StrSubstitutor substitutor = new StrSubstitutor(contextParams, "{", "}");
    url = substitutor.replace(url);
    return url;
  }
}

@@ -1310,24 +1310,76 @@
 eManager();%0A
+%0A    if (contextParams.containsKey(%22issueKey%22)) %7B%0A  
     String i
@@ -1422,24 +1422,26 @@
 y%22)%5B0%5D;%0A    
+ 
+ 
 context.putA
@@ -1459,16 +1459,18 @@
 apper);%0A
+  
     if (
@@ -1488,32 +1488,34 @@
 ontains(%22$%22)) %7B%0A
+  
       context.pu
@@ -1546,16 +1546,18 @@
 ;%0A      
+  
 MutableI
@@ -1610,24 +1610,26 @@
 (issueKey);%0A
+  
       if (is
@@ -1643,32 +1643,34 @@
 null) %7B%0A        
+  
 context.put(%22iss
@@ -1700,24 +1700,26 @@
 oString());%0A
+  
         cont
@@ -1763,24 +1763,34 @@
 eTypeId());%0A
+        %7D%0A
       %7D%0A    
@@ -1784,32 +1784,85 @@
 %0A      %7D%0A    %7D%0A%0A
+    if (contextParams.containsKey(%22projectKey%22)) %7B%0A  
     String proje
@@ -1905,24 +1905,26 @@
 y%22)%5B0%5D;%0A    
+  
 if (!project
@@ -1942,32 +1942,34 @@
 s(%22$%22)) %7B%0A      
+ 
+ 
 context.put(%22pro
@@ -1988,24 +1988,26 @@
 rojectKey);%0A
+  
       Projec
@@ -2071,24 +2071,26 @@
 Key);%0A      
+ 
+ 
 if (project 
@@ -2092,32 +2092,34 @@
 ject != null) %7B%0A
+  
         context.
@@ -2161,24 +2161,34 @@
 oString());%0A
+        %7D%0A
       %7D%0A    
@@ -2182,32 +2182,84 @@
 %0A      %7D%0A    %7D%0A%0A
+    if (contextParams.containsKey(%22versionId%22)) %7B%0A  
     String versi
@@ -2300,24 +2300,26 @@
 d%22)%5B0%5D;%0A    
+ 
+ 
 if (!version
@@ -2330,32 +2330,34 @@
 ontains(%22$%22)) %7B%0A
+  
       context.pu
@@ -2384,27 +2384,89 @@
 ionId);%0A    
-%7D%0A%0A
+  %7D%0A    %7D%0A%0A    if (contextParams.containsKey(%22componentId%22)) %7B%0A  
     String c
@@ -2515,24 +2515,26 @@
 d%22)%5B0%5D;%0A    
+  
 if (!compone
@@ -2551,24 +2551,26 @@
 ins(%22$%22)) %7B%0A
+  
       contex
@@ -2601,24 +2601,32 @@
 mponentId);%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A 

fix bug in admin page