package epson.print.storage.box;

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



import com.box.androidsdk.content.auth.BoxAuthentication;
import com.epson.iprint.storage.StorageSignInActivity;

import java.lang.ref.WeakReference;



public class BoxNetSignInActivity extends StorageSignInActivity implements BoxAuthentication.AuthListener {
    private static final String PARAM_KEY_LOGOUT = "logout";
    private BoxNetClient mBoxClient;
    private boolean mModeLogout;

    public StorageSignInActivity.BasicSignIn getBasicSignIn() {
        return null;
    }

    public void onBackPressed() {
    }

    @NonNull
    public static Intent getLogoutIntent(@NonNull Context context) {
        Intent intent = new Intent(context, BoxNetSignInActivity.class);
        intent.putExtra(PARAM_KEY_LOGOUT, true);
        return intent;
    }

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        mModeLogout = getMode(getIntent());
        if (mModeLogout) {
            setContentView((int) R.layout.storage_progress);
            findViewById(R.id.messageText).setVisibility(8);
            findViewById(R.id.cancelButton).setVisibility(8);
        }
        mBoxClient = BoxNetClient.getInstance();
        mBoxClient.setSessionAuthListener(this);
        if (mModeLogout) {
            startLogoutTask();
        } else {
            mBoxClient.authenticate();
        }
    }

    private boolean getMode(@Nullable Intent intent) {
        if (intent == null) {
            return false;
        }
        return intent.getBooleanExtra(PARAM_KEY_LOGOUT, false);
    }

    private void startLogoutTask() {
        if (mBoxClient == null) {
            finish();
        }
        new LogoutTask(this, mBoxClient).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, new Void[0]);
    }

    public void onRefreshed(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo) {
        mBoxClient.setLoginStatus(true);
        finish();
    }

    public void onAuthCreated(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo) {
        mBoxClient.setLoginStatus(true);
        finish();
    }

    public void onAuthFailure(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo, Exception exc) {
        mBoxClient.setLoginStatus(false);
        showLoginErrorAndFinish();
    }

    public void onLoggedOut(BoxAuthentication.BoxAuthenticationInfo boxAuthenticationInfo, Exception exc) {
        mBoxClient.setLoginStatus(false);
        finish();
    }

    private static class LogoutTask extends AsyncTask<Void, Void, Void> {
        private final WeakReference<Activity> mActivityReference;
        private final BoxNetClient mBoxNetClient;

        LogoutTask(Activity activity, @NonNull BoxNetClient boxNetClient) {
            mActivityReference = new WeakReference<>(activity);
            mBoxNetClient = boxNetClient;
        }

        protected Void doInBackground(Void... voidArr) {
            mBoxNetClient.boxLogout();
            return null;
        }
    }
}
