package com.epson.iprint.storage.gdrivev3;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
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 = this.mGooglePlayServiceDialog;
        if (dialog != null && dialog.isShowing()) {
            this.mGooglePlayServiceDialog.dismiss();
        }
        GoogleApiAvailability instance = GoogleApiAvailability.getInstance();
        int isGooglePlayServicesAvailable = instance.isGooglePlayServicesAvailable(activity);
        if (isGooglePlayServicesAvailable == 0) {
            return false;
        }
        if (instance.isUserResolvableError(isGooglePlayServicesAvailable)) {
            this.mGooglePlayServiceDialog = instance.getErrorDialog(activity, isGooglePlayServicesAvailable, i, new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialogInterface) {
                    singInCancelNotifier.playServiceInstallCancel();
                }
            });
            this.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 = this.mGooglePlayServiceDialog;
        if (dialog != null && dialog.isShowing()) {
            this.mGooglePlayServiceDialog.dismiss();
        }
    }
}
