package eu.over9000.skadi.notification;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.imageio.ImageIO;

import eu.over9000.skadi.SkadiMain;
import eu.over9000.skadi.channel.Channel;
import eu.over9000.skadi.channel.ChannelEventListener;
import eu.over9000.skadi.channel.ChannelManager;
import eu.over9000.skadi.util.StringUtil;

public class NotificationManager implements ChannelEventListener {
	
	private static NotificationManager instance;
	
	public static NotificationManager getInstance() {
		if (NotificationManager.instance == null) {
			NotificationManager.instance = new NotificationManager();
		}
		return NotificationManager.instance;
	}
	
	private TrayIcon trayIcon;
	
	public NotificationManager() {
		try {
			final Image image = ImageIO.read(this.getClass().getResource("/icon.png"));
			this.trayIcon = new TrayIcon(image, "Skadi", null);
			this.trayIcon.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(final MouseEvent e) {
					if ((e.getClickCount() == 1) || (e.getClickCount() == 2)) {
						System.out.println("clicked on icon");
					}
				}
			});
			
			if (SystemTray.isSupported()) {
				SystemTray.getSystemTray().add(this.trayIcon);
			}
			
			ChannelManager.getInstance().addListener(this);
			
		} catch (final IOException | AWTException e1) {
			e1.printStackTrace();
		}
	}
	
	public void displayNotification(final String header, final String message) {
		if (this.trayIcon == null) {
			return;
		}
		
		this.trayIcon.displayMessage(header, message, MessageType.NONE);
		
	}
	
	@Override
	public void added(final Channel channel, final int count) {
	}
	
	@Override
	public void removed(final Channel channel, final int count) {
	}
	
	@Override
	public void updatedMetadata(final Channel channel) {
		if (SkadiMain.getInstance().display_notifications && channel.wentOnline()) {
			this.displayNotification(StringUtil.extractChannelName(channel.getURL()) + " went live", channel
			        .getMetadata().getTitle());
		}
	}
	
	@Override
	public void updatedStreamdata(final Channel channel) {
	}
	
	@Override
	public String getListenerName() {
		return this.getClass().getName();
	}
}

@@ -927,16 +927,52 @@
 %09%09try %7B%0A
+%09%09%09if (SystemTray.isSupported()) %7B%0A%09
 %09%09%09final
@@ -1041,24 +1041,25 @@
 .png%22));%0A%09%09%09
+%09
 this.trayIco
@@ -1098,16 +1098,17 @@
  null);%0A
+%09
 %09%09%09this.
@@ -1158,16 +1158,17 @@
 ) %7B%0A%09%09%09%09
+%09
 @Overrid
@@ -1169,16 +1169,17 @@
 verride%0A
+%09
 %09%09%09%09publ
@@ -1226,16 +1226,17 @@
  %7B%0A%09%09%09%09%09
+%09
 if ((e.g
@@ -1289,24 +1289,25 @@
 2)) %7B%0A%09%09%09%09%09%09
+%09
 System.out.p
@@ -1342,16 +1342,19 @@
 %09%09%09%09
+%09
 %7D%0A
+%09
 %09%09%09%09%7D%0A
+%09
 %09%09%09%7D
@@ -1363,43 +1363,9 @@
 %0A%09%09%09
-%0A%09%09%09if (SystemTray.isSupported()) %7B
+%09
 %0A%09%09%09

improve notification handler