/*
 * Copyright 2016-present The Material Motion Authors. All Rights Reserved.
 *
 * 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.google.android.reactive.motion.sources;

import com.google.android.indefinite.observable.IndefiniteObservable.Subscription;
import com.google.android.material.motion.physics.Integrator;
import com.google.android.material.motion.physics.forces.Spring;
import com.google.android.material.motion.physics.integrators.Rk4Integrator;
import com.google.android.material.motion.physics.math.Vector;
import com.google.android.reactive.motion.MotionObservable;
import com.google.android.reactive.motion.MotionObservable.MotionObserver;
import com.google.android.reactive.motion.MotionObservable.SimpleMotionObserver;
import com.google.android.reactive.motion.interactions.MaterialSpring;

/**
 * A source for physics springs.
 */
public final class PhysicsSpringSource<T> extends SpringSource<T> {

  public static final System SYSTEM = new System() {
    @Override
    public <T> SpringSource<T> create(MaterialSpring<?, T> spring) {
      return new PhysicsSpringSource<>(spring);
    }
  };

  private final MaterialSpring<?, T> spring;

  private Integrator integrator;
  private Spring springForce;

  private Subscription destinationSubscription;
  private Subscription frictionSubscription;
  private Subscription tensionSubscription;

  public PhysicsSpringSource(MaterialSpring<?, T> spring) {
    super(spring);
    this.spring = spring;
  }

  @Override
  protected void onConnect(final MotionObserver<T> observer) {
    integrator = new Rk4Integrator();
    springForce = new Spring();
    integrator.addForce(springForce);

    integrator.addListener(new Integrator.SimpleListener() {
      @Override
      public void onStart() {
        observer.state(MotionObservable.ACTIVE);
      }

      @Override
      public void onUpdate(Vector x, Vector v) {
        T value = spring.vectorizer.compose(x.getValues());
        observer.next(value);
      }

      @Override
      public void onStop() {
        observer.state(MotionObservable.AT_REST);
      }
    });
  }

  @Override
  protected void onEnable(MotionObserver<T> observer) {
    tensionSubscription = spring.tension.subscribe(new SimpleMotionObserver<Float>() {
      @Override
      public void next(Float value) {
        springForce.k = Spring.tensionFromOrigamiValue(value);
        integrator.start();
      }
    });
    frictionSubscription = spring.friction.subscribe(new SimpleMotionObserver<Float>() {
      @Override
      public void next(Float value) {
        springForce.b = Spring.frictionFromOrigamiValue(value);
        integrator.start();
      }
    });

    final int count = spring.vectorizer.getVectorLength();

    float[] initialValues = new float[count];
    spring.vectorizer.vectorize(spring.initialValue.read(), initialValues);

    float[] initialVelocities = new float[count];
    spring.vectorizer.vectorize(spring.initialVelocity.read(), initialVelocities);

    for (int i = 0; i < count; i++) {
      integrator.setState(new Vector(initialValues), new Vector(initialVelocities));
    }

    final float[] endValues = new float[count];
    destinationSubscription = spring.destination.subscribe(new SimpleMotionObserver<T>() {
      @Override
      public void next(T value) {
        spring.vectorizer.vectorize(value, endValues);

        springForce.setAnchorPoint(new Vector(endValues));
        integrator.start();
      }
    });

    integrator.start();
  }

  @Override
  protected void onDisable(MotionObserver<T> observer) {
    integrator.stop();

    tensionSubscription.unsubscribe();
    frictionSubscription.unsubscribe();
    destinationSubscription.unsubscribe();
  }
}

@@ -825,32 +825,109 @@
 ics.Integrator;%0A
+import com.google.android.material.motion.physics.Integrator.SimpleListener;%0A
 import com.googl
@@ -1403,16 +1403,68 @@
 pring;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A * A
@@ -1817,16 +1817,22 @@
 private 
+final 
 Integrat
@@ -1856,16 +1856,22 @@
 private 
+final 
 Spring s
@@ -1881,16 +1881,99 @@
 ngForce;
+%0A  private final List%3CIntegrator.Listener%3E integratorListeners = new ArrayList%3C%3E();
 %0A%0A  priv
@@ -2213,88 +2213,8 @@
 ng;%0A
-  %7D%0A%0A  @Override%0A  protected void onConnect(final MotionObserver%3CT%3E observer) %7B%0A
     
@@ -2317,17 +2317,16 @@
 Force);%0A
-%0A
     inte
@@ -2348,27 +2348,749 @@
 ner(new 
-Integrator.
+SimpleListener() %7B%0A      @Override%0A      public void onStart() %7B%0A        for (int i = 0, count = integratorListeners.size(); i %3C count; i++) %7B%0A          integratorListeners.get(i).onStart();%0A        %7D%0A      %7D%0A%0A      @Override%0A      public void onUpdate(Vector x, Vector v) %7B%0A        for (int i = 0, count = integratorListeners.size(); i %3C count; i++) %7B%0A          integratorListeners.get(i).onUpdate(x, v);%0A        %7D%0A      %7D%0A%0A      @Override%0A      public void onStop() %7B%0A        for (int i = 0, count = integratorListeners.size(); i %3C count; i++) %7B%0A          integratorListeners.get(i).onStop();%0A        %7D%0A      %7D%0A    %7D);%0A  %7D%0A%0A  @Override%0A  protected void onConnect(final MotionObserver%3CT%3E observer) %7B%0A    integratorListeners.add(new 
 SimpleLi
@@ -3092,32 +3092,33 @@
 pleListener() %7B%0A
+%0A
       @Override%0A

Remove per-target state from PhysicsSpringSource.