package epson.print.storage.gdrivev3;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;

import com.google.android.gms.common.GoogleApiAvailability;

public class PlayServiceDialogManager {
    private Dialog mGooglePlayServiceDialog;

    public interface SingInCancelNotifier {
        void playServiceInstallCancel();
    }

    private boolean checkPlayService(@NonNull Activity activity, int i, @NonNull final SingInCancelNotifier singInCancelNotifier) {
        Dialog dialog = mGooglePlayServiceDialog;
        if (dialog != null && dialog.isShowing()) {
            mGooglePlayServiceDialog.dismiss();
        }
        GoogleApiAvailability instance = GoogleApiAvailability.getInstance();
        int isGooglePlayServicesAvailable = instance.isGooglePlayServicesAvailable(activity);
        if (isGooglePlayServicesAvailable == 0) {
            return false;
        }
        if (instance.isUserResolvableError(isGooglePlayServicesAvailable)) {
            mGooglePlayServiceDialog = instance.getErrorDialog(activity, isGooglePlayServicesAvailable, i, new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialogInterface) {
                    singInCancelNotifier.playServiceInstallCancel();
                }
            });
            mGooglePlayServiceDialog.show();
            return true;
        }
        singInCancelNotifier.playServiceInstallCancel();
        return false;
    }

    public boolean checkPlayService(@NonNull ActivityWrapper activityWrapper, @NonNull SingInCancelNotifier singInCancelNotifier) {
        return checkPlayService(activityWrapper.getActivity(), activityWrapper.getPlayServiceRequestCode(), singInCancelNotifier);
    }

    public void onActivityPaused() {
        Dialog dialog = mGooglePlayServiceDialog;
        if (dialog != null && dialog.isShowing()) {
            mGooglePlayServiceDialog.dismiss();
        }
    }
}
