/*
 * Copyright (C) 2015 Pedro Vicente Gomez Sanchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.pedrovgs.lynx.presenter;

import com.github.pedrovgs.lynx.LynxConfig;
import com.github.pedrovgs.lynx.model.Lynx;
import com.github.pedrovgs.lynx.model.Trace;
import java.util.List;

/**
 * Presenter created to decouple Lynx library view implementations from Lynx model. This presenter
 * responsibility is related to all the presentation logic to Lynx UI implementations. Lynx UI
 * implementations have to implement LynxPresenter.View interface.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class LynxPresenter implements Lynx.Listener {

  private static final int MIN_VISIBLE_POSITION_TO_ENABLE_AUTO_SCROLL = 3;

  private final Lynx lynx;
  private final View view;
  private final TraceBuffer traceBuffer;

  public LynxPresenter(Lynx lynx, View view, int maxNumberOfTracesToShow) {
    validateNumberOfTracesConfiguration(maxNumberOfTracesToShow);

    this.lynx = lynx;
    this.view = view;
    this.traceBuffer = new TraceBuffer(maxNumberOfTracesToShow);
  }

  public void setLynxConfig(LynxConfig lynxConfig) {
    validateLynxConfig(lynxConfig);

    updateBufferConfig(lynxConfig);
    updateLynxConfig(lynxConfig);
  }

  public void resume() {
    lynx.registerListener(this);
    lynx.startReading();
  }

  public void pause() {
    lynx.unregisterListener(this);
    lynx.stopReading();
  }

  @Override public void onNewTraces(List<Trace> traces) {
    int tracesRemoved = updateTraceBuffer(traces);
    List<Trace> tracesToNotify = getCurrentTraces();
    view.showTraces(tracesToNotify, tracesRemoved);
  }

  public void onScrollToPosition(int lastVisiblePosition) {
    if (shouldDisableAutoScroll(lastVisiblePosition)) {
      view.disableAutoScroll();
    } else {
      view.enableAutoScroll();
    }
  }

  private void updateBufferConfig(LynxConfig lynxConfig) {
    traceBuffer.setBufferSize(lynxConfig.getMaxNumberOfTracesToShow());
    refreshTraces();
  }

  private void refreshTraces() {
    onNewTraces(traceBuffer.getTraces());
  }

  private void updateLynxConfig(LynxConfig lynxConfig) {
    lynx.setConfig(lynxConfig);
  }

  private int updateTraceBuffer(List<Trace> traces) {
    return traceBuffer.add(traces);
  }

  private List<Trace> getCurrentTraces() {
    return traceBuffer.getTraces();
  }

  private void validateNumberOfTracesConfiguration(long maxNumberOfTracesToShow) {
    if (maxNumberOfTracesToShow <= 0) {
      throw new IllegalArgumentException(
          "You can't pass a zero or negative number of traces to show.");
    }
  }

  private void validateLynxConfig(LynxConfig lynxConfig) {
    if (lynxConfig == null) {
      throw new IllegalArgumentException(
          "You can't use a null instance of LynxConfig as configuration.");
    }
  }

  private boolean shouldDisableAutoScroll(int lastVisiblePosition) {
    int positionOffset = traceBuffer.getCurrentNumberOfTraces() - lastVisiblePosition;
    return positionOffset >= MIN_VISIBLE_POSITION_TO_ENABLE_AUTO_SCROLL;
  }

  public interface View {

    void showTraces(List<Trace> traces, int removedTraces);

    void disableAutoScroll();

    void enableAutoScroll();
  }
}

@@ -1489,17 +1489,16 @@
 oShow);%0A
-%0A
     this
@@ -1692,17 +1692,16 @@
 onfig);%0A
-%0A
     upda
@@ -1892,55 +1892,55 @@
 ynx.
-unregisterListener(this);%0A    lynx.stopReading(
+stopReading();%0A    lynx.unregisterListener(this
 );%0A 
@@ -2357,32 +2357,366 @@
 l();%0A    %7D%0A  %7D%0A%0A
+  public void onFilterUpdated(String filter) %7B%0A    LynxConfig lynxConfig = lynx.getConfig();%0A    lynxConfig.withFilter(filter);%0A    lynx.setConfig(lynxConfig);%0A    clearView();%0A    restartLynx();%0A  %7D%0A%0A  private void clearView() %7B%0A    traceBuffer.clear();%0A    view.clear();%0A  %7D%0A%0A  private void restartLynx() %7B%0A    lynx.restart();%0A  %7D%0A%0A
   private void u
@@ -4058,15 +4058,34 @@
 croll();
+%0A%0A    void clear();
 %0A  %7D%0A%7D%0A

Add implementation for onFilterUpdated method in LynxPresenter