package actors;

import java.util.List;

import models.NotificationEvent;
import models.PullRequest;
import models.PullRequestEvent;
import models.PullRequestEventMessage;
import models.PullRequestMergeResult;
import models.enumeration.EventType;
import models.enumeration.State;
import akka.actor.UntypedActor;

/**
 * Push가 되었을때 관련있는 PullRequest의 충돌/해결/커밋변경 이벤트 등록
 *
 * 변경된 branch 와 관련있는 pull-request 에 충돌이 발생하거나 충돌이 해결되었을때
 * 해당 pull-request 를 보낸 사람에게 알림을 주고 이벤트를 추가한다.
 *
 * 커밋목록이 변경되었을때 알림을 주고 커밋변경 이벤트를 추가한다.
 */
public class PullRequestEventActor extends UntypedActor {
    @Override
    public void onReceive(Object object) throws Exception {
        if (!(object instanceof PullRequestEventMessage)) {
            return;
        }

        PullRequestEventMessage message = (PullRequestEventMessage) object;
        List<PullRequest> pullRequests = PullRequest.findRelatedPullRequests(
                message.getProject(), message.getBranch());

        for (PullRequest pullRequest : pullRequests) {
            PullRequestMergeResult mergeResult = pullRequest.attemptMerge();

            if (mergeResult.commitChanged()) {

                mergeResult.saveCommits();

                if (!mergeResult.getNewCommits().isEmpty()) {
                    PullRequestEvent.addCommitEvents(message.getSender(), pullRequest, mergeResult.getNewCommits());
                }
            }

            if (mergeResult.conflicts()) {

                mergeResult.setConflictStateOfPullRequest();

                NotificationEvent notiEvent = NotificationEvent.addPullRequestMerge(message.getSender(),
                    pullRequest, mergeResult.getGitConflicts(), message.getRequest(), State.CONFLICT);
                PullRequestEvent.addMergeEvent(notiEvent.getSender(), EventType.PULL_REQUEST_MERGED, State.CONFLICT, pullRequest);

            } else if (mergeResult.resolved()) {

                mergeResult.setResolvedStateOfPullRequest();

                NotificationEvent notiEvent = NotificationEvent.addPullRequestMerge(message.getSender(),
                    pullRequest, mergeResult.getGitConflicts(), message.getRequest(), State.RESOLVED);
                PullRequestEvent.addMergeEvent(notiEvent.getSender(), EventType.PULL_REQUEST_MERGED, State.RESOLVED, pullRequest);

            }

            mergeResult.save();
        }
    }
}

@@ -34,16 +34,48 @@
 .List;%0A%0A
+import com.jcraft.jsch.Logger;%0A%0A
 import m
@@ -662,27 +662,14 @@
 ct) 
-throws Exception %7B%0A
+%7B%0A    
     
@@ -736,16 +736,20 @@
         
+    
 return;%0A
@@ -748,34 +748,50 @@
 return;%0A        
-%7D%0A
+    %7D%0A            
 %0A        PullReq
@@ -775,32 +775,36 @@
        %0A        
+    
 PullRequestEvent
@@ -863,16 +863,20 @@
         
+  
+  
 List%3CPul
@@ -953,16 +953,20 @@
         
+   
+ 
 message.
@@ -1001,17 +1001,33 @@
 nch());%0A
-%0A
+            %0A    
         
@@ -1069,24 +1069,54 @@
 Requests) %7B%0A
+                try %7B%0A        
             
@@ -1176,25 +1176,49 @@
 mptMerge();%0A
-%0A
+                    %0A    
             
@@ -1209,32 +1209,36 @@
                 
+    
 if (mergeResult.
@@ -1248,33 +1248,65 @@
 mitChanged()) %7B%0A
-%0A
+                        %0A        
                 
@@ -1328,25 +1328,57 @@
 eCommits();%0A
-%0A
+                        %0A        
             
@@ -1423,24 +1423,32 @@
 sEmpty()) %7B%0A
+        
             
@@ -1560,34 +1560,50 @@
                 
+      
-%7D%0A
+  %7D%0A        
             %7D%0A%0A 
@@ -1592,33 +1592,61 @@
               %7D%0A
-%0A
+                    %0A        
             if (
@@ -1668,25 +1668,57 @@
 flicts()) %7B%0A
-%0A
+                        %0A        
             
@@ -1758,33 +1758,65 @@
 fPullRequest();%0A
-%0A
+                        %0A        
                 
@@ -1916,32 +1916,44 @@
                 
+ 
+           
 pullRequest, mer
@@ -2019,16 +2019,24 @@
 FLICT);%0A
+        
         
@@ -2150,33 +2150,65 @@
 , pullRequest);%0A
-%0A
+                        %0A        
             %7D el
@@ -2240,17 +2240,49 @@
 ed()) %7B%0A
-%0A
+                        %0A        
         
@@ -2334,17 +2334,49 @@
 uest();%0A
-%0A
+                        %0A        
         
@@ -2484,32 +2484,44 @@
                 
+        
+    
 pullRequest, mer
@@ -2587,16 +2587,24 @@
 OLVED);%0A
+        
         
@@ -2730,65 +2730,278 @@
 t);%0A
-%0A            %7D%0A%0A            mergeResult.save();%0A        %7D
+                        %0A                    %7D%0A                    %0A                    mergeResult.save();%0A                %7D catch (Exception e) %7B%0A                    play.Logger.error(%22Failed to check merging from %22 + pullRequest, e );%0A                %7D%0A            %7D%0A
 %0A   

Add error log on pull-request merging