package com.flowdock.plugins.confluence;

import com.atlassian.confluence.event.events.content.ContentEvent;
import com.atlassian.confluence.event.events.content.comment.CommentCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageCreateEvent;
import com.atlassian.confluence.event.events.content.page.PageEvent;
import com.atlassian.confluence.event.events.content.page.PageTrashedEvent;
import com.atlassian.confluence.event.events.content.page.PageUpdateEvent;
import com.atlassian.confluence.pages.Page;
import com.atlassian.event.api.EventListener;
import com.atlassian.event.api.EventPublisher;
import com.flowdock.plugins.confluence.config.FlowdockConfigurationManager;

/**
 * Listen to all kinds of events we're aware of.
 * 
 * This class needs to be in sync with FlowdockEventRenderer, who
 * turns all events into POSTable data.
 * 
 * @author mutru
 *
 */
public class ChangeListener {
	private FlowdockEventRenderer eventRenderer = null;
	private FlowdockConfigurationManager flowdockConfigurationManager = null;
	
	public ChangeListener(EventPublisher eventPublisher, FlowdockConfigurationManager manager, FlowdockEventRenderer eventRenderer) {
		this.eventRenderer = eventRenderer;
		this.setFlowdockConfigurationManager(manager);
		eventPublisher.register(this);
	}
	
	@EventListener
	public void pageUpdateEvent(PageUpdateEvent event) {
		this.sendNotification(event);
	}
	
	@EventListener
	public void pageCreateEvent(PageCreateEvent event) {
		this.sendNotification(event);
	}
	
	@EventListener
	public void pageTrashedEvent(PageTrashedEvent event) {
		this.sendNotification(event);
	}
	
	@EventListener
	public void commentCreateEvent(CommentCreateEvent event) {
		this.sendNotification(event);
	}
	
	// Bean configuration
	public void setFlowdockConfigurationManager(FlowdockConfigurationManager manager) {
		this.flowdockConfigurationManager = manager;
	}
	
	private void sendNotification(final CommentCreateEvent event) {
		if (event.getComment().getOwner().getType() == "page") {
			Page page = (Page)event.getComment().getOwner();
			this.sendNotification(page, event);
		}
	}
	
	private void sendNotification(final PageEvent event) {
		this.sendNotification(event.getPage(), event);
	}
	
	private void sendNotification(final Page page, final ContentEvent event) {
		final String apiKey = flowdockConfigurationManager.getApiKeyForSpace(page.getSpace());
		
		Thread t = new Thread(new Runnable() {
			public void run() {
				FlowdockConnection.sendApiMessage(eventRenderer.renderEvent(event), apiKey);
			}
		});
		t.start();
	}
}

@@ -690,16 +690,73 @@
 Manager;
+%0Aimport org.springframework.beans.factory.DisposableBean;
 %0A%0A/**%0A *
@@ -968,16 +968,42 @@
 istener 
+implements DisposableBean 
 %7B%0A%09priva
@@ -1124,16 +1124,61 @@
 = null;%0A
+    protected EventPublisher eventPublisher;%0A
 %09%0A%09publi
@@ -1298,24 +1298,24 @@
 Renderer) %7B%0A
-
 %09%09this.event
@@ -1340,16 +1340,62 @@
 nderer;%0A
+        this.eventPublisher = eventPublisher;%0A
 %09%09this.s
@@ -2742,11 +2742,181 @@
 rt();%0A%09%7D
+%0A%0A    // Unregister the listener if the plugin is uninstalled or disabled.%0A    public void destroy() throws Exception%0A    %7B%0A        eventPublisher.unregister(this);%0A    %7D
 %0A%7D%0A

Implement DisposableBean so plugin can be uninstalled without restarting Confluence.