package com.ufreedom.floatingview.spring;

import com.facebook.rebound.SimpleSpringListener;
import com.facebook.rebound.Spring;
import com.ufreedom.floatingview.YumFloating;

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

public class SpringHelper {
    
    private float startValue;
    private float endValue;
    private double configValueOne;
    private double configValueTwo;
    private int config;
    private ReboundListener reboundListener;

    private SpringHelper(float startValue, float endValue) {
        this.startValue = startValue;
        this.endValue = endValue;
    }

    public static SpringHelper create(float startValue, float endValue){
        return new SpringHelper(startValue,endValue);
    }

    public static  SpringHelper createWidthBouncinessAndSpeed(float startValue, float endValue,double bounciness, double speed){
        SpringHelper springHelper = new SpringHelper(startValue,endValue);
        return springHelper.configBouncinessAndSpeed(bounciness,speed);
    }

    public static SpringHelper createWidthTensionAndFriction(float startValue, float endValue,double tension, double friction){
        SpringHelper springHelper = new SpringHelper(startValue,endValue);
        return springHelper.configTensionAndFriction(tension,friction);
    }
    
    
    public SpringHelper configBouncinessAndSpeed(double bounciness, double speed){
        configValueOne = bounciness;
        configValueTwo = speed;
        config = 0;
        return this;
    }
    
    public SpringHelper configTensionAndFriction(double tension, double friction){
        configValueOne = tension;
        configValueTwo = friction;
        config = 1;
        return this;
    }
    
    public SpringHelper reboundListener(ReboundListener reboundListener){
        this.reboundListener = reboundListener;
        return this;
    }
    
    
    public void start(final YumFloating yumFloating){
        if (config == -1){
            throw new IllegalStateException("Hi , You mast call one of the method configBouncinessAndSpeed and configTensionAndFriction to make config");
        }
        
        if (config == 0){
            yumFloating.createSpringByBouncinessAndSpeed(configValueOne,configValueTwo)
                    .addListener(new SimpleSpringListener(){
                        @Override
                        public void onSpringUpdate(Spring spring) {
                            if (spring.getCurrentValue() == spring.getEndValue()){
                                if (reboundListener != null){
                                    reboundListener.onReboundEnd();
                                }
                            }
                            if (reboundListener != null){
                                reboundListener.onReboundUpdate(yumFloating.transition(spring.getCurrentValue(),startValue,endValue));
                            }
                        }
                    }).setEndValue(endValue);
            
        }
        
    }
  
}

@@ -2110,32 +2110,62 @@
      %7D%0A        %0A
+        Spring spring = null;%0A
         if (conf
@@ -2185,16 +2185,25 @@
         
+ spring =
  yumFloa
@@ -2270,16 +2270,17 @@
 alueTwo)
+;
 %0A       
@@ -2280,28 +2280,186 @@
         
-            
+%7Delse if (config == 1)%7B%0A            yumFloating.createSpringByTensionAndFriction(configValueOne,configValueTwo);%0A        %7D%0A        if (spring != null)%7B%0A            spring
 .addList
@@ -2507,24 +2507,16 @@
         
-        
 @Overrid
@@ -2517,24 +2517,16 @@
 verride%0A
-        
         
@@ -2593,32 +2593,24 @@
             
-    
-    
 if (spring.g
@@ -2672,32 +2672,24 @@
             
-        
 if (reboundL
@@ -2730,32 +2730,24 @@
             
-       
- 
 reboundListe
@@ -2790,34 +2790,18 @@
         
-        %7D%0A        
+%7D%0A
         
@@ -2830,32 +2830,24 @@
             
-   
-     
 if (reboundL
@@ -2860,32 +2860,24 @@
 r != null)%7B%0A
-        
             
@@ -3011,34 +3011,18 @@
         
-       
- 
 %7D%0A
-        
         
@@ -3043,24 +3043,16 @@
         
- 
-       
 %7D).setEn
@@ -3081,31 +3081,9 @@
     
-    %0A        %7D%0A        
+%7D
 %0A   

fix bug