/*
 * 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 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) {
    updateTraceBuffer(traces);
    List<Trace> tracesToNotify = getCurrentTraces();
    view.showTraces(tracesToNotify);
  }

  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 void updateTraceBuffer(List<Trace> traces) {
    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.");
    }
  }

  public interface View {

    void showTraces(List<Trace> traces);
  }
}

@@ -1175,16 +1175,92 @@
 ener %7B%0A%0A
+  private static final int MIN_VISIBLE_POSITION_TO_ENABLE_AUTO_SCROLL = 3;%0A%0A
   privat
@@ -2002,24 +2002,44 @@
 races) %7B%0A   
+ int tracesRemoved =
  updateTrace
@@ -2141,18 +2141,236 @@
 ToNotify
-);
+, tracesRemoved);%0A  %7D%0A%0A  public void onScrollToPosition(int lastVisiblePosition) %7B%0A    if (shouldDisableAutoScroll(lastVisiblePosition)) %7B%0A      view.disableAutoScroll();%0A    %7D else %7B%0A      view.enableAutoScroll();%0A    %7D
 %0A  %7D%0A%0A  
@@ -2704,28 +2704,27 @@
 %0A%0A  private 
-void
+int
  updateTrace
@@ -2748,32 +2748,39 @@
 %3E traces) %7B%0A    
+return 
 traceBuffer.add(
@@ -3352,70 +3352,384 @@
 %0A  p
-ublic interface View %7B%0A%0A    void showTraces(List%3CTrace%3E traces
+rivate boolean shouldDisableAutoScroll(int lastVisiblePosition) %7B%0A    int positionOffset = traceBuffer.getCurrentNumberOfTraces() - lastVisiblePosition;%0A    return positionOffset %3E= MIN_VISIBLE_POSITION_TO_ENABLE_AUTO_SCROLL;%0A  %7D%0A%0A  public interface View %7B%0A%0A    void showTraces(List%3CTrace%3E traces, int removedTraces);%0A%0A    void disableAutoScroll();%0A%0A    void enableAutoScroll(
 );%0A 

Implement presenter onScroll method to change view scroll configuration based on the last visible element in the list