package com.googlesource.gerrit.plugins.hooks.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.gerrit.server.config.GerritServerConfig;
import com.google.inject.Inject;

import com.googlesource.gerrit.plugins.hooks.its.ItsName;

import org.eclipse.jgit.lib.Config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class IssueExtractor {
  private static final Logger log = LoggerFactory.getLogger(
      IssueExtractor.class);

  @Inject @GerritServerConfig
  private Config gerritConfig;

  @Inject @ItsName
  private String itsName;

  /**
   * Gets issue ids from a string.
   *
   * @param haystack String to extract issue ids from
   * @return array of {@link Strings}. Each String being a found issue id.
   */
  public String[] getIssueIds(String haystack) {
    List<Pattern> commentRegexList = getCommentRegexList();
    if (commentRegexList == null) return new String[] {};

    log.debug("Matching '" + haystack + "' against " + commentRegexList);

    ArrayList<String> issues = new ArrayList<String>();
    for (Pattern pattern : commentRegexList) {
      Matcher matcher = pattern.matcher(haystack);

      while (matcher.find()) {
        String issueId = extractMatchedWorkItems(matcher);
        if (issueId != null) {
          issues.add(issueId);
        }
      }
    }

    return issues.toArray(new String[issues.size()]);
  }

  public String extractMatchedWorkItems(Matcher matcher) {
    int groupCount = matcher.groupCount();
    if (groupCount >= 1)
      return matcher.group(1);
    else
      return null;
  }

  /**
   * Gets the regular expression used to identify issue ids.
   * @return the regular expression, or {@code null}, if there is no pattern
   *    to match issue ids.
   */
  public Pattern getPattern() {
    Pattern ret = null;
    String match = gerritConfig.getString("commentLink", itsName, "match");
    if (match != null) {
      ret = Pattern.compile(match);
    }
    return ret;
  }

  private List<Pattern> getCommentRegexList() {
    ArrayList<Pattern> regexList = new ArrayList<Pattern>();

    Pattern pattern = getPattern();
    if (pattern != null) {
      regexList.add(pattern);
    }

    return regexList;
  }
}

@@ -78,31 +78,8 @@
 st;%0A
-import java.util.List;%0A
 impo
@@ -853,88 +853,55 @@
     
-List%3C
 Pattern
-%3E commentRegexList = getCommentRegexList();%0A    if (commentRegexList
+ pattern = getPattern();%0A    if (pattern
  == 
@@ -986,32 +986,33 @@
 nst %22 + 
-commentRegexList
+pattern.pattern()
 );%0A%0A    
@@ -1067,57 +1067,8 @@
 ();%0A
-    for (Pattern pattern : commentRegexList) %7B%0A  
     
@@ -1117,18 +1117,16 @@
 );%0A%0A    
-  
 while (m
@@ -1138,26 +1138,24 @@
 r.find()) %7B%0A
-  
       String
@@ -1205,18 +1205,16 @@
 ;%0A      
-  
 if (issu
@@ -1236,18 +1236,16 @@
         
-  
 issues.a
@@ -1261,18 +1261,8 @@
 d);%0A
-        %7D%0A
     
@@ -1922,244 +1922,7 @@
 %0A  %7D
-%0A%0A  private List%3CPattern%3E getCommentRegexList() %7B%0A    ArrayList%3CPattern%3E regexList = new ArrayList%3CPattern%3E();%0A%0A    Pattern pattern = getPattern();%0A    if (pattern != null) %7B%0A      regexList.add(pattern);%0A    %7D%0A%0A    return regexList;%0A  %7D
 %0A%7D%0A

Stop using a list for patterns, as there is at most one pattern