package org.aslak.github.merge.service;

import java.util.HashSet;
import java.util.Set;

import org.aslak.github.merge.model.PullRequest;
import org.aslak.github.merge.rest.GithubUtil;
import org.kohsuke.github.GitHub;

public class PullRequestService {

    private Set<PullRequest> store;
    
    public PullRequestService() {
        this.store = new HashSet<>();
    }

    public PullRequest get(String user, String repo, int pull) {
        PullRequest request = locate(user, repo, pull);
        if(request == null) {
            request = fetch(user, repo, pull);
            store(request);
        }
        return request;
    }
    
    private PullRequest fetch(String user, String repo, int pull) {
        try {
            return GithubUtil.toPullRequest(
                    GitHub.connectUsingOAuth("a7d0b4356be94497a0964a0005ce09c671d616e9")
                        .getOrganization(user)
                        .getRepository(repo)
                        .getPullRequest(pull));

        } catch (Exception e) {
            throw new RuntimeException("Could not fetch PullRequest from GitHub for " + user + "/" + repo + "/" + pull, e);
        }
    }

    private PullRequest locate(String user, String repo, int pull) {
        for(PullRequest request : store) {
            if(
                    request.getSource().getUser().equals(user) &&
                    request.getSource().getRepository().equals(user) &&
                    request.getNumber() == pull) {
                return request;
            }
        }
        return null;
    }
    
    public void store(PullRequest pullrequest) {
        this.store.add(pullrequest);
    }

    public void delete(PullRequest pullRequest) {
        this.store.remove(pullRequest);
    }
}

@@ -83,16 +83,68 @@
 l.Set;%0A%0A
+import javax.enterprise.context.ApplicationScoped;%0A%0A
 import o
@@ -266,16 +266,35 @@
 itHub;%0A%0A
+@ApplicationScoped%0A
 public c
@@ -1396,30 +1396,30 @@
  request.get
-Source
+Target
 ().getUser()
@@ -1470,14 +1470,14 @@
 .get
-Source
+Target
 ().g
@@ -1490,36 +1490,36 @@
 sitory().equals(
-user
+repo
 ) &&%0A           

Fix PullRequest cache