/*
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * 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.facebook.samples.litho.animations.animationcookbook;

import android.animation.Animator;
import android.animation.ValueAnimator;
import com.facebook.litho.Column;
import com.facebook.litho.Component;
import com.facebook.litho.ComponentContext;
import com.facebook.litho.DynamicValue;
import com.facebook.litho.StateValue;
import com.facebook.litho.VisibleEvent;
import com.facebook.litho.annotations.LayoutSpec;
import com.facebook.litho.annotations.OnCreateInitialState;
import com.facebook.litho.annotations.OnCreateLayout;
import com.facebook.litho.annotations.OnEvent;
import com.facebook.litho.annotations.State;
import com.facebook.litho.widget.Text;
import com.facebook.yoga.YogaAlign;
import com.facebook.yoga.YogaEdge;
import com.facebook.yoga.YogaJustify;

@LayoutSpec
class ContinuousExampleComponentSpec {

  private static final long ANIMATION_DURATION_MS = 3000;

  private static Animator createRotationAnimator(final DynamicValue<Float> rotation) {
    final ValueAnimator rotationAnimator = ValueAnimator.ofFloat(0f, 365f);
    rotationAnimator.setDuration(ANIMATION_DURATION_MS);
    rotationAnimator.setInterpolator(null);
    rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);
    rotationAnimator.addUpdateListener(
        new ValueAnimator.AnimatorUpdateListener() {
          @Override
          public void onAnimationUpdate(ValueAnimator animation) {
            rotation.set((Float) animation.getAnimatedValue());
          }
        });
    return rotationAnimator;
  }

  @OnCreateInitialState
  static void createInitialState(
      ComponentContext c, StateValue<Animator> animator, StateValue<DynamicValue<Float>> rotation) {
    rotation.set(new DynamicValue<>(0f));
    animator.set(createRotationAnimator(rotation.get()));
  }

  @OnCreateLayout
  static Component onCreateLayout(ComponentContext c, @State DynamicValue<Float> rotation) {
    return Column.create(c)
        .alignItems(YogaAlign.CENTER)
        .justifyContent(YogaJustify.CENTER)
        .paddingDip(YogaEdge.ALL, 50)
        .child(Text.create(c).text("\u2B50").textSizeSp(50).rotation(rotation))
        .visibleHandler(ContinuousExampleComponent.onVisible(c))
        .build();
  }

  @OnEvent(VisibleEvent.class)
  static void onVisible(ComponentContext c, @State Animator animator) {
    animator.start();
  }
}

@@ -737,20 +737,60 @@
 imation.
-Valu
+FloatEvaluator;%0Aimport android.animation.Tim
 eAnimato
@@ -1638,20 +1638,19 @@
   final 
-Valu
+Tim
 eAnimato
@@ -1651,25 +1651,17 @@
 nimator 
-rotationA
+a
 nimator 
@@ -1662,20 +1662,23 @@
 mator = 
-Valu
+new Tim
 eAnimato
@@ -1682,219 +1682,95 @@
 ator
-.ofFloat(0f, 365f);%0A    rotationAnimator.setDuration(ANIMATION_DURATION_MS);%0A    rotationAnimator.setInterpolator(null);%0A    rotationAnimator.setRepeatCount(ValueAnimator.INFINITE);%0A    rotationAnimator.addUpdat
+();%0A    final FloatEvaluator floatEvaluator = new FloatEvaluator();%0A    animator.setTim
 eLis
@@ -1788,20 +1788,19 @@
     new 
-Valu
+Tim
 eAnimato
@@ -1805,21 +1805,11 @@
 tor.
-AnimatorUpdat
+Tim
 eLis
@@ -1866,28 +1866,22 @@
 d on
-Animation
+Time
 Update(
-Valu
+Tim
 eAni
@@ -1899,73 +1899,202 @@
 tion
-) %7B%0A            rotation.set((Float) anim
+, long totalTime, long deltaTime) %7B%0A            float fraction =%0A                Float.valueOf(totalTime %25 ANIMATION_DURATION_MS) / ANIMATION_DURATION_MS;%0A            rot
 ation.
-getAnimatedValue()
+set(fraction * 360
 );%0A 
@@ -2131,17 +2131,9 @@
 urn 
-rotationA
+a
 nima

Fix glitch in rotation example