package com.azendoo.reactnativesnackbar;

import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.Snackbar;

import android.view.View;

import android.widget.TextView;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;

import java.util.HashMap;
import java.util.Map;

public class SnackbarModule extends ReactContextBaseJavaModule{

    private static final String REACT_NAME = "RNSnackbar";

    public SnackbarModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

    @Override
    public String getName() {
        return REACT_NAME;
    }

    @Override
    public Map<String, Object> getConstants() {
        final Map<String, Object> constants = new HashMap<>();

        constants.put("LENGTH_LONG", Snackbar.LENGTH_LONG);
        constants.put("LENGTH_SHORT", Snackbar.LENGTH_SHORT);
        constants.put("LENGTH_INDEFINITE", Snackbar.LENGTH_INDEFINITE);

        return constants;
    }

    @ReactMethod
    public void show(ReadableMap options, final Callback callback) {
        View view = getCurrentActivity().findViewById(android.R.id.content);

        if (view == null) return;

        String title = options.hasKey("title") ? options.getString("title") : "Hello";
        int duration = options.hasKey("duration") ? options.getInt("duration") : Snackbar.LENGTH_SHORT;

        Snackbar snackbar = Snackbar.make(view, title, duration);

        // Set the background color.
        if (options.hasKey("backgroundColor")) {
            snackbar.getView().setBackgroundColor(options.getInt("backgroundColor"));
        }

        if (options.hasKey("action")) {
            View.OnClickListener onClickListener = new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.invoke();
                }
            };

            ReadableMap actionDetails = options.getMap("action");
            snackbar.setAction(actionDetails.getString("title"), onClickListener);
            snackbar.setActionTextColor(actionDetails.getInt("color"));
        }

        // For older devices, explicitly set the text color; otherwise it may appear dark gray.
        // http://stackoverflow.com/a/31084530/763231
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            View snackbarView = snackbar.getView();
            TextView snackbarText = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
            snackbarText.setTextColor(Color.WHITE);
        }

        snackbar.show();
    }
}

@@ -130,33 +130,32 @@
 idget.Snackbar;%0A
-%0A
 import android.v
@@ -164,16 +164,46 @@
 w.View;%0A
+import android.view.ViewGroup;
 %0Aimport 
@@ -223,24 +223,25 @@
 t.TextView;%0A
+%0A
 import com.f
@@ -483,16 +483,44 @@
 leMap;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -685,16 +685,112 @@
 kbar%22;%0A%0A
+    // Store all modals in the screen.%0A    private ArrayList%3CView%3E modals = new ArrayList%3C%3E();%0A%0A
     publ
@@ -1417,15 +1417,32 @@
 View
+Group
  view =
+ (ViewGroup)
  get
@@ -1459,16 +1459,43 @@
 ivity().
+getWindow().getDecorView().
 findView
@@ -1558,16 +1558,488 @@
 eturn;%0A%0A
+        if (!view.hasWindowFocus()) %7B%0A            // The view is not focused, we should get all the modal views in the screen.%0A            recursiveLoopChildren(view);%0A%0A            for(View modalViews : modals) %7B%0A                displaySnackbar(modalViews, options, callback);%0A            %7D%0A%0A            return;%0A        %7D%0A%0A        displaySnackbar(view, options, callback);%0A    %7D%0A%0A    private void displaySnackbar(View view, ReadableMap options, final Callback callback) %7B%0A
         
@@ -3449,11 +3449,494 @@
 );%0A    %7D
+%0A%0A    private void recursiveLoopChildren(ViewGroup view) %7B%0A        // Get all the modal views.%0A        if (view.getClass().getSimpleName().equalsIgnoreCase(%22ReactModalHostView%22)) %7B%0A            modals.add(view.getChildAt(0));%0A        %7D%0A%0A        for (int i = view.getChildCount() - 1; i %3E= 0; i--) %7B%0A            final View child = view.getChildAt(i);%0A%0A            if (child instanceof ViewGroup) %7B%0A                recursiveLoopChildren((ViewGroup) child);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

Add support for modals on Android (#17)