package org.lantern.ui;

import java.awt.Point;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;

import org.lantern.state.Settings;

import com.google.inject.Inject;
import com.google.inject.Singleton;

@Singleton
public class NotificationManager {

    List<NotificationDialog> notifications = new ArrayList<NotificationDialog>();
    private final Settings settings;
    public static final int MAX_NOTIFICATIONS = 3;

    @Inject
    public NotificationManager(Settings settings) {
        this.settings = settings;
    }

    public synchronized void notify(final NotificationDialog notification) {
        if (!settings.isUiEnabled()) {
            //no UI, no notifications
            return;
        }
        if (notifications.size() > MAX_NOTIFICATIONS ) {
            return;
        }

        for (NotificationDialog dialog : notifications) {
            if (dialog.equals(notification)) {
                //already have a dialog for this friend
                return;
            }
        }

        doNotify(notification);

    }
    /*
    static public Rectangle getScreenBounds(Window wnd) {
        Rectangle sb;
        Insets si = getScreenInsets(wnd);

        if (wnd == null) {
            sb = Toolkit.getDefaultToolkit().getGraphicsConfiguration().getBounds();
        } else {
            sb = wnd.getGraphicsConfiguration().getBounds();
        }

        sb.x += si.left;
        sb.width -= (si.left + si.right);
        sb.y += si.top;
        sb.height -= (si.top + si.bottom);
        return sb;
    }

    static public Insets getScreenInsets(Window wnd) {
        Insets si;

        if (wnd == null) {
            si = Toolkit.getDefaultToolkit().getScreenInsets(new Frame().getGraphicsConfiguration());
        } else {
            si = wnd.getToolkit().getScreenInsets(wnd.getGraphicsConfiguration());
        }
        return si;
    }
    */
    protected synchronized void doNotify(NotificationDialog notification) {
        //install the dialog in the shell

        Rectangle clientArea = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();

        int startX = clientArea.x + clientArea.width - notification.dialog.getSize().width;

        int totalHeight = 0;
        for (NotificationDialog existing : notifications) {
            totalHeight += existing.dialog.getSize().height;
        }

        totalHeight += notification.dialog.getSize().height;

        int startY = clientArea.y + clientArea.height - totalHeight;

        if (startY < 0) {
            //no need to notify
            return;
        }

        notification.dialog.setLocation(startX, startY);
        notification.dialog.setVisible(true);

        notifications.add(notification);

    }

    public void remove(NotificationDialog toRemove) {
        boolean later = false;
        for (int i = 0; i < notifications.size(); ++i) {
            NotificationDialog notification = notifications.get(i);
            if (later) {
                Point location = notification.dialog.getLocation();
                int height = notification.dialog.getSize().height;
                notification.dialog.setLocation(location.x, location.y + height);
            } else if (notification == toRemove) {
                later = true;
            }
        }
        notifications.remove(toRemove);
    }
}

@@ -18,16 +18,148 @@
 rn.ui;%0A%0A
+import java.awt.GraphicsConfiguration;%0Aimport java.awt.GraphicsDevice;%0Aimport java.awt.GraphicsEnvironment;%0Aimport java.awt.Insets;%0A
 import j
@@ -200,16 +200,41 @@
 tangle;%0A
+import java.awt.Toolkit;%0A
 import j
@@ -2218,89 +2218,21 @@
 a = 
-java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds
+getClientArea
 ();%0A
@@ -2852,32 +2852,754 @@
 ation);%0A%0A    %7D%0A%0A
+    private Rectangle getClientArea() %7B%0A%0A        GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();%0A%0A        GraphicsConfiguration gc = gd.getDefaultConfiguration();%0A        Rectangle bounds = gc.getBounds();%0A%0A        Insets screenInsets = Toolkit.getDefaultToolkit().getScreenInsets(gc);%0A%0A        Rectangle clientArea = new Rectangle();%0A%0A        clientArea.x = bounds.x + screenInsets.left;%0A        clientArea.y = bounds.y + screenInsets.top;%0A        clientArea.height = bounds.height - screenInsets.top%0A                - screenInsets.bottom;%0A        clientArea.width = bounds.width - screenInsets.left%0A                - screenInsets.right;%0A%0A        return clientArea;%0A    %7D%0A%0A
     public void 

fix #835