package com.github.aleksandermielczarek.bindingsnackbar;

import android.content.res.ColorStateList;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.design.widget.Snackbar;
import android.view.View;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by Aleksander on 20.04.2016.
 */
public class BindingSnackbar {

    private final CharSequence text;
    private final int resId;
    private final int duration;
    private final List<SnackbarSettingsSetter> snackbarSettingsSetters;

    private BindingSnackbar(Builder builder) {
        text = builder.text;
        resId = builder.resId;
        duration = builder.duration;
        snackbarSettingsSetters = builder.snackbarSettingsSetters;
    }

    public static Builder builder(@NonNull CharSequence text, @Snackbar.Duration int duration) {
        return new Builder(text, duration);
    }

    public static Builder builder(@StringRes int resId, @Snackbar.Duration int duration) {
        return new Builder(resId, duration);
    }

    private Snackbar asSnackbar(View view) {
        Snackbar snackbar = text == null ? Snackbar.make(view, resId, duration) : Snackbar.make(view, text, duration);
        for (SnackbarSettingsSetter settingsSetter : snackbarSettingsSetters) {
            settingsSetter.setSettings(snackbar);
        }
        return snackbar;
    }

    public void show(@NonNull View view) {
        Snackbar snackbar = asSnackbar(view);
        snackbar.show();
    }

    public void show(@NonNull ObservableBindingSnackbar observableBindingSnackbar) {
        observableBindingSnackbar.set(this);
    }

    public static final class Builder {

        private CharSequence text;
        private int resId;
        private int duration;
        private List<SnackbarSettingsSetter> snackbarSettingsSetters;

        private Builder(int duration) {
            this.duration = duration;
        }

        private Builder(CharSequence text, int duration) {
            this(duration);
            this.text = text;
        }

        private Builder(int resId, int duration) {
            this(duration);
            this.resId = resId;
        }

        public Builder addSnackbarSettingsSetter(SnackbarSettingsSetter snackbarSettingsSetter) {
            if (snackbarSettingsSetters == null) {
                snackbarSettingsSetters = new ArrayList<>();
            }
            snackbarSettingsSetters.add(snackbarSettingsSetter);
            return this;
        }

        public BindingSnackbar build() {
            if (snackbarSettingsSetters == null) {
                snackbarSettingsSetters = Collections.emptyList();
            }
            return new BindingSnackbar(this);
        }

        public Builder setAction(@StringRes final int resId, final View.OnClickListener listener) {
            return addSnackbarSettingsSetter(new SnackbarSettingsSetter() {
                @Override
                public void setSettings(Snackbar snackbar) {
                    snackbar.setAction(resId, listener);
                }
            });
        }

        public Builder setAction(final CharSequence text, final View.OnClickListener listener) {
            return addSnackbarSettingsSetter(new SnackbarSettingsSetter() {
                @Override
                public void setSettings(Snackbar snackbar) {
                    snackbar.setAction(text, listener);
                }
            });
        }

        public Builder setActionTextColor(final ColorStateList colors) {
            return addSnackbarSettingsSetter(new SnackbarSettingsSetter() {
                @Override
                public void setSettings(Snackbar snackbar) {
                    snackbar.setActionTextColor(colors);
                }
            });
        }

        public Builder setActionTextColor(@ColorInt final int color) {
            return addSnackbarSettingsSetter(new SnackbarSettingsSetter() {
                @Override
                public void setSettings(Snackbar snackbar) {
                    snackbar.setActionTextColor(color);
                }
            });
        }

        public Builder setCallback(final Snackbar.Callback callback) {
            return addSnackbarSettingsSetter(new SnackbarSettingsSetter() {
                @Override
                public void setSettings(Snackbar snackbar) {
                    snackbar.setCallback(callback);
                }
            });
        }

    }
}

@@ -629,24 +629,56 @@
 gsSetters;%0A%0A
+    private Snackbar snackbar;%0A%0A
     private 
@@ -1186,24 +1186,20 @@
 private 
-Snackbar
+void
  asSnack
@@ -1219,33 +1219,37 @@
 ) %7B%0A        
-S
+if (s
 nackbar 
 snackbar = t
@@ -1228,32 +1228,55 @@
    if (snackbar 
+== null) %7B%0A            
 snackbar = text 
@@ -1369,16 +1369,20 @@
         
+    
 for (Sna
@@ -1441,24 +1441,28 @@
 sSetters) %7B%0A
+    
             
@@ -1499,32 +1499,36 @@
 ckbar);%0A        
+    
 %7D%0A        return
@@ -1525,24 +1525,118 @@
     
-return snackbar;
+%7D%0A    %7D%0A%0A    public void dismiss() %7B%0A        if (snackbar != null) %7B%0A            snackbar.dismiss();%0A        %7D
 %0A   
@@ -1693,28 +1693,8 @@
     
- Snackbar snackbar =
  asS

Add dismiss method