package com.github.alexkolpa.fabtoolbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import com.melnykov.fab.FloatingActionButton;
import io.codetail.animation.SupportAnimator;
import io.codetail.animation.ViewAnimationUtils;
import io.codetail.widget.RevealFrameLayout;

public class FabToolbar extends RevealFrameLayout {

	private static final int DEFAULT_ANIMATION_DURATION = 500;

	private LinearLayout container;
	private FloatingActionButton button;
	private float screenWidth;
	private int animationDuration = DEFAULT_ANIMATION_DURATION;
	private OnClickListener clickListener;

	public FabToolbar(Context context) {
		super(context);
		init();
	}

	public FabToolbar(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		loadAttributes(attrs);
	}

	public FabToolbar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		loadAttributes(attrs);
	}

	private void init() {
		screenWidth = getResources().getDisplayMetrics().widthPixels;

		inflate(getContext(), R.layout.fab_toolbar, this);
		button = (FloatingActionButton) findViewById(R.id.button);
		button.setOnClickListener(new ButtonClickListener());
		container = ((LinearLayout) findViewById(R.id.container));
	}

	private void loadAttributes(AttributeSet attrs) {
		TypedArray a = getContext().getTheme().obtainStyledAttributes(
				attrs,
				R.styleable.FabToolbar,
				0, 0);

		int containerGravity;
		int buttonGravity;
		try {
			setColor(a.getColor(R.styleable.FabToolbar_tb_color, getResources().getColor(R.color.blue)));
			animationDuration = a.getInteger(R.styleable.FabToolbar_tb_anim_duration, DEFAULT_ANIMATION_DURATION);
			containerGravity = a.getInteger(R.styleable.FabToolbar_tb_container_gravity, 1);
			buttonGravity = a.getInteger(R.styleable.FabToolbar_tb_button_gravity, 2);

		}
		finally {
			a.recycle();
		}

		container.setGravity(getGravity(containerGravity));

		FrameLayout.LayoutParams buttonParams = (LayoutParams) button.getLayoutParams();
		buttonParams.gravity = getGravity(buttonGravity);
	}

	private int getGravity(int gravityEnum) {
		return (gravityEnum == 0 ? Gravity.START : gravityEnum == 1 ? Gravity.CENTER_HORIZONTAL : Gravity.END)
				| Gravity.CENTER_VERTICAL;
	}

	public void setColor(int color) {
		button.setColorNormal(color);
		button.setColorPressed(color);
		container.setBackgroundColor(color);
	}

	public void setAnimationDuration(int duration) {
		animationDuration = duration;
	}

	public void setButtonOnClickListener(OnClickListener listener) {
		clickListener = listener;
	}

	public void attachToListView(AbsListView listView) {
		button.attachToListView(listView);
	}

	public void attachToRecyclerView(RecyclerView recyclerView) {
		button.attachToRecyclerView(recyclerView);
	}

	public void setButtonIcon(Drawable drawable) {
		button.setImageDrawable(drawable);
	}

	public void setButtonIcon(int resId) {
		button.setImageResource(resId);
	}

	public void show() {
		button.setOnClickListener(null);
		button.setVisibility(GONE);
		container.setVisibility(VISIBLE);
		animateCircle(0, screenWidth, null);
	}

	public void hide() {
		//If button was attached to list and got hidden, closing the toolbar should still show the button
		button.show(false);
		animateCircle(screenWidth, 0, new ToolbarCollapseListener());
	}

	private void animateCircle(float startRadius, float endRadius, SupportAnimator.AnimatorListener listener) {
		int cx = (button.getLeft() + button.getRight()) / 2;
		int cy = (button.getTop() + button.getBottom()) / 2;

		SupportAnimator animator =
				ViewAnimationUtils.createCircularReveal(container, cx, cy, startRadius, endRadius);
		animator.setInterpolator(new AccelerateDecelerateInterpolator());
		animator.setDuration(animationDuration);
		if (listener != null) {
			animator.addListener(listener);
		}
		animator.start();

	}

	@Override
	public void addView(@NonNull View child) {
		if (canAddViewToContainer(child)) {
			container.addView(child);
		}
		else {
			super.addView(child);
		}
	}

	@Override
	public void addView(@NonNull View child, int width, int height) {
		if (canAddViewToContainer(child)) {
			container.addView(child, width, height);
		}
		else {
			super.addView(child, width, height);
		}
	}

	@Override
	public void addView(@NonNull View child, ViewGroup.LayoutParams params) {
		if (canAddViewToContainer(child)) {
			container.addView(child, params);
		}
		else {
			super.addView(child, params);
		}
	}

	@Override
	public void addView(@NonNull View child, int index, ViewGroup.LayoutParams params) {
		if (canAddViewToContainer(child)) {
			container.addView(child, index, params);
		}
		else {
			super.addView(child, index, params);
		}
	}

	private boolean canAddViewToContainer(View child) {
		return container != null && !(child instanceof FloatingActionButton);
	}

	private class ButtonClickListener implements OnClickListener {
		@Override
		public void onClick(View v) {
			show();

			if (clickListener != null) {
				clickListener.onClick(v);
			}
		}
	}

	private class ToolbarCollapseListener implements SupportAnimator.AnimatorListener {
		@Override
		public void onAnimationEnd() {
			container.setVisibility(GONE);
            button.setVisibility(VISIBLE);
			button.setOnClickListener(new ButtonClickListener());
		}

		@Override public void onAnimationStart() {}

		@Override public void onAnimationCancel() {}

		@Override public void onAnimationRepeat() {}
	}
}

@@ -196,55 +196,8 @@
 ll;%0A
-import android.support.v7.widget.RecyclerView;%0A
 impo
@@ -226,16 +226,16 @@
 uteSet;%0A
+
 import a
@@ -380,43 +380,8 @@
 or;%0A
-import android.widget.AbsListView;%0A
 impo
@@ -451,54 +451,8 @@
 ut;%0A
-import com.melnykov.fab.FloatingActionButton;%0A
 impo
@@ -542,16 +542,16 @@
 nUtils;%0A
-
 import i
@@ -586,16 +586,75 @@
 eLayout;
+%0Aimport android.support.design.widget.FloatingActionButton;
 %0A%0Apublic
@@ -2654,26 +2654,32 @@
 nt color) %7B%0A
-%09%09
+        
 button.setCo
@@ -2680,52 +2680,23 @@
 .set
-ColorNormal(color);%0A%09%09button.setColorPressed
+BackgroundColor
 (col
@@ -2931,214 +2931,8 @@
 %09%7D%0A%0A
-%09public void attachToListView(AbsListView listView) %7B%0A%09%09button.attachToListView(listView);%0A%09%7D%0A%0A%09public void attachToRecyclerView(RecyclerView recyclerView) %7B%0A%09%09button.attachToRecyclerView(recyclerView);%0A%09%7D%0A
 %0A%09pu
@@ -3287,129 +3287,8 @@
 ) %7B%0A
-%09%09//If button was attached to list and got hidden, closing the toolbar should still show the button%0A%09%09button.show(false);
 %0A%09%09a

use design library for FAB