/*
 * Copyright (C) 2019 tobi.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301  USA
 */
package net.sf.jaer.eventprocessing.filter;

import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.util.gl2.GLUT;
import java.util.HashMap;
import net.sf.jaer.chip.AEChip;
import net.sf.jaer.eventprocessing.EventFilter2D;
import net.sf.jaer.graphics.FrameAnnotater;

/**
 * Superclass for all noise filters
 *
 * @author tobi
 */
public abstract class AbstractNoiseFilter extends EventFilter2D implements FrameAnnotater {

    protected boolean showFilteringStatistics = getBoolean("showFilteringStatistics", true);
    protected int totalEventCount = 0;
    protected int filteredOutEventCount = 0;
    /** Map from noise filters to drawing positions of noise filtering statistics annotations */
    static protected HashMap<AbstractNoiseFilter,Integer> noiseStatDrawingMap=new HashMap<AbstractNoiseFilter,Integer>();
    protected int statisticsDrawingPosition=-10; // y coordinate we write ourselves to, start with -10 so we end up at 0 for first one (hack)

    public AbstractNoiseFilter(AEChip chip) {
        super(chip);
        setPropertyTooltip("showFilteringStatistics", "Annotates screen with percentage of filtered out events, if filter implements this count");
   }

    /**
     * @return the showFilteringStatistics
     */
    public boolean isShowFilteringStatistics() {
        return showFilteringStatistics;
    }

    /**
     * @param showFilteringStatistics the showFilteringStatistics to set
     */
    public void setShowFilteringStatistics(boolean showFilteringStatistics) {
        this.showFilteringStatistics = showFilteringStatistics;
        putBoolean("showFilteringStatistics", showFilteringStatistics);
    }

    @Override
    public void annotate(GLAutoDrawable drawable) {
        if (!showFilteringStatistics) {
            return;
        }
        findUnusedDawingY();
        GL2 gl = drawable.getGL().getGL2();
        gl.glPushMatrix();
        final GLUT glut = new GLUT();
        gl.glColor3f(.2f, .2f, .8f); // must set color before raster position (raster position is like glVertex)
        gl.glRasterPos3f(0, statisticsDrawingPosition, 0);
        final float filteredOutPercent = 100 * (float) filteredOutEventCount / totalEventCount;
        String s = null;
        s = String.format("%s: filtered out %%%6.1f",
                getClass().getSimpleName(),
                filteredOutPercent);
        glut.glutBitmapString(GLUT.BITMAP_HELVETICA_18, s);
        gl.glPopMatrix();
    }

    protected void findUnusedDawingY() {
        // find a y posiiton not used yet for us
        if(noiseStatDrawingMap.get(this)==null){
            for(int y:noiseStatDrawingMap.values()){
                if(y>statisticsDrawingPosition) statisticsDrawingPosition=y;
            }
            statisticsDrawingPosition=statisticsDrawingPosition+10;
            noiseStatDrawingMap.put(this,statisticsDrawingPosition);
        }
    }

}

@@ -930,16 +930,44 @@
 2.GLUT;%0A
+import java.io.IOException;%0A
 import j
@@ -1109,16 +1109,104 @@
 notater;
+%0Aimport net.sf.jaer.util.RemoteControlCommand;%0Aimport net.sf.jaer.util.RemoteControlled;
 %0A%0A/**%0A *
@@ -1351,16 +1351,34 @@
 nnotater
+, RemoteControlled
  %7B%0A%0A    
@@ -2126,16 +2126,209 @@
 ount%22);%0A
+        %0A        if (chip.getRemoteControl() != null) %7B%0A            chip.getRemoteControl().addCommandListener(this, %22setNoiseFilterParameters%22, %22set correlation time or distance.%22);%0A        %7D%0A
    %7D%0A%0A  
@@ -4021,11 +4021,658 @@
 %7D%0A    %7D%0A
+     %0A    private String USAGE = %22Need at least 2 arguments: noisefilter %3Ccommand%3E %3Cargs%3E%5CnCommands are: specific to the filter%5Cn%22;%0A %0A    public String processRemoteControlCommand(RemoteControlCommand command, String input) %7B%0A        String%5B%5D tok = input.split(%22%5C%5Cs%22, 3);%0A        if (tok.length %3C 2) %7B%0A            return USAGE;%0A        %7D%0A%0A        log.info(%22Received Command:%22 + input);%0A        return USAGE;%0A    %7D%0A    %0A    public String setParameters(RemoteControlCommand command, String input) %7B%0A        String%5B%5D tok = input.split(%22%5C%5Cs%22, 3);%0A        if (tok.length %3C 2) %7B%0A            return USAGE;%0A        %7D%0A        %0A        return USAGE;%0A    %7D%0A%0A
 %0A%7D%0A

use nc command to set parameters