package com.alorma.github.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.v4.view.ActionProvider;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.BounceInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alorma.github.R;
import com.alorma.github.sdk.bean.dto.response.Notification;
import com.alorma.github.sdk.services.client.BaseClient;
import com.alorma.github.sdk.services.notifications.GetNotificationsClient;
import com.alorma.githubicons.GithubIconDrawable;
import com.alorma.githubicons.GithubIconify;
import com.nineoldandroids.animation.ValueAnimator;

import java.util.List;

import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by Bernat on 18/02/2015.
 */
public class NotificationsActionProvider extends ActionProvider implements BaseClient.OnResultCallback<List<Notification>>, View.OnClickListener {

	private NotificationImageView bt;
	private OnNotificationListener onNotificationListener;

	/**
	 * Creates a new instance.
	 *
	 * @param context Context for accessing resources.
	 */
	public NotificationsActionProvider(Context context) {
		super(context);
	}

	@Override
	public View onCreateActionView() {


		int actionBarSize = getContext().getResources().getDimensionPixelSize(android.support.v7.appcompat.R.dimen.abc_action_button_min_height_material);


		ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(actionBarSize, actionBarSize);

		LinearLayout layout = new LinearLayout(getContext());
		layout.setLayoutParams(layoutParams);
		layout.setGravity(Gravity.CENTER);
		layout.setOnClickListener(this);

		bt = new NotificationImageView(getContext(), getContext().getResources().getColor(R.color.repos_accent));
		bt.setOnClickListener(this);

		layout.addView(bt);

		GetNotificationsClient client = new GetNotificationsClient(getContext());
		client.setOnResultCallback(this);
		client.execute();

		return layout;
	}

	@Override
	public void onResponseOk(List<Notification> notifications, Response r) {
		if (bt != null && notifications != null) {
			bt.showNotificationBubble(notifications.size() > 0);
			if (onNotificationListener != null)  {
				onNotificationListener.onNotificationInfoReceived(notifications.size());
			}
		}
	}

	@Override
	public void onFail(RetrofitError error) {

	}

	@Override
	public void onClick(View v) {
		if (bt != null & onNotificationListener != null) {
			onNotificationListener.onNotificationRequested();
		}
	}

	public void setOnNotificationListener(OnNotificationListener onNotificationListener) {
		this.onNotificationListener = onNotificationListener;
	}

	public interface OnNotificationListener {
		void onNotificationRequested();
		void onNotificationInfoReceived(int notifications);
	}
	
	private class NotificationImageView extends ImageView implements ValueAnimator.AnimatorUpdateListener {

		private final Rect rect;
		private Paint paint;
		private boolean show;
		
		private long animationDuration = 500;
		private ValueAnimator animator;
		private int value;

		public NotificationImageView(Context context, int color) {
			super(context);
			rect = new Rect();
			paint = new Paint();
			paint.setAntiAlias(true);
			paint.setColor(color);

			GithubIconDrawable drawable = new GithubIconDrawable(context, GithubIconify.IconValue.octicon_inbox);
			drawable.actionBarSize();
			drawable.colorRes(R.color.repos_icons);
			setImageDrawable(drawable);

			this.show = false;
		}

		@Override
		protected void onDraw(Canvas canvas) {
			super.onDraw(canvas);

			if (show || (animator != null && animator.isRunning())) {
				canvas.getClipBounds(rect);

				int radius = value;
				int top = rect.top + (int) animator.getAnimatedValue();
				int right = rect.right - (int) animator.getAnimatedValue();

				canvas.drawCircle(right, top, radius, paint);
			}
		}

		public void showNotificationBubble(boolean show) {
			this.show = show;

			int start = show ? 0 : 16;
			int end = show ? 16 : 0;

			animator = ValueAnimator.ofInt(start, end);
			animator.setDuration(animationDuration);
			animator.setInterpolator(show ? new BounceInterpolator() : new AccelerateDecelerateInterpolator());
			animator.addUpdateListener(this);
			animator.start();
			
			this.postInvalidate();
		}

		@Override
		public void onAnimationUpdate(ValueAnimator animation) {
			value = (int) animation.getAnimatedValue();
			this.postInvalidate();
		}

	}
}

@@ -1122,16 +1122,55 @@
 ener %7B%0A%0A
+%09private int currentNotifications = 0;%0A
 %09private
@@ -1196,16 +1196,16 @@
 iew bt;%0A
-
 %09private
@@ -1472,17 +1472,16 @@
 ew() %7B%0A%0A
-%0A
 %09%09int ac
@@ -1622,17 +1622,16 @@
 rial);%0A%0A
-%0A
 %09%09ViewGr
@@ -2340,16 +2340,76 @@
 l) %7B%0A%09%09%09
+%0A%09%09%09if (currentNotifications != notifications.size()) %7B%0A%09%09%09%09
 bt.showN
@@ -2453,16 +2453,17 @@
 ) %3E 0);%0A
+%09
 %09%09%09if (o
@@ -2496,12 +2496,12 @@
 ull)
- 
  %7B%0A
+%09
 %09%09%09%09
@@ -2573,16 +2573,22 @@
 ize());%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D
@@ -3032,16 +3032,17 @@
 sted();%0A
+%0A
 %09%09void o
@@ -3090,18 +3090,17 @@
 ns);%0A%09%7D%0A
-%09
 %0A
+
 %09private
@@ -3271,19 +3271,17 @@
 n show;%0A
-%09%09
 %0A
+
 %09%09privat
@@ -4554,19 +4554,16 @@
 tart();%0A
-%09%09%09
 %0A%09%09%09this

Fix notifications bubble