package com.ufreedom.floatingview.transition;

import android.view.View;

import com.facebook.rebound.Spring;
import com.facebook.rebound.SpringConfig;
import com.facebook.rebound.SpringSystem;
import com.facebook.rebound.SpringUtil;

import java.lang.ref.WeakReference;

import static android.R.attr.rotationX;

/**
 * Author UFreedom
 * Date : 2016 十月 19
 */

public class YumFloating implements ITransition, Rebound {

    private SpringSystem springSystem;
    private WeakReference<View> targetViewWeakReference;
    
    public YumFloating(View targetView, SpringSystem springSystem) {
        targetViewWeakReference = new WeakReference<View>(targetView);
        this.springSystem = springSystem;
    }


    public YumFloating(View targetView) {
        targetViewWeakReference = new WeakReference<View>(targetView);

    }

    public View getTargetView() {
        return targetViewWeakReference.get();
    }

    @Override
    public void setAlpha(float alpha) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setAlpha(alpha);
        }
    }

    @Override
    public void setRotation(float rotation) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setRotation(rotation);
        }
    }

    @Override
    public void setRotationX(float rotationX) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setRotationX(rotationX);
        }
    }

    @Override
    public void setRotationY(float rotationY) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setRotationY(rotationY);
        }
    }

    @Override
    public void setScaleX(float scaleX) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setScaleX(scaleX);
        }
    }

    @Override
    public void setScaleY(float scaleY) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setScaleY(scaleY);
        }
    }

    @Override
    public void setScrollX(int scrollX) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setScaleX(scrollX);
        }
    }

    @Override
    public void setScrollY(int scrollY) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setScaleY(scrollY);
        }
      
    }

    @Override
    public void setTranslationX(float translationX) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setTranslationX(translationX);
        }
    }

    @Override
    public void setTranslationY(float translationY) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setTranslationY(translationY);
        }
    }

    @Override
    public void setX(float x) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setX(x);
        }
    }

    @Override
    public void setY(float y) {
        View targetView;
        if ((targetView = getTargetView()) !=null){
            targetView.setY(y);
        }
    }


    @Override
    public Spring createSpringByBouncinessAndSpeed(double bounciness, double speed) {
        return springSystem.createSpring()
                .setSpringConfig(SpringConfig.fromBouncinessAndSpeed(bounciness, speed));
    }

    @Override
    public Spring createSpringByTensionAndFriction(double tension, double friction) {
        return springSystem.createSpring()
                .setSpringConfig(SpringConfig.fromOrigamiTensionAndFriction(tension, friction));
    }

    @Override
    public float transition(double progress, float startValue, float endValue) {
        return (float) SpringUtil.mapValueFromRangeToRange(progress, 0, 1, startValue, endValue);
    }
    
    
}

@@ -65,16 +65,79 @@
 ew.View;
+%0Aimport android.view.ViewGroup;%0Aimport android.view.ViewParent;
 %0A%0Aimport
@@ -332,49 +332,8 @@
 e;%0A%0A
-import static android.R.attr.rotationX;%0A%0A
 /**%0A
@@ -3982,10 +3982,388 @@
   %0A    %0A
+    public void clear()%7B%0A        View view = getTargetView();%0A        if (view != null)%7B%0A            ViewParent viewParent =  view.getParent();%0A            if (viewParent instanceof ViewGroup)%7B%0A                ViewGroup parent = (ViewGroup) viewParent;%0A                parent.removeView(view);%0A                targetViewWeakReference.clear();%0A            %7D%0A        %7D%0A    %7D%0A    %0A
 %7D%0A

add clear method