package org.robolectric.shadows;

import static org.robolectric.shadow.api.Shadow.directlyOn;

import android.os.Looper;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.LayoutAnimationController;
import java.io.PrintStream;
import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;
import org.robolectric.annotation.RealObject;
import org.robolectric.shadow.api.Shadow;
import org.robolectric.util.ReflectionHelpers.ClassParameter;

@SuppressWarnings({"UnusedDeclaration"})
@Implements(ViewGroup.class)
public class ShadowViewGroup extends ShadowView {
  @RealObject protected ViewGroup realViewGroup;

  private AnimationListener animListener;
  private LayoutAnimationController layoutAnim;
  private boolean disallowInterceptTouchEvent = false;
  private MotionEvent interceptedTouchEvent;

  @Implementation
  public void addView(final View child, final int index, final ViewGroup.LayoutParams params) {
    ShadowLooper shadowLooper = Shadow.extract(Looper.getMainLooper());
    shadowLooper.runPaused(() ->
        directlyOn(realViewGroup, ViewGroup.class, "addView",
            ClassParameter.from(View.class, child),
            ClassParameter.from(int.class, index),
            ClassParameter.from(ViewGroup.LayoutParams.class, params)));
  }

  /**
   * Returns a string representation of this {@code ViewGroup} by concatenating all of the
   * strings contained in all of the descendants of this {@code ViewGroup}.
   */
  @Override
  public String innerText() {
    StringBuilder innerText = new StringBuilder();
    String delimiter = "";

    for (int i = 0; i < realViewGroup.getChildCount(); i++) {
      View child = realViewGroup.getChildAt(i);
      ShadowView shadowView = Shadow.extract(child);
      String childText = shadowView.innerText();
      if (childText.length() > 0) {
        innerText.append(delimiter);
        delimiter = " ";
      }
      innerText.append(childText);
    }
    return innerText.toString();
  }

  /**
   * Dumps the state of this {@code ViewGroup} to {@code System.out}.
   */
  @Override
  public void dump(PrintStream out, int indent) {
    dumpFirstPart(out, indent);
    if (realViewGroup.getChildCount() > 0) {
      out.println(">");

      for (int i = 0; i < realViewGroup.getChildCount(); i++) {
        View child = realViewGroup.getChildAt(i);
        ShadowView shadowChild = Shadow.extract(child);
        shadowChild.dump(out, indent + 2);
      }

      dumpIndent(out, indent);
      out.println("</" + realView.getClass().getSimpleName() + ">");
    } else {
      out.println("/>");
    }
  }

  @Implementation
  public void setLayoutAnimationListener(AnimationListener listener) {
    animListener = listener;
  }

  @Implementation
  public AnimationListener getLayoutAnimationListener() {
    return animListener;
  }

  @Implementation
  public void setLayoutAnimation(LayoutAnimationController layoutAnim) {
    this.layoutAnim = layoutAnim;
  }

  @Implementation
  public LayoutAnimationController getLayoutAnimation() {
    return layoutAnim;
  }

  @Implementation
  public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
    disallowInterceptTouchEvent = disallowIntercept;
  }

  public boolean getDisallowInterceptTouchEvent() {
    return disallowInterceptTouchEvent;
  }

  protected void removedChild(View child) {
    if (isAttachedToWindow()) {
      ShadowView shadowView = Shadow.extract(child);
      shadowView.callOnDetachedFromWindow();
    }
  }

  public MotionEvent getInterceptedTouchEvent() {
    return interceptedTouchEvent;
  }

  @Implementation
  public boolean onInterceptTouchEvent(MotionEvent ev) {
    interceptedTouchEvent = ev;
    return false;
  }
}

@@ -208,124 +208,8 @@
 up;%0A
-import android.view.animation.Animation.AnimationListener;%0Aimport android.view.animation.LayoutAnimationController;%0A
 impo
@@ -653,98 +653,8 @@
 p;%0A%0A
-  private AnimationListener animListener;%0A  private LayoutAnimationController layoutAnim;%0A
   pr
@@ -2530,471 +2530,8 @@
  %7D%0A%0A
-  @Implementation%0A  public void setLayoutAnimationListener(AnimationListener listener) %7B%0A    animListener = listener;%0A  %7D%0A%0A  @Implementation%0A  public AnimationListener getLayoutAnimationListener() %7B%0A    return animListener;%0A  %7D%0A%0A  @Implementation%0A  public void setLayoutAnimation(LayoutAnimationController layoutAnim) %7B%0A    this.layoutAnim = layoutAnim;%0A  %7D%0A%0A  @Implementation%0A  public LayoutAnimationController getLayoutAnimation() %7B%0A    return layoutAnim;%0A  %7D%0A%0A
   @I

Remove @Implementation methods from ShadowViewGroup that are not necessary as we can just call the real implementation instead.