package com.epson.iprint.storage.gdrivev3;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import com.epson.iprint.storage.Network;
import com.epson.iprint.storage.OneButtonDialogFragment;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;

import java.util.Iterator;
import java.util.LinkedList;

import epson.print.R;

public class GoogleV3UploadSignInActivity extends AppCompatActivity implements ActivityWrapper, PlayServiceDialogManager.SingInCancelNotifier, OneButtonDialogFragment.DialogCallback, IprintGoogleSignIn.SignInListener {
    private static final String DIALOG_TAG_OFFLINE_ERROR = "offline_error";
    private static final String DIALOG_TAG_SIGN_IN_FAILED = "sign-in_failed";
    private static final String PARAM_KEY_CHECK_SERVICE_ONLY = "check-service_only";
    private static final String PARAM_KEY_FOR_DISCONNECT = "for-disconnect";
    private static final int REQUEST_CODE_CHECK_PLAY_SERVICE = 11;
    private static final int REQUEST_CODE_SIGN_IN = 10;
    private static final int REQUEST_CODE_SIGN_IN_ADD_SCOPE = 12;
    private boolean mActivityForeground;
    private boolean mCheckServiceOnly;
    private LinkedList<Runnable> mForegroundRunnableList;
    private IprintGoogleSignIn mIprintGoogleSignIn;
    private Network mMisc;
    private PlayServiceDialogManager mPlayServiceDialogManager;
    private IprintGoogleSignIn.StartActivityResultCallback mSignInStartActivityCallback;
    private boolean mStartForDisconnect;
    private boolean mWaitSingInProcess;

    @NonNull
    public Activity getActivity() {
        return this;
    }

    public int getPlayServiceRequestCode() {
        return 11;
    }

    public int getSingInAddScopeRequestCode() {
        return 12;
    }

    @NonNull
    public static Intent getDisconnectIntent(@NonNull Context context) {
        Intent intent = new Intent(context, GoogleV3UploadSignInActivity.class);
        intent.putExtra(PARAM_KEY_FOR_DISCONNECT, true);
        return intent;
    }

    @NonNull
    public static Intent getPlayServiceCheckIntent(@NonNull Context context) {
        Intent intent = new Intent(context, GoogleV3UploadSignInActivity.class);
        intent.putExtra(PARAM_KEY_CHECK_SERVICE_ONLY, true);
        return intent;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mPlayServiceDialogManager = new PlayServiceDialogManager();
        mMisc = new Network();
        mIprintGoogleSignIn = new IprintGoogleSignIn();
        mWaitSingInProcess = false;
        mActivityForeground = false;
        mForegroundRunnableList = new LinkedList<>();
        Intent intent = getIntent();
        mStartForDisconnect = intent.getBooleanExtra(PARAM_KEY_FOR_DISCONNECT, false);
        mCheckServiceOnly = intent.getBooleanExtra(PARAM_KEY_CHECK_SERVICE_ONLY, false);
    }

    private void showErrorDialog(final int i, final String str) {
        if (!this.mActivityForeground) {
            mForegroundRunnableList.addLast(new Runnable() {
                public void run() {
                    GoogleV3UploadSignInActivity.this.showErrorDialog(i, str);
                }
            });
        } else {
            OneButtonDialogFragment.newInstance(i, str).show(getSupportFragmentManager(), str);
        }
    }

    protected void onPostResume() {
        super.onPostResume();
        mMisc.selectSimpleAp(this, false);
        mActivityForeground = true;
        processForegroundList();
        if (!this.mMisc.isOnline(this)) {
            showErrorDialog(R.string.network_error_mes, DIALOG_TAG_OFFLINE_ERROR);
        } else if (!this.mPlayServiceDialogManager.checkPlayService(this, this)) {
            if (mCheckServiceOnly) {
                finishWithResult(true);
            } else if (!this.mWaitSingInProcess) {
                mWaitSingInProcess = true;
                if (mStartForDisconnect) {
                    mIprintGoogleSignIn.disconnectAccount(this, this);
                } else {
                    mIprintGoogleSignIn.startSignIn(this, this, IprintGoogleSignIn.OperationType.UPLOAD);
                }
            }
        }
    }

    private void processForegroundList() {
        Iterator it = mForegroundRunnableList.iterator();
        while (it.hasNext()) {
            it.remove();
            ((Runnable) it.next()).run();
        }
    }

    protected void onPause() {
        super.onPause();
        mPlayServiceDialogManager.onActivityPaused();
        mActivityForeground = false;
    }

    public void requestSignIn(@NonNull Intent intent, @NonNull IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback) {
        mSignInStartActivityCallback = startActivityResultCallback;
        startActivityForResult(intent, 10);
    }

    public void playServiceInstallCancel() {
        if (mCheckServiceOnly) {
            finishWithResult(false);
        } else {
            showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
        }
    }

    public void buttonPressed(@Nullable String str) {
        if (str != null) {
            char c = 65535;
            int hashCode = str.hashCode();
            if (hashCode != -683930452) {
                if (hashCode == 366991495 && str.equals(DIALOG_TAG_SIGN_IN_FAILED)) {
                    c = 0;
                }
            } else if (str.equals(DIALOG_TAG_OFFLINE_ERROR)) {
                c = 1;
            }
            switch (c) {
                case 0:
                    finishWithResult(false);
                    return;
                case 1:
                    startActivity(new Intent("android.settings.WIRELESS_SETTINGS"));
                    finishWithResult(false);
                    return;
                default:
                    return;
            }
        }
    }

    protected void onActivityResult(int i, int i2, Intent intent) {
        switch (i) {
            case 10:
                IprintGoogleSignIn.StartActivityResultCallback startActivityResultCallback = mSignInStartActivityCallback;
                if (startActivityResultCallback != null) {
                    startActivityResultCallback.onActivityResult(i2, intent);
                    mSignInStartActivityCallback = null;
                    return;
                }
                return;
            case 11:
                return;
            case 12:
                if (i2 == -1) {
                    finishWithResult(true);
                    return;
                } else {
                    showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
                    return;
                }
            default:
                return;
        }
    }

    public void onSignInComplete(boolean z, GoogleSignInAccount googleSignInAccount) {
        if (z) {
            finishWithResult(true);
        } else {
            showErrorDialog(R.string.authenticate_error_mes, DIALOG_TAG_SIGN_IN_FAILED);
        }
    }

    public void onDisconnectCompleted() {
        finishWithResult(true);
    }

    private void finishWithResult(boolean z) {
        GoogleV3UploadClient.setSingInValue(z);
        setResult(z ? -1 : 0);
        finish();
    }
}
