package org.edx.mobile.base;

import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import com.google.android.gms.cast.framework.CastButtonFactory;
import com.google.android.gms.cast.framework.CastStateListener;

import org.edx.mobile.R;
import org.edx.mobile.event.NewRelicEvent;
import org.edx.mobile.googlecast.GoogleCastDelegate;

import de.greenrobot.event.EventBus;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;

public abstract class BaseAppActivity extends RoboAppCompatActivity implements CastStateListener {

    private GoogleCastDelegate googleCastDelegate;
    private MenuItem mediaRouteMenuItem;

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().post(new NewRelicEvent(getClass().getSimpleName()));
        googleCastDelegate = GoogleCastDelegate.getInstance(MainApplication.getEnvironment(this)
                .getAnalyticsRegistry());
        googleCastDelegate.addCastStateListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO: Replace the try-catch block with more appropriate logic so Travis-ci build get passed.
        // Can't access the CastButtonFactory while executing test cases of "CourseUnitNavigationActivityTest"
        // and throw exception.
        try {
            if (googleCastDelegate != null && (googleCastDelegate.isConnected() || showGoogleCastButton())) {
                getMenuInflater().inflate(R.menu.google_cast_menu_item, menu);
                mediaRouteMenuItem = CastButtonFactory.setUpMediaRouteButton(getApplicationContext(),
                        menu, R.id.media_route_menu_item);
                // show the introduction overlay.
                if (isInForeground()) {
                    googleCastDelegate.showIntroductoryOverlay(this, mediaRouteMenuItem);
                }
            }
        } catch (Exception ignore) {
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    protected void onResume() {
        // refresh the menu items to update the current state of google cast button
        invalidateOptionsMenu();
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        if (googleCastDelegate != null) {
            googleCastDelegate.removeCastStateListener(this);
        }
        super.onDestroy();
    }

    /**
     * @return True if screen needs to show the Google chrome un-casted button otherwise False.
     */
    public boolean showGoogleCastButton() {
        return false;
    }

    @Override
    public void onCastStateChanged(int newState) {
        if (mediaRouteMenuItem != null) {
            googleCastDelegate.showIntroductoryOverlay(this, mediaRouteMenuItem);
        }
        invalidateOptionsMenu();
    }
}

@@ -387,16 +387,53 @@
 elegate;
+%0Aimport org.edx.mobile.logger.Logger;
 %0A%0Aimport
@@ -717,16 +717,95 @@
 enuItem;
+%0A    private final Logger logger = new Logger(BaseAppActivity.class.getName());
 %0A%0A    @O
@@ -2268,17 +2268,47 @@
 ion 
-ignore) %7B
+e) %7B%0A            logger.error(e, true);
 %0A   
@@ -3002,32 +3002,528 @@
 int newState) %7B%0A
+        /* App throws %60IllegalArgumentException%60 when showing the Introductory Overlay in some cases.%0A         * Check the following issue for more details (still open).%0A         * Ref: https://issuetracker.google.com/issues/36191274%0A         * TODO: Replace the try-catch block with more appropriate logic / by updating the cast library%0A         * as part of the Jira story: https://openedx.atlassian.net/browse/LEARNER-7722%0A         */%0A        try %7B%0A            if (isInForeground()) %7B%0A        
         if (medi
@@ -3552,32 +3552,40 @@
 ) %7B%0A            
+ 
+       
 googleCastDelega
@@ -3638,34 +3638,50 @@
 uItem);%0A        
-%7D%0A
+        %7D%0A        
         invalida
@@ -3693,20 +3693,111 @@
 ionsMenu();%0A
+            %7D%0A        %7D catch (Exception e) %7B%0A            logger.error(e, true);%0A        %7D%0A
     %7D%0A%7D%0A

Fix crash while showing the cast IntroductoryOverlay - LEARNER-7717 - Ignore the crash by using try-catch block and print exception logs on Crashlytics, as this issue is already reported but still open. ref: https://issuetracker.google.com/issues/36191274