package org.yamcs.studio.ui.links;

import java.util.ArrayList;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;

import org.eclipse.swt.widgets.Display;
import org.yamcs.protobuf.YamcsManagement.LinkInfo;

class LinkTableModel {
    private ArrayList<LinkInfo> links = new ArrayList<LinkInfo>();
    private ArrayList<Long> lastDataCountIncrease = new ArrayList<Long>();
    private ArrayList<ScheduledFuture<?>> schduledFutures = new ArrayList<ScheduledFuture<?>>();

    private LinksTableViewer linksTableViewer;

    ScheduledThreadPoolExecutor timer;
    private static final Logger log = Logger.getLogger(LinkTableModel.class.getName());

    public LinkTableModel(ScheduledThreadPoolExecutor timer, LinksTableViewer linksTableViewer) {
        this.linksTableViewer = linksTableViewer;
        this.timer = timer;
    }

    /**
     * schedule a fire rows updated , to change the color of the line if no data has been received
     * in the last two seconds
     *
     * @param row
     */
    private void scheduleFireTableRowsUpdated(final int row) {
        ScheduledFuture<?> future = schduledFutures.get(row);
        if (future != null)
            future.cancel(false);
        future = timer.schedule(new Runnable() {
            @Override
            public void run() {
                Display.getDefault().asyncExec(() ->
                {
                    // fireTableRowsUpdated(row, row);
                    });

            }
        }, 2, TimeUnit.SECONDS);
        schduledFutures.set(row, future);
    }

    public boolean isDataCountIncreasing(LinkInfo li) {
        for (int i = 0; i < links.size(); i++)
        {
            if (links.get(i) == li)
                return (System.currentTimeMillis() - lastDataCountIncrease.get(i)) < 1500;
        }
        log.severe("Unable to find the datalink in the model. " + li);
        return false;

    }

    public void updateLink(LinkInfo uli) {
        boolean found = false;
        for (int i = 0; i < links.size(); ++i) {
            LinkInfo li = links.get(i);
            if (li.getName().equals(uli.getName())) {
                if (uli.getDataCount() > li.getDataCount()) {
                    lastDataCountIncrease.set(i, System.currentTimeMillis());
                    scheduleFireTableRowsUpdated(i);
                }
                links.set(i, uli);
                if (linksTableViewer.getTable().isDisposed())
                    return;
                linksTableViewer.replace(uli, i);
                found = true;
                break;
            }
        }
        if (!found) {
            links.add(uli);
            lastDataCountIncrease.add(0L);
            schduledFutures.add(null);
            linksTableViewer.add(uli);
        }

    }

}

@@ -235,48 +235,8 @@
 r;%0A%0A
-import org.eclipse.swt.widgets.Display;%0A
 impo
@@ -1300,113 +1300,148 @@
 ule(
-new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A                Display.getDefault
+() -%3E %7B%0A            if (linksTableViewer.getTable().isDisposed())%0A                return;%0A            linksTableViewer.getTable().getDisplay
 ().a
@@ -1454,16 +1454,18 @@
 ec(() -%3E
+ %7B
 %0A       
@@ -1465,38 +1465,106 @@
                 
-%7B%0A    
+if (linksTableViewer.getTable().isDisposed())%0A                    return;%0A
                 
@@ -1614,31 +1614,20 @@
     
-        
 %7D);%0A
-%0A  
         
   %7D%0A
@@ -1622,29 +1622,18 @@
         
-  %7D%0A        
 %7D
+ 
 , 2, Tim
@@ -1749,24 +1749,24 @@
 kInfo li) %7B%0A
+
         for 
@@ -1799,24 +1799,16 @@
 (); i++)
-%0A       
  %7B%0A     

Fix an unclean shutdown issue