package org.kurento.room.demo;

import java.util.SortedMap;

import org.kurento.client.Continuation;
import org.kurento.client.Filter;
import org.kurento.module.markerdetector.ArMarkerdetector;
import org.kurento.room.api.UserNotificationService;
import org.kurento.room.internal.DefaultNotificationRoomHandler;
import org.kurento.room.internal.Participant;
import org.kurento.room.internal.ProtocolElements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.JsonObject;

public class DemoNotificationRoomHandler extends DefaultNotificationRoomHandler {

  private static final Logger log = LoggerFactory.getLogger(DemoNotificationRoomHandler.class);
  private SortedMap<Integer, String> markerUrls;

  private UserNotificationService notifService;

  public DemoNotificationRoomHandler(UserNotificationService notifService) {
    super(notifService);

    this.notifService = notifService;
  }

  @Override
  public void updateFilter(String roomName, Participant participant, String filterId,
      String state) {
    Integer newState = -1;

    if (state != null) {
      newState = Integer.parseInt(state);
    }

    String url = markerUrls.get(newState);

    JsonObject notificationParams = new JsonObject();
    notificationParams.addProperty("MarkerFilterState", newState);

    notifService.sendNotification(participant.getId(), ProtocolElements.CUSTOM_NOTIFICATION,
        notificationParams);

    if (url == null) {
      log.info("Removing {} filter from participant {}", filterId, participant.getId());
      participant.disableFilterelement(filterId, false);
      return;
    }

    ArMarkerdetector newFilter;
    Filter filter = participant.getFilterElement(filterId);

    if (filter == null) {
      newFilter = new ArMarkerdetector.Builder(participant.getPipeline()).build();
      log.info("New {} filter for participant {}", filterId, participant.getId());
      participant.addFilterElement(filterId, newFilter);
    } else {
      log.info("Reusing {} filter in participant {}", filterId, participant.getId());
      newFilter = (ArMarkerdetector) filter;
    }

    participant.enableFilterelement(filterId);

    newFilter.setOverlayImage(url, new Continuation<Void>() {
      @Override
      public void onSuccess(Void result) throws Exception {

      }

      @Override
      public void onError(Throwable cause) throws Exception {

      }
    });
  }

  @Override
  public String getNextFilterState(String filterId, String oldState) {
    Integer currentUrlIndex;

    if (oldState == null) {
      currentUrlIndex = -1;
    } else {
      currentUrlIndex = Integer.parseInt(oldState);
    }

    Integer nextIndex = -1; // disable filter

    if (currentUrlIndex < markerUrls.firstKey()) {
      nextIndex = markerUrls.firstKey(); // enable filter using first URL
    } else if (currentUrlIndex < markerUrls.lastKey()) {
      nextIndex = markerUrls.tailMap(currentUrlIndex + 1).firstKey();
    }

    return nextIndex.toString();
  }

  public void setMarkerUrls(SortedMap<Integer, String> markerUrls) {
    this.markerUrls = markerUrls;
  }
}

@@ -1435,198 +1435,8 @@
 );%0A%0A
-    if (url == null) %7B%0A      log.info(%22Removing %7B%7D filter from participant %7B%7D%22, filterId, participant.getId());%0A      participant.disableFilterelement(filterId, false);%0A      return;%0A    %7D%0A%0A
     
@@ -1932,52 +1932,561 @@
     
-participant.enableFilterelement(filterId);%0A%0A
+if (url != null) %7B%0A      newFilter.setOverlayImage(url, new Continuation%3CVoid%3E() %7B%0A        @Override%0A        public void onSuccess(Void result) throws Exception %7B%0A%0A        %7D%0A%0A        @Override%0A        public void onError(Throwable cause) throws Exception %7B%0A%0A        %7D%0A      %7D);%0A      newFilter.setOverlayScale(1.0F, new Continuation%3CVoid%3E() %7B%0A        @Override%0A        public void onSuccess(Void result) throws Exception %7B%0A%0A        %7D%0A%0A        @Override%0A        public void onError(Throwable cause) throws Exception %7B%0A%0A        %7D%0A      %7D);%0A    %7D else %7B%0A  
     
@@ -2501,33 +2501,37 @@
 r.setOverlay
-Image(url
+Scale(0.0001F
 , new Contin
@@ -2549,24 +2549,26 @@
 %3E() %7B%0A      
+  
 @Override%0A  
@@ -2563,32 +2563,34 @@
 @Override%0A      
+ 
+ 
 public void onSu
@@ -2630,27 +2630,31 @@
 on %7B%0A%0A      
+  
 %7D%0A%0A
+  
       @Overr
@@ -2659,24 +2659,26 @@
 rride%0A      
+  
 public void 
@@ -2718,24 +2718,26 @@
 xception %7B%0A%0A
+  
       %7D%0A    
@@ -2736,20 +2736,28 @@
   %7D%0A    
+  
 %7D);%0A
+    %7D%0A
   %7D%0A%0A  @

Improve performance of filter change