package org.helioviewer.plugins.eveplugin.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.helioviewer.plugins.eveplugin.draw.DrawableElement;
import org.helioviewer.plugins.eveplugin.draw.DrawableType;
import org.helioviewer.plugins.eveplugin.draw.YAxisElement;

public class DrawControllerData {

    private int nrOfDrawableElements;
    private Set<YAxisElement> yAxisSet;
    private final Map<DrawableType, Set<DrawableElement>> drawableElements;
    private List<DrawControllerListener> listeners;

    public DrawControllerData() {
        nrOfDrawableElements = 0;
        drawableElements = new HashMap<DrawableType, Set<DrawableElement>>();
        listeners = Collections.synchronizedList(new ArrayList<DrawControllerListener>());
        yAxisSet = new HashSet<YAxisElement>();
    }

    public int getNrOfDrawableElements() {
        return nrOfDrawableElements;
    }

    public void setNrOfDrawableElements(int nrOfDrawableElements) {
        this.nrOfDrawableElements = nrOfDrawableElements;
    }

    public Set<YAxisElement> getyAxisSet() {
        synchronized (yAxisSet) {
            return yAxisSet;
        }
    }

    public void setyAxisSet(Set<YAxisElement> yAxisSet) {
        this.yAxisSet = yAxisSet;
    }

    public Map<DrawableType, Set<DrawableElement>> getDrawableElements() {
        return drawableElements;
    }

    public List<DrawControllerListener> getListeners() {
        synchronized (listeners) {
            List<DrawControllerListener> temp = new ArrayList<DrawControllerListener>(listeners);
            return temp;
        }

    }

    public void setListeners(List<DrawControllerListener> listeners) {
        synchronized (this.listeners) {
            this.listeners = listeners;
        }
    }

    public void addDrawControllerListener(DrawControllerListener listener) {
        synchronized (listeners) {
            listeners.add(listener);
        }
    }

    public void removeDrawControllerListener(DrawControllerListener listener) {
        synchronized (listeners) {
            listeners.remove(listener);
        }

    }

    public void addDrawableElement(final DrawableElement element) {
        Set<DrawableElement> elements = drawableElements.get(element.getDrawableElementType().getLevel());
        if (elements == null) {
            elements = Collections.synchronizedSet(new HashSet<DrawableElement>());
            drawableElements.put(element.getDrawableElementType().getLevel(), elements);
        }

        elements.add(element);

        Set<YAxisElement> tempSet = new HashSet<YAxisElement>(yAxisSet);
        if (element.getYAxisElement() != null) {
            tempSet.add(element.getYAxisElement());
            yAxisSet = tempSet;
        }
        nrOfDrawableElements++;
    }

    public void removeDrawableElement(final DrawableElement element) {
        Set<DrawableElement> elements = drawableElements.get(element.getDrawableElementType().getLevel());
        if (elements != null) {
            synchronized (elements) {
                elements.remove(element);
                nrOfDrawableElements--;
                synchronized (yAxisSet) {
                    createYAxisSet();
                }
            }
        }
    }

    public Date getLastDateWithData() {
        Date lastDate = null;
        for (Set<DrawableElement> des : drawableElements.values()) {
            for (DrawableElement de : des) {
                if (de.getLastDateWithData() != null) {
                    if (lastDate == null || de.getLastDateWithData().before(lastDate)) {
                        lastDate = de.getLastDateWithData();
                    }
                }
            }
        }
        return lastDate;
    }

    private void createYAxisSet() {
        Set<YAxisElement> tempSet = new HashSet<YAxisElement>();
        for (Set<DrawableElement> elementsSet : drawableElements.values()) {
            for (DrawableElement de : elementsSet) {
                if (de.getYAxisElement() != null) {
                    tempSet.add(de.getYAxisElement());
                }
            }
        }
        yAxisSet = tempSet;
    }
}

@@ -80,38 +80,8 @@
 st;%0A
-import java.util.Collections;%0A
 impo
@@ -604,16 +604,22 @@
 private 
+final 
 List%3CDra
@@ -821,37 +821,8 @@
 s = 
-Collections.synchronizedList(
 new 
@@ -856,17 +856,16 @@
 tener%3E()
-)
 ;%0A      
@@ -1182,46 +1182,8 @@
 ) %7B%0A
-        synchronized (yAxisSet) %7B%0A    
     
@@ -1199,34 +1199,24 @@
 n yAxisSet;%0A
-        %7D%0A
     %7D%0A%0A    p
@@ -1485,47 +1485,8 @@
 ) %7B%0A
-        synchronized (listeners) %7B%0A    
     
@@ -1579,28 +1579,24 @@
 s);%0A        
-    
 return temp;
@@ -1600,186 +1600,8 @@
 mp;%0A
-        %7D%0A%0A    %7D%0A%0A    public void setListeners(List%3CDrawControllerListener%3E listeners) %7B%0A        synchronized (this.listeners) %7B%0A            this.listeners = listeners;%0A        %7D
 %0A   
@@ -1685,47 +1685,8 @@
 ) %7B%0A
-        synchronized (listeners) %7B%0A    
     
@@ -1714,26 +1714,16 @@
 tener);%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1805,47 +1805,8 @@
 ) %7B%0A
-        synchronized (listeners) %7B%0A    
     
@@ -1837,27 +1837,16 @@
 tener);%0A
-        %7D%0A%0A
     %7D%0A%0A 
@@ -2078,36 +2078,8 @@
 s = 
-Collections.synchronizedSet(
 new 
@@ -2104,17 +2104,16 @@
 ement%3E()
-)
 ;%0A      
@@ -2707,50 +2707,8 @@
 ) %7B%0A
-            synchronized (elements) %7B%0A    
     
@@ -2737,28 +2737,24 @@
 e(element);%0A
-    
             
@@ -2781,58 +2781,8 @@
 --;%0A
-                synchronized (yAxisSet) %7B%0A        
     
@@ -2819,41 +2819,10 @@
     
-        %7D%0A            %7D%0A        
 %7D
+%0A
 %0A   

Remove synchronised from DrawControllerDate