package com.freak.dashboard.animation;

import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.util.Log;
import android.widget.ImageView;

import com.freak.dashboard.GetInfoThread;
import com.freak.dashboard.bus.AnimationEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.Timer;
import java.util.TimerTask;

public abstract class SpriteAnimation {

    private static final boolean DEBUG = false;
    private static final String TAG = SpriteAnimation.class.getSimpleName();

    private final int mAnimationStop;
    private final int mAnimationLow;
    private final int mAnimationMedium;
    private final int mAnimationHigh;

    private final ImageView mAnimationView;
    private AnimationDrawable mAnimation;

    public SpriteAnimation(ImageView animation){
        mAnimationView = animation;
        mAnimationStop = getAnimationStop();
        mAnimationLow = getAnimationLow();
        mAnimationMedium = getAnimationMedium();
        mAnimationHigh = getAnimationHigh();


        mAnimationView.setImageResource(mAnimationStop);
        mAnimation = (AnimationDrawable) mAnimationView.getDrawable();
        mAnimation.start();
    }

    @Subscribe
    public void onAnimationEvent(final AnimationEvent event) {
        if (event.getAnimationStatus() == GetInfoThread.ANIM_STATUS_SPEED_0){
            mAnimation.stop();
            mAnimationView.setImageResource(mAnimationStop);
            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();
            mAnimation.start();
        } else if (event.getAnimationStatus() == GetInfoThread.ANIM_STATUS_SPEED_1){
            mAnimation.stop();
            mAnimationView.setImageResource(mAnimationLow);
            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();
            mAnimation.start();
        } else if (event.getAnimationStatus() == GetInfoThread.ANIM_STATUS_SPEED_2){
            mAnimation.stop();
            mAnimationView.setImageResource(mAnimationMedium);
            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();
            mAnimation.start();
        } else {
            mAnimation.stop();
            mAnimationView.setImageResource(mAnimationHigh);
            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();
            mAnimation.start();
        }
    }

    protected abstract int getAnimationStop();
    protected abstract int getAnimationLow();
    protected abstract int getAnimationMedium();
    protected abstract int getAnimationHigh();

}

@@ -763,16 +763,61 @@
 onView;%0A
+    private final Handler mAnimationHandler;%0A
     priv
@@ -1119,16 +1119,59 @@
 igh();%0A%0A
+        mAnimationHandler = new Handler();%0A
 %0A       
@@ -1498,475 +1498,167 @@
     
-mAnimation.stop();%0A            mAnimationView.setImageResource(mAnimationStop);%0A            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();%0A            mAnimation.start();%0A        %7D else if (event.getAnimationStatus() == GetInfoThread.ANIM_STATUS_SPEED_1)%7B%0A            mAnimation.stop();%0A            mAnimationView.setImageResource(mAnimationLow);%0A            mAnimation = (AnimationDrawable) mAnimationView.getDrawable();%0A            mAnimation.start(
+changeAnimation(mAnimationStop);%0A        %7D else if (event.getAnimationStatus() == GetInfoThread.ANIM_STATUS_SPEED_1)%7B%0A            changeAnimation(mAnimationLow
 );%0A 
@@ -1745,33 +1745,38 @@
 2)%7B%0A            
-m
+change
 Animation.stop()
@@ -1772,60 +1772,72 @@
 tion
-.stop();%0A            mAnimationView.setImageResource
+(mAnimationMedium);%0A        %7D else %7B%0A            changeAnimation
 (mAn
@@ -1835,38 +1835,36 @@
 ation(mAnimation
-Medium
+High
 );%0A            m
@@ -1862,121 +1862,137 @@
     
+%7D%0A
     
-mAnimation = (AnimationDrawable) mAnimationView.getDrawable();%0A            mAnimation.start();%0A        %7D else
+%7D%0A%0A    public void changeAnimation(final int animation) %7B%0A        mAnimationHandler.post(new Runnable() %7B public void run()
  %7B%0A 
@@ -2065,26 +2065,25 @@
 esource(
-mA
+a
 nimation
 High);%0A 
@@ -2066,36 +2066,32 @@
 source(animation
-High
 );%0A            m
@@ -2184,33 +2184,40 @@
 tart();%0A        
-%7D
+    %7D%7D);
 %0A    %7D%0A%0A    prot

Fix issue when upadting animation