package com.reactnativenavigation.views;

import android.content.Context;
import android.support.design.widget.CoordinatorLayout;

import com.reactnativenavigation.events.Event;
import com.reactnativenavigation.events.EventBus;
import com.reactnativenavigation.events.FabSetEvent;
import com.reactnativenavigation.events.ScreenChangedEvent;
import com.reactnativenavigation.events.Subscriber;
import com.reactnativenavigation.layouts.Layout;
import com.reactnativenavigation.params.FabParams;
import com.reactnativenavigation.params.SnackbarParams;

public class SnackbarAndFabContainer extends CoordinatorLayout implements Snakbar.OnDismissListener, Subscriber{
    private Snakbar snakbar;
    private FloatingActionButtonCoordinator fabCoordinator;
    private Layout layout;

    public SnackbarAndFabContainer(Context context, Layout layout) {
        super(context);
        this.layout = layout;
        fabCoordinator = new FloatingActionButtonCoordinator(this);
        EventBus.instance.register(this);
    }

    public void showSnackbar(final String navigatorEventId, final SnackbarParams params) {
        snakbar = new Snakbar(this, navigatorEventId, params);
        snakbar.show();
    }

    @Override
    public void onDismiss(Snakbar snakbar) {
        if (this.snakbar == snakbar) {
            this.snakbar = null;
        }
    }

    public void destroy() {
        EventBus.instance.unregister(this);
    }

    @Override
    public void onEvent(Event event) {
        if (ScreenChangedEvent.TYPE.equals(event.getType())) {
            onScreenChange(((ScreenChangedEvent) event).fabParams);
        }
        if (FabSetEvent.TYPE.equals(event.getType())) {
            updateFab(((FabSetEvent) event).fabParams);
        }
    }

    private void onScreenChange(FabParams fabParams) {
        dismissSnackbar();
        updateFab(fabParams);
    }

    public void dismissSnackbar() {
        if (snakbar != null) {
            snakbar.dismiss();
            snakbar = null;
        }
    }

    private void updateFab(final FabParams fabParams) {
        fabCoordinator.remove(new Runnable() {
            @Override
            public void run() {
                if (fabParams != null) {
                    if (layout.getCurrentScreen().getScreenInstanceId().equals(fabParams.screenInstanceId)) {
                        fabCoordinator.add(fabParams);
                    }
                }
            }
        });
    }
}

@@ -541,16 +541,104 @@
 rParams;
+%0Aimport com.reactnativenavigation.screens.Screen;%0A%0Aimport java.util.EmptyStackException;
 %0A%0Apublic
@@ -2320,37 +2320,321 @@
     
-if (layout.getCurrentScreen()
+Screen currentScreen = null;%0A                    try %7B%0A                        currentScreen = layout.getCurrentScreen();%0A                    %7D catch(EmptyStackException exception) %7B%0A                        currentScreen = null;%0A                    %7D%0A                    if (currentScreen != null && currentScreen
 .get

Fix EmptyStackException (#2746)