package ru.wapstart.plus1.sdk;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

final class Plus1ImageView extends View {
	private Drawable	image;
	private Movie		movie;
	private long 		movieStart;
	
	public Plus1ImageView(Context context) {
		super(context);
	}

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

	public Plus1ImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setImage(Drawable drawable) {
		this.image	= drawable;
		this.movie	= null;

		this.movieStart = 0;
	}
	
	public void setMovie(Movie movie) {
		this.movie	= movie;
		this.image	= null;
		
		this.movieStart = 0;
		
		final Handler handler = new Handler();
		final View view = this;
		
		new Thread() {
		    @Override public void run() {
		        while(!Thread.currentThread().isInterrupted()) {
		            handler.post(new Runnable() {
		                public void run(){
		                    view.invalidate();
		                }
		            });
		            try {
		                Thread.sleep(40); // yields 25 fps
		            } catch (InterruptedException e) {
		                Thread.currentThread().interrupt();
		            }
		        }
		    }
		}.start();		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		
		if (image != null) {
			int x = (getWidth() - image.getIntrinsicWidth()) / 2;
			int y = (getHeight() - image.getIntrinsicHeight()) / 2;
			
			image.setBounds(
				x, y, 
				x + image.getIntrinsicWidth(), y + image.getIntrinsicHeight()
			);
			
			image.draw(canvas);
		}
		
		if (movie != null) {
			long now = android.os.SystemClock.uptimeMillis();
			if (movieStart == 0)
				movieStart = now;
			
			int duration = movie.duration();
			if (duration == 0)
				duration = 1;
			
			int relTime = (int)((now - movieStart) % duration);
			
			movie.setTime(relTime);
			movie.draw(
				canvas, 
				getWidth() - movie.width(),
				getHeight() - movie.height()
			);
		}
	}
	
}

@@ -431,19 +431,46 @@
 ntext);%0A
+%09%09startInvalidateThread();%0A
 %09%7D%0A
-
 %0A%09public
@@ -549,16 +549,43 @@
 attrs);%0A
+%09%09startInvalidateThread();%0A
 %09%7D%0A%0A%09pub
@@ -691,16 +691,43 @@
 Style);%0A
+%09%09startInvalidateThread();%0A
 %09%7D%0A%0A%09pub
@@ -950,580 +950,8 @@
  0;%0A
-%09%09%0A%09%09final Handler handler = new Handler();%0A%09%09final View view = this;%0A%09%09%0A%09%09new Thread() %7B%0A%09%09    @Override public void run() %7B%0A%09%09        while(!Thread.currentThread().isInterrupted()) %7B%0A%09%09            handler.post(new Runnable() %7B%0A%09%09                public void run()%7B%0A%09%09                    view.invalidate();%0A%09%09                %7D%0A%09%09            %7D);%0A%09%09            try %7B%0A%09%09                Thread.sleep(40); // yields 25 fps%0A%09%09            %7D catch (InterruptedException e) %7B%0A%09%09                Thread.currentThread().interrupt();%0A%09%09            %7D%0A%09%09        %7D%0A%09%09    %7D%0A%09%09%7D.start();%09%09%0A
 %09%7D%0A%09
@@ -1707,12 +1707,622 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
+%0A%09private void startInvalidateThread()%0A%09%7B%0A%09%09final Handler handler = new Handler();%0A%09%09final View view = this;%0A%09%09%0A%09%09new Thread() %7B%0A%09%09    @Override public void run() %7B%0A%09%09        while(!Thread.currentThread().isInterrupted()) %7B%0A%09%09            handler.post(new Runnable() %7B%0A%09%09                public void run()%7B%0A%09%09                    view.invalidate();%0A%09%09                %7D%0A%09%09            %7D);%0A%09%09            try %7B%0A%09%09                Thread.sleep(40); // yields 25 fps%0A%09%09            %7D catch (InterruptedException e) %7B%0A%09%09                Thread.currentThread().interrupt();%0A%09%09            %7D%0A%09%09        %7D%0A%09%09    %7D%0A%09%09%7D.start();%0A%09%7D
 %0A%7D%0A

move thread