package org.helioviewer.jhv.plugins.swhvhekplugin;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;

import org.helioviewer.base.logging.Log;
import org.helioviewer.jhv.data.container.JHVEventContainer;
import org.helioviewer.jhv.data.container.JHVEventHandler;
import org.helioviewer.jhv.data.container.cache.JHVEventCache;
import org.helioviewer.jhv.data.container.cache.JHVEventCacheResult;
import org.helioviewer.jhv.data.datatype.event.JHVEvent;
import org.helioviewer.jhv.display.Displayer;
import org.helioviewer.jhv.layers.Layers;
import org.helioviewer.jhv.layers.LayersListener;
import org.helioviewer.viewmodel.view.AbstractView;

/**
 * This class intercepts changes of the layers and request data from the
 * JHVEventContainer.
 *
 * @author Bram Bourgoignie (Bram.Bourgoignie@oma.be)
 *
 */
public class SWHVHEKData implements LayersListener, JHVEventHandler {

    private static SWHVHEKData instance;
    private Map<String, NavigableMap<Date, NavigableMap<Date, List<JHVEvent>>>> data;
    private ArrayList<JHVEvent> events;
    private Date beginDate = null;
    private Date endDate = null;

    private SWHVHEKData() {
    }

    public static SWHVHEKData getSingletonInstance() {
        if (instance == null) {
            instance = new SWHVHEKData();
        }
        return instance;
    }

    public void reset() {
        instance = null;
    }

    public void requestEvents() {
        boolean request = false;
        Date first = Layers.getFirstDate();
        Date last = Layers.getLastDate();
        if (beginDate == null || (first != null && first.getTime() < beginDate.getTime())) {
            beginDate = first;
            request = true;
        }
        if (endDate == null || (last != null && last.getTime() > endDate.getTime())) {
            endDate = last;
            request = true;
        }

        if (request) {
            JHVEventContainer.getSingletonInstance().requestForInterval(beginDate, endDate, SWHVHEKData.this);
        }
    }

    @Override
    public void layerAdded(AbstractView view) {
        requestEvents();
    }

    @Override
    public void activeLayerChanged(AbstractView view) {
    }

    @Override
    public void newEventsReceived(Map<String, NavigableMap<Date, NavigableMap<Date, List<JHVEvent>>>> eventList) {
        cacheUpdated();
    }

    @Override
    public void cacheUpdated() {
        if (beginDate != null && endDate != null) {
            JHVEventCacheResult result = JHVEventCache.getSingletonInstance().get(beginDate, endDate, beginDate, endDate);
            data = result.getAvailableEvents();
            ArrayList<JHVEvent> events = new ArrayList<JHVEvent>();
            for (String eventType : data.keySet()) {
                for (Date sDate : data.get(eventType).keySet()) {
                    for (Date eDate : data.get(eventType).get(sDate).keySet()) {
                        for (JHVEvent event : data.get(eventType).get(sDate).get(eDate)) {
                            events.add(event);
                        }
                    }
                }
            }
            this.events = events;
            Displayer.display();
        }
    }

    public Map<String, NavigableMap<Date, NavigableMap<Date, List<JHVEvent>>>> getData() {
        return data;
    }

    public ArrayList<JHVEvent> getActiveEvents(Date currentDate) {
        ArrayList<JHVEvent> activeEvents = new ArrayList<JHVEvent>();
        if (events != null) {
            for (JHVEvent event : events) {
                if (event != null && event.getStartDate() != null && event.getEndDate() != null) {
                    if (event.getStartDate().getTime() <= currentDate.getTime() && event.getEndDate().getTime() >= currentDate.getTime()) {
                        activeEvents.add(event);
                    }
                    event.addHighlightListener(Displayer.getSingletonInstance());
                } else {
                    Log.warn("Possibly something strange is going on with incoming events. Either the date or the event is null");
                }
            }
        }
        return activeEvents;
    }

}

@@ -2376,28 +2376,29 @@
         
-cacheUpdated
+displayEvents
 ();%0A    
@@ -2495,32 +2495,138 @@
  != null) %7B%0A    
+        requestEvents();%0A            displayEvents();%0A        %7D%0A    %7D%0A%0A    private void displayEvents() %7B%0A
         JHVEvent
@@ -2736,20 +2736,16 @@
         
-    
 data = r
@@ -2764,36 +2764,32 @@
 ilableEvents();%0A
-    
         ArrayLis
@@ -2840,28 +2840,24 @@
 ();%0A        
-  
-  
 for (String 
@@ -2877,36 +2877,32 @@
 ata.keySet()) %7B%0A
-    
             for 
@@ -2939,36 +2939,32 @@
 pe).keySet()) %7B%0A
-    
                 
@@ -3036,36 +3036,32 @@
                 
-    
 for (JHVEvent ev
@@ -3131,28 +3131,24 @@
             
-    
 events.add(e
@@ -3170,34 +3170,26 @@
             
-   
- 
 %7D%0A
-    
             
@@ -3198,36 +3198,32 @@
   %7D%0A            
-    
 %7D%0A            %7D%0A
@@ -3212,34 +3212,26 @@
   %7D%0A        
- 
-   %7D%0A    
+%7D%0A
         this
@@ -3248,20 +3248,16 @@
 events;%0A
-    
         
@@ -3273,34 +3273,24 @@
 .display();%0A
-        %7D%0A
     %7D%0A%0A    p

Use JHVContainer in better way: should eliminate events not loading