package com.epson.iprint.storage.onedrive;

import android.content.Context;
import android.content.Intent;
import android.widget.Button;

import com.epson.iprint.storage.StorageProcessActivity;
import com.epson.iprint.storage.StorageProcessUploadActivity;
import com.epson.iprint.storage.StorageServiceClient;
import com.onedrive.sdk.extensions.IOneDriveClient;

import epson.print.R;

public class OneDriveStorageProcessUploadActivity extends StorageProcessUploadActivity {
    protected static final String TAG = "OneDriveStorageProcessUploadActivity";
    boolean mExpired;
    private OneDriveClient mStorageServiceClient;

    protected void onStart() {
        super.onStart();
        mExpired = false;
        checkSession();
    }

    protected void onResume() {
        super.onResume();
        mExpired = false;
        OneDriveClient oneDriveClient = mStorageServiceClient;
        if (oneDriveClient != null && oneDriveClient.getUploadProcessError().equals(StorageServiceClient.ProcessError.RETRY)) {
            mExpired = true;
            checkSession();
        }
    }

    private void checkSession() {
        if (isConnected()) {
            mStorageServiceClient = (OneDriveClient) getStorageClient();
            if (mStorageServiceClient.isSignedIn(this) && mStorageServiceClient.isSessionExpired()) {
                new Thread() {
                    public void run() {
                        super.run();
                        OneDriveStorageProcessUploadActivity.this.mStorageServiceClient.getOneDriveClient(OneDriveStorageProcessUploadActivity.this, new OneDriveAuthenticator.OnOneDriveAuthenticatorListener() {
                            public void onNotifyOneDriveClient(IOneDriveClient iOneDriveClient) {
                                OneDriveStorageProcessUploadActivity.this.findViewById(R.id.folder_content_layout_for_upload).setVisibility(0);
                                if (iOneDriveClient == null) {
                                    OneDriveStorageProcessUploadActivity.this.mStorageServiceClient.revokeSignedInData(OneDriveStorageProcessUploadActivity.this);
                                    OneDriveStorageProcessUploadActivity.this.updateSignInStatus();
                                } else if (OneDriveStorageProcessUploadActivity.this.mExpired) {
                                    OneDriveStorageProcessUploadActivity.this.mExpired = false;
                                    OneDriveStorageProcessUploadActivity.this.mStorageServiceClient.resetUploadProcessError();
                                    ((Button) OneDriveStorageProcessUploadActivity.this.findViewById(R.id.save_button)).performClick();
                                }
                            }
                        });
                    }
                }.start();
                findViewById(R.id.folder_content_layout_for_upload).setVisibility(4);
            }
        }
    }

    public static Intent getProcessIntent(Context context, String str, StorageProcessActivity.ProcessType processType) {
        Intent intent = new Intent(context, OneDriveStorageProcessUploadActivity.class);
        setCommonExtra(intent, str);
        return intent;
    }
}
