package com.mercadopago.android.px.internal.features.express.animations;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import com.mercadopago.android.px.R;

import static com.mercadopago.android.px.internal.util.ViewUtils.cancelAnimation;
import static com.mercadopago.android.px.internal.util.ViewUtils.hasEndedAnim;

public class FadeAnimator {

    private final int normalDuration;
    private final int fastDuration;
    private final int fastestDuration;

    public FadeAnimator(@NonNull final Context context) {
        normalDuration = context.getResources().getInteger(R.integer.px_long_animation_time);
        fastDuration = context.getResources().getInteger(R.integer.px_short_animation_time);
        fastestDuration = context.getResources().getInteger(R.integer.px_shortest_animation_time);
    }

    private AlphaAnimation createFade(final int from, final int to) {
        final AlphaAnimation anim = new AlphaAnimation(from, to);
        anim.setInterpolator(new AccelerateInterpolator());
        anim.setFillAfter(true);
        return anim;
    }

    public void fadeIn(@NonNull final View viewToAnimate) {
        if (hasEndedAnim(viewToAnimate)) {
            final AlphaAnimation fade = createFade(0, 1);
            fade.setDuration(normalDuration);
            viewToAnimate.startAnimation(fade);
        } else {
            cancelAnimation(viewToAnimate);
        }
        viewToAnimate.setVisibility(View.VISIBLE);
    }

    public void fadeInFastest(@NonNull final View viewToAnimate) {
        if (hasEndedAnim(viewToAnimate)) {
            final AlphaAnimation fade = createFade(0, 1);
            fade.setDuration(fastestDuration);
            viewToAnimate.startAnimation(fade);
        } else {
            cancelAnimation(viewToAnimate);
        }

        viewToAnimate.setVisibility(View.VISIBLE);
    }

    public void fadeOut(@NonNull final View viewToAnimate) {
        if (hasEndedAnim(viewToAnimate)) {
            viewToAnimate.clearAnimation();
            viewToAnimate.setVisibility(View.VISIBLE);
            final AlphaAnimation fade = createFade(1, 0);
            fade.setAnimationListener(new HideViewOnAnimationListener(viewToAnimate));
            fade.setDuration(normalDuration);
            viewToAnimate.startAnimation(fade);
        } else {
            cancelAnimation(viewToAnimate);
            viewToAnimate.setVisibility(View.GONE);
        }
    }

    public void fadeOutFast(@NonNull final View viewToAnimate) {
        if (hasEndedAnim(viewToAnimate)) {
            viewToAnimate.setVisibility(View.VISIBLE);
            final AlphaAnimation fade = createFade(1, 0);
            fade.setAnimationListener(new HideViewOnAnimationListener(viewToAnimate));
            fade.setDuration(fastDuration);
            viewToAnimate.startAnimation(fade);
        } else {
            cancelAnimation(viewToAnimate);
            viewToAnimate.setVisibility(View.GONE);
        }
    }
}

@@ -1224,33 +1224,214 @@
 %7D%0A%0A    p
-ublic void fadeIn
+rivate boolean shouldFadeIn(@NonNull final View viewToAnimate) %7B%0A        return hasEndedAnim(viewToAnimate) && viewToAnimate.getVisibility() != View.VISIBLE;%0A    %7D%0A%0A    private boolean shouldFadeOut
 (@NonNul
@@ -1460,36 +1460,39 @@
 mate) %7B%0A        
-if (
+return 
 hasEndedAnim(vie
@@ -1494,32 +1494,186 @@
 m(viewToAnimate)
+ && viewToAnimate.getVisibility() != View.GONE;%0A    %7D%0A%0A    public void fadeIn(@NonNull final View viewToAnimate) %7B%0A        if (shouldFadeIn(viewToAnimate)
 ) %7B%0A            
@@ -2016,36 +2016,36 @@
         if (
-hasEndedAnim
+shouldFadeIn
 (viewToAnima
@@ -2403,36 +2403,37 @@
         if (
-hasEndedAnim
+shouldFadeOut
 (viewToAnima
@@ -2984,28 +2984,29 @@
     if (
-hasEndedAnim
+shouldFadeOut
 (viewToA

split payment animations (#1594)