package com.antwerkz.afton;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutionException;

import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.JiraRestClientFactory;
import com.atlassian.jira.rest.client.domain.Issue;
import com.atlassian.jira.rest.client.internal.async.AsynchronousJiraRestClientFactory;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Ignorant {
  private static final Logger LOG = LoggerFactory.getLogger(Ignorant.class);

  private static String userName;

  private static String password;

  public static Status ignore(GitHubIgnore annotation) {
    try {
      if (annotation == null) {
        return Status.NOT_TRACKING;
      }
      GitHub gitHub = GitHub.connect();
      GHRepository repository = gitHub.getRepository(annotation.repository());
      GHIssue issue = repository.getIssue(annotation.issue());
      if (issue != null && issue.getState() == GHIssueState.OPEN) {
        return Status.OPEN;
      } else {
        return Status.CLOSED;
      }
    } catch (FileNotFoundException e) {
      return Status.NONEXISTENT;
    } catch (IOException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }

  public static Status ignore(final JiraIgnore annotation) {
    if (annotation == null) {
      return Status.NOT_TRACKING;
    }
    try {
      JiraRestClientFactory factory = new AsynchronousJiraRestClientFactory();
      URI jiraServerUri = new URI(annotation.server());
      JiraRestClient restClient = factory
          .createWithBasicHttpAuthentication(jiraServerUri, getUserName(), getPassword());
      Issue issue = null;
      try {
        issue = restClient.getIssueClient().getIssue(annotation.issue()).get();
      } catch (ExecutionException e) {
        return Status.NONEXISTENT;
      }
      if (issue != null && !issue.getStatus().getName().equalsIgnoreCase("CLOSED")) {
        return Status.OPEN;
      } else {
        return Status.CLOSED;
      }
    } catch (URISyntaxException | InterruptedException e) {
      throw new RuntimeException(e.getMessage(), e);
    }
  }

  private static String getPassword() {
    if (password == null) {
      password = System.getProperty("jira.password");
    }
    return password;
  }

  private static String getUserName() {
    if (userName == null) {
      userName = System.getProperty("jira.userName");
    }
    return userName;
  }
}

@@ -1693,64 +1693,8 @@
 ();%0A
-      URI jiraServerUri = new URI(annotation.server());%0A
     
@@ -1780,21 +1780,36 @@
 ion(
-jiraS
+new URI(annotation.s
 erver
-Uri
+())
 , ge
@@ -1841,34 +1841,8 @@
 ));%0A
-      Issue issue = null;%0A
     
@@ -1857,16 +1857,22 @@
         
+Issue 
 issue = 
@@ -1945,111 +1945,14 @@
     
-%7D catch (ExecutionException e) %7B%0A        return Status.NONEXISTENT;%0A      %7D%0A      if (issue != null && 
+  if (
 !iss
@@ -2006,32 +2006,34 @@
 ED%22)) %7B%0A        
+  
 return Status.OP
@@ -2034,32 +2034,34 @@
 tus.OPEN;%0A      
+  
 %7D else %7B%0A       
@@ -2053,32 +2053,34 @@
  else %7B%0A        
+ 
+ 
 return Status.CL
@@ -2077,32 +2077,116 @@
  Status.CLOSED;%0A
+        %7D%0A      %7D catch (ExecutionException e) %7B%0A        return Status.NONEXISTENT;%0A
       %7D%0A    %7D ca

simplify issue checking