/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * 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.android.deskclock;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.util.Property;
import android.view.View;
import android.view.animation.Interpolator;
import android.widget.ImageView;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class AnimatorUtils {

    public static final long ANIM_DURATION_SHORT = 266L;  // 8/30 frames long

    public static final Interpolator DECELERATE_ACCELERATE_INTERPOLATOR = new Interpolator() {
        @Override
        public float getInterpolation(float x) {
            return 0.5f + 4.0f * (x - 0.5f) * (x - 0.5f) * (x - 0.5f);
        }
    };

    public static final Property<View, Integer> BACKGROUND_ALPHA =
            new Property<View, Integer>(Integer.class, "background.alpha") {
        @Override
        public Integer get(View view) {
            return view.getBackground().getAlpha();
        }

        @Override
        public void set(View view, Integer value) {
            view.getBackground().setAlpha(value);
        }
    };

    public static final Property<ImageView, Integer> DRAWABLE_ALPHA =
            new Property<ImageView, Integer>(Integer.class, "drawable.alpha") {
        @Override
        public Integer get(ImageView view) {
            return view.getDrawable().getAlpha();
        }

        @Override
        public void set(ImageView view, Integer value) {
            view.getDrawable().setAlpha(value);
        }
    };

    public static final Property<ImageView, Integer> DRAWABLE_TINT =
            new Property<ImageView, Integer>(Integer.class, "drawable.tint") {
        @Override
        public Integer get(ImageView view) {
            return null;
        }

        @Override
        public void set(ImageView view, Integer value) {
            view.getDrawable().setTint(value);
        }
    };

    public static final TypeEvaluator ARGB_EVALUATOR = new ArgbEvaluator();

    public static void start(ValueAnimator... animators) {
        for (ValueAnimator animator : animators) {
            final float fraction = animator.getAnimatedFraction();
            if (fraction < 1.0f) {
                animator.start();
            }
        }
    }

    public static void reverse(ValueAnimator... animators) {
        for (ValueAnimator animator : animators) {
            final float fraction = animator.getAnimatedFraction();
            if (fraction > 0.0f) {
                animator.reverse();
            }
        }
    }

    public static void cancel(ValueAnimator... animators) {
        for (ValueAnimator animator : animators) {
            animator.cancel();
        }
    }

    public static ValueAnimator getScaleAnimator(View view, float... values) {
        return ObjectAnimator.ofPropertyValuesHolder(view,
                PropertyValuesHolder.ofFloat(View.SCALE_X, values),
                PropertyValuesHolder.ofFloat(View.SCALE_Y, values));
    }
}
