package com.akexorcist.sleepingforless.util;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;

import com.akexorcist.sleepingforless.R;

/**
 * Created by Akexorcist on 3/10/2016 AD.
 */
public class AnimationUtility {
    private static AnimationUtility utility;

    public static AnimationUtility getInstance() {
        if (utility == null) {
            utility = new AnimationUtility();
        }
        return utility;
    }

    public static final int DEFAULT_DURATION = 300;
    public static final int DEFAULT_START_DELAY = 0;

    public void fadeOut(final View view, int duration, long startDelay) {
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(Contextor.getContext(), R.animator.animator_default_fade_out);
        objectAnimator.setTarget(view);
        objectAnimator.setDuration(duration);
        objectAnimator.setStartDelay(startDelay);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        objectAnimator.start();
    }

    public void fadeOut(View view) {
        fadeOut(view, DEFAULT_DURATION, DEFAULT_START_DELAY);
    }

    public void fadeOut(View view, int duration) {
        fadeOut(view, duration, DEFAULT_START_DELAY);
    }


    public void fadeOut(View view, long startDelay) {
        fadeOut(view, DEFAULT_DURATION, startDelay);
    }

    public void fadeIn(final View view, int duration, long startDelay) {
        view.setVisibility(View.VISIBLE);
        view.setEnabled(false);
        ObjectAnimator objectAnimator = (ObjectAnimator) AnimatorInflater.loadAnimator(Contextor.getContext(), R.animator.animator_default_fade_in);
        objectAnimator.setTarget(view);
        objectAnimator.setDuration(duration);
        objectAnimator.setStartDelay(startDelay);
        objectAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                view.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {
            }

            @Override
            public void onAnimationRepeat(Animator animation) {
            }
        });
        objectAnimator.start();
    }

    public void fadeIn(View view) {
        fadeIn(view, DEFAULT_DURATION, DEFAULT_START_DELAY);
    }

    public void fadeIn(View view, int duration) {
        fadeIn(view, duration, DEFAULT_START_DELAY);
    }

    public void fadeIn(View view, long startDelay) {
        fadeIn(view, DEFAULT_DURATION, startDelay);
    }

    public void scaleUp(final View view, int duration, long startDelay) {
        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(Contextor.getContext(), R.animator.animator_default_scale_up);
        animatorSet.setTarget(view);
        animatorSet.setDuration(duration);
        animatorSet.setStartDelay(startDelay);
        animatorSet.start();
    }

    public void scaleUp(View view, long startDelay) {
        scaleUp(view, DEFAULT_DURATION, startDelay);
    }

    public void scaleUp(View view, int duration) {
        scaleUp(view, duration, DEFAULT_START_DELAY);
    }

    public void scaleUp(View view) {
        scaleUp(view, DEFAULT_DURATION, DEFAULT_START_DELAY);
    }

    public void scaleBack(final View view, int duration, long startDelay) {
        AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(Contextor.getContext(), R.animator.animator_default_scale_back);
        animatorSet.setTarget(view);
        animatorSet.setDuration(duration);
        animatorSet.setStartDelay(startDelay);
        animatorSet.start();
    }

    public void scaleBack(View view, long startDelay) {
        scaleBack(view, DEFAULT_DURATION, startDelay);
    }

    public void scaleBack(View view, int duration) {
        scaleBack(view, duration, DEFAULT_START_DELAY);
    }

    public void scaleBack(View view) {
        scaleBack(view, DEFAULT_DURATION, DEFAULT_START_DELAY);
    }
}

@@ -668,16 +668,672 @@
 Y = 0;%0A%0A
+    public void fadeOut(View view) %7B%0A        fadeOut(view, DEFAULT_DURATION, DEFAULT_START_DELAY, null);%0A    %7D%0A%0A    public void fadeOut(View view, AnimationFinishCallback callback) %7B%0A        fadeOut(view, DEFAULT_DURATION, DEFAULT_START_DELAY, callback);%0A    %7D%0A%0A    public void fadeOut(View view, int duration) %7B%0A        fadeOut(view, duration, DEFAULT_START_DELAY, null);%0A    %7D%0A%0A    public void fadeOut(View view, int duration, AnimationFinishCallback callback) %7B%0A        fadeOut(view, duration, DEFAULT_START_DELAY, callback);%0A    %7D%0A%0A    public void fadeOut(View view, long startDelay) %7B%0A        fadeOut(view, DEFAULT_DURATION, startDelay, null);%0A    %7D%0A%0A
     publ
@@ -1386,32 +1386,72 @@
  long startDelay
+, final AnimationFinishCallback callback
 ) %7B%0A        Obje
@@ -2019,24 +2019,134 @@
 View.GONE);%0A
+                if (callback != null) %7B%0A                    callback.onAnimationFinished();%0A                %7D%0A
             
@@ -2412,35 +2412,34 @@
 public void fade
-Out
+In
 (View view) %7B%0A  
@@ -2440,35 +2440,34 @@
 ) %7B%0A        fade
-Out
+In
 (view, DEFAULT_D
@@ -2516,35 +2516,34 @@
 public void fade
-Out
+In
 (View view, int 
@@ -2558,35 +2558,34 @@
 ) %7B%0A        fade
-Out
+In
 (view, duration,
@@ -2614,17 +2614,16 @@
 %0A    %7D%0A%0A
-%0A
     publ
@@ -2626,35 +2626,34 @@
 public void fade
-Out
+In
 (View view, long
@@ -2671,35 +2671,34 @@
 ) %7B%0A        fade
-Out
+In
 (view, DEFAULT_D
@@ -3720,334 +3720,8 @@
  %7D%0A%0A
-    public void fadeIn(View view) %7B%0A        fadeIn(view, DEFAULT_DURATION, DEFAULT_START_DELAY);%0A    %7D%0A%0A    public void fadeIn(View view, int duration) %7B%0A        fadeIn(view, duration, DEFAULT_START_DELAY);%0A    %7D%0A%0A    public void fadeIn(View view, long startDelay) %7B%0A        fadeIn(view, DEFAULT_DURATION, startDelay);%0A    %7D%0A%0A
     
@@ -5150,11 +5150,101 @@
 );%0A    %7D
+%0A%0A    public interface AnimationFinishCallback %7B%0A        void onAnimationFinished();%0A    %7D
 %0A%7D%0A

Add callback