/*
 *   Copyright 2014 Oguz Bilgener
 */
package com.oguzdev.circularfloatingactionmenu.library;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

/**
 * A simple button implementation with a similar look an feel to{@link FloatingActionButton}.
 */
public class SubActionButton extends FrameLayout {

    public static final int THEME_LIGHT = 0;
    public static final int THEME_DARK = 1;
    public static final int THEME_LIGHTER = 2;
    public static final int THEME_DARKER = 3;

    public SubActionButton(Activity activity, FrameLayout.LayoutParams layoutParams, int theme, Drawable backgroundDrawable, View contentView) {
        super(activity);
        setLayoutParams(layoutParams);
        // If no custom backgroundDrawable is specified, use the background drawable of the theme.
        if(backgroundDrawable == null) {
            if(theme == THEME_LIGHT) {
                backgroundDrawable = activity.getResources().getDrawable(R.drawable.button_sub_action_selector);
            }
            else if(theme == THEME_DARK) {
                backgroundDrawable = activity.getResources().getDrawable(R.drawable.button_sub_action_dark_selector);
            }
            else if(theme == THEME_LIGHTER) {
                backgroundDrawable = activity.getResources().getDrawable(R.drawable.button_action_selector);
            }
            else if(theme == THEME_DARKER) {
                backgroundDrawable = activity.getResources().getDrawable(R.drawable.button_action_dark_selector);
            }
            else {
                throw new RuntimeException("Unknown SubActionButton theme: " + theme);
            }
        }
        else {
            backgroundDrawable = backgroundDrawable.mutate().getConstantState().newDrawable();
        }
        setBackgroundResource(backgroundDrawable);
        if(contentView != null) {
            setContentView(contentView);
        }
        setClickable(true);
    }

    /**
     * Sets a content view that will be displayed inside this SubActionButton.
     * @param contentView
     */
    public void setContentView(View contentView) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
        final int margin = getResources().getDimensionPixelSize(R.dimen.sub_action_button_content_margin);
        params.setMargins(margin, margin, margin, margin);
        contentView.setLayoutParams(params);
        contentView.setClickable(false);
        this.addView(contentView, params);
    }

    private void setBackgroundResource(Drawable drawable) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            setBackground(drawable);
        }
        else {
            setBackgroundDrawable(drawable);
        }
    }

    /**
     * A builder for {@link SubActionButton} in conventional Java Builder format
     */
    public static class Builder {

        private Activity activity;
        private FrameLayout.LayoutParams layoutParams;
        private int theme;
        private Drawable backgroundDrawable;
        private View contentView;

        public Builder(Activity activity) {
            this.activity = activity;

            // Default SubActionButton settings
            int size = activity.getResources().getDimensionPixelSize(R.dimen.sub_action_button_size);
            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(size, size, Gravity.TOP | Gravity.LEFT);
            setLayoutParams(params);
            setTheme(SubActionButton.THEME_LIGHT);
        }

        public Builder setLayoutParams(FrameLayout.LayoutParams params) {
            this.layoutParams = params;
            return this;
        }

        public Builder setTheme(int theme) {
            this.theme = theme;
            return this;
        }

        public Builder setBackgroundDrawable(Drawable backgroundDrawable) {
            this.backgroundDrawable = backgroundDrawable;
            return this;
        }

        public Builder setContentView(View contentView) {
            this.contentView = contentView;
            return this;
        }

        public SubActionButton build() {
            return new SubActionButton(activity,
                    layoutParams,
                    theme,
                    backgroundDrawable,
                    contentView);
        }
    }
}

@@ -124,40 +124,8 @@
 ty;%0A
-import android.content.Context;%0A
 impo
@@ -192,42 +192,8 @@
 ld;%0A
-import android.util.AttributeSet;%0A
 impo
@@ -247,39 +247,8 @@
 ew;%0A
-import android.view.ViewGroup;%0A
 impo
@@ -749,32 +749,72 @@
 View contentView
+, FrameLayout.LayoutParams contentParams
 ) %7B%0A        supe
@@ -2053,32 +2053,47 @@
 View(contentView
+, contentParams
 );%0A        %7D%0A   
@@ -2159,16 +2159,41 @@
 nt view 
+with custom LayoutParams 
 that wil
@@ -2262,16 +2262,37 @@
 entView%0A
+     * @param params%0A
      */%0A
@@ -2338,27 +2338,17 @@
 tentView
-) %7B%0A       
+,
  FrameLa
@@ -2357,32 +2357,83 @@
 ut.LayoutParams 
+params) %7B%0A        if(params == null) %7B%0A            
 params = new Fra
@@ -2533,16 +2533,20 @@
         
+    
 final in
@@ -2640,24 +2640,28 @@
 n);%0A        
+    
 params.setMa
@@ -2691,32 +2691,43 @@
 argin, margin);%0A
+        %7D%0A%0A
         contentV
@@ -2737,87 +2737,240 @@
 .set
-LayoutParams(params);%0A        contentView.setClickable(false);%0A        this.add
+Clickable(false);%0A        this.addView(contentView, params);%0A    %7D%0A%0A    /**%0A     * Sets a content view with default LayoutParams%0A     * @param contentView%0A     */%0A    public void setContentView(View contentView) %7B%0A        setContent
 View
@@ -2975,38 +2975,36 @@
 ew(contentView, 
-params
+null
 );%0A    %7D%0A%0A    pr
@@ -3579,16 +3579,72 @@
 entView;
+%0A        private FrameLayout.LayoutParams contentParams;
 %0A%0A      
@@ -4648,32 +4648,258 @@
 his;%0A        %7D%0A%0A
+        public Builder setContentView(View contentView, FrameLayout.LayoutParams contentParams) %7B%0A            this.contentView = contentView;%0A            this.contentParams = contentParams;%0A            return this;%0A        %7D%0A%0A
         public S
@@ -5104,16 +5104,51 @@
 tentView
+,%0A                    contentParams
 );%0A     

allow sub action button content views to have custom layout params