package io.github.plastix.forage.ui.login;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;

import butterknife.BindView;
import butterknife.OnClick;
import io.github.plastix.forage.ForageApplication;
import io.github.plastix.forage.R;
import io.github.plastix.forage.data.api.ApiConstants;
import io.github.plastix.forage.ui.base.PresenterActivity;
import io.github.plastix.forage.util.ActivityUtils;
import timber.log.Timber;

// TODO Log out support?
// Update button if we have stored OAuth tokens?
public class LoginActivity extends PresenterActivity<LoginPresenter, LoginView> implements LoginView {

    @BindView(R.id.login_coordinator_layout)
    CoordinatorLayout coordinatorLayout;

    @BindView(R.id.login_toolbar)
    Toolbar toolbar;

    @BindView(R.id.login_loading_spinner)
    ProgressBar progressBar;

    @BindView(R.id.login_button)
    Button loginButton;

    public static Intent newIntent(Context context) {
        return new Intent(context, LoginActivity.class);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        injectDependencies();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setSupportActionBar(toolbar);
        ActivityUtils.setSupportActionBarBack(getDelegate());
    }

    private void injectDependencies() {
        ForageApplication.getComponent(this)
                .plus(new LoginModule(this)).injectTo(this);
    }

    @Override
    public void openBrowser(String authUrl) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl));
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        startActivity(intent);
    }

    @Override
    public void onErrorRequestToken() {
        makeErrorSnackbar(R.string.login_error_request_token);
    }

    private void makeErrorSnackbar(@StringRes int resID) {
        stopLoading();
        Snackbar.make(coordinatorLayout, resID, Snackbar.LENGTH_LONG)
                .setAction(R.string.login_error_retry, view -> {
                    loginButtonClick();
                })
                .show();
    }

    @Override
    public void stopLoading() {
        loginButton.setEnabled(true);
        progressBar.setVisibility(View.INVISIBLE);
    }

    @OnClick(R.id.login_button)
    public void loginButtonClick() {
        presenter.startOAuth();
    }

    @Override
    public void onErrorAccessToken() {
        makeErrorSnackbar(R.string.login_error_access_token);
    }

    @Override
    public void onErrorNoInternet() {
        Timber.e("No internet!");
        makeErrorSnackbar(R.string.login_error_no_internet);
    }

    @Override
    public void onAuthSuccess() {
        stopLoading();
        Snackbar.make(coordinatorLayout, R.string.login_authentication_success, Snackbar.LENGTH_LONG)
                .show();
    }

    @Override
    public void showLoading() {
        loginButton.setEnabled(false);
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        Uri uri = intent.getData();

        if (uri != null && uri.toString().startsWith(ApiConstants.OAUTH_CALLBACK)) {
            presenter.oauthCallback(uri);
        }
    }
}

@@ -816,82 +816,8 @@
 r;%0A%0A
-// TODO Log out support?%0A// Update button if we have stored OAuth tokens?%0A
 publ
@@ -1827,24 +1827,212 @@
  authUrl) %7B%0A
+        // Hide the progress bar when the we're in the the web browsers%0A        // Users can navigate out of the browser and come back to Forage without logging in%0A        stopLoading();%0A%0A
         Inte
@@ -2183,32 +2183,174 @@
 intent);%0A    %7D%0A%0A
+    @Override%0A    public void stopLoading() %7B%0A        loginButton.setEnabled(true);%0A        progressBar.setVisibility(View.INVISIBLE);%0A    %7D%0A%0A
     @Override%0A  
@@ -2769,150 +2769,8 @@
  %7D%0A%0A
-    @Override%0A    public void stopLoading() %7B%0A        loginButton.setEnabled(true);%0A        progressBar.setVisibility(View.INVISIBLE);%0A    %7D%0A%0A
     

Stop loading when the browser is opened in LoginActivity