/*
 * Copyright (C) 2016 Andrey Kulikov (andkulikov@gmail.com)
 *
 * 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.transitionseverywhere.extra;

import android.animation.Animator;
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.PointF;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import com.transitionseverywhere.Transition;
import com.transitionseverywhere.TransitionValues;
import com.transitionseverywhere.utils.AnimatorUtils;
import com.transitionseverywhere.utils.PointFProperty;

/**
 * This transition tracks changes to the translationX and translationY of
 * target views in the start and end scenes and animates change.
 * <p/>
 * Created by Andrey Kulikov on 13/03/16.
 */
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public class TranslationTransition extends Transition {

    private static final String TRANSLATION_X = "TranslationTransition:translationX";
    private static final String TRANSLATION_Y = "TranslationTransition:translationY";

    private static final PointFProperty<View> TRANSLATION_PROPERTY;

    static {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            TRANSLATION_PROPERTY = new PointFProperty<View>() {

                @Override
                public void set(View object, PointF value) {
                    object.setTranslationX(value.x);
                    object.setTranslationY(value.y);
                }

                @Override
                public PointF get(View object) {
                    return new PointF(object.getTranslationX(), object.getTranslationY());
                }
            };
        } else {
            TRANSLATION_PROPERTY = null;
        }
    }

    public TranslationTransition() {
    }

    public TranslationTransition(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void captureValues(TransitionValues transitionValues) {
        if (transitionValues.view != null) {
            transitionValues.values.put(TRANSLATION_X, transitionValues.view.getTranslationX());
            transitionValues.values.put(TRANSLATION_Y, transitionValues.view.getTranslationY());
        }
    }

    @Override
    public void captureStartValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override
    public void captureEndValues(TransitionValues transitionValues) {
        captureValues(transitionValues);
    }

    @Override
    public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues,
                                   TransitionValues endValues) {
        if (startValues != null && endValues != null && TRANSLATION_PROPERTY != null) {
            return AnimatorUtils.ofPointF(endValues.view, TRANSLATION_PROPERTY, getPathMotion(),
                    (float) startValues.values.get(TRANSLATION_X),
                    (float) startValues.values.get(TRANSLATION_Y),
                    (float) endValues.values.get(TRANSLATION_X),
                    (float) endValues.values.get(TRANSLATION_Y));
        } else {
            return null;
        }
    }

}
@@ -3323,112 +3323,96 @@
     
-return AnimatorUtils.ofPointF(endValues.view, TRANSLATION_PROPERTY, getPathMotion(),%0A                   
+float startX = (float) startValues.values.get(TRANSLATION_X);%0A            float startY =
  (fl
@@ -3443,35 +3443,35 @@
 get(TRANSLATION_
-X),
+Y);
 %0A               
@@ -3459,39 +3459,44 @@
 Y);%0A            
-       
+float endX =
  (float) startVa
@@ -3480,37 +3480,35 @@
  endX = (float) 
-start
+end
 Values.values.ge
@@ -3525,11 +3525,11 @@
 ION_
-Y),
+X);
 %0A   
@@ -3529,39 +3529,44 @@
 X);%0A            
-       
+float endY =
  (float) endValu
@@ -3595,11 +3595,11 @@
 ION_
-X),
+Y);
 %0A   
@@ -3596,32 +3596,75 @@
 ON_Y);%0A         
+   endValues.view.setTranslationX(startX);%0A
            (floa
@@ -3658,23 +3658,16 @@
         
-(float)
  endValu
@@ -3674,32 +3674,160 @@
 es.v
-alues.get(TRANSLATION_Y)
+iew.setTranslationY(startY);%0A            return AnimatorUtils.ofPointF(endValues.view, TRANSLATION_PROPERTY, getPathMotion(), startX, startY, endX, endY
 );%0A 

fix for #65