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.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import io.codetail.animation.SupportAnimator;
import io.codetail.animation.ViewAnimationUtils;
import io.codetail.widget.RevealFrameLayout;
import android.support.design.widget.FloatingActionButton;

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.setBackgroundColor(color);
		container.setBackgroundColor(color);
	}

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

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


	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() {

		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() {}
	}
}

@@ -67,16 +67,59 @@
 ontext;%0A
+import android.content.res.ColorStateList;%0A
 import a
@@ -2729,28 +2729,55 @@
 ckground
-Color
+TintList(ColorStateList.valueOf
 (color)
+)
 ;%0A%09%09cont

change FAB color