package me.mattlogan.stravaflow.ui.fragment;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.mattlogan.stravaflow.R;
import me.mattlogan.stravaflow.api.StravaApi;
import me.mattlogan.stravaflow.api.model.AuthResponse;
import me.mattlogan.stravaflow.api.util.ApiUtils;
import me.mattlogan.stravaflow.ui.activity.ActivitiesActivity;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

public class AuthFragment extends BaseFragment {

    @InjectView(R.id.auth_webview) WebView authWebView;

    private StravaApi stravaApi;

    public static AuthFragment newInstance() {
        return new AuthFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.auth, container, false);
        ButterKnife.inject(this, view);
        setupWebView();
        return view;
    }

    private void setupWebView() {
        String clientId = getResources().getString(R.string.client_id);
        String url = "https://www.strava.com/oauth/authorize?" +
                "client_id=" + clientId +
                "&response_type=code" +
                "&redirect_uri=http://localhost" +
                "&approval_prompt=force";

        authWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Uri uri = Uri.parse(url);
                if (uri.getHost().equals("localhost")) {
                    if (uri.getQueryParameterNames().contains("error")) {
                        handleAccessDenied();
                    } else {
                        finishAuth(uri.getQueryParameter("code"));
                        return true;
                    }
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });

        authWebView.loadUrl(url);
    }

    @Override
    protected String getTitle() {
        return getString(R.string.app_name);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        stravaApi = getStravaFlowApp(activity).getStravaApi();
    }

    private void finishAuth(String code) {
        stravaApi.authenticate(ApiUtils.getAuthParams(getActivity(), code),
                new Callback<AuthResponse>() {

                    @Override
                    public void success(AuthResponse authResponse, Response response) {
                        if (isAdded()) {
                            Activity activity = getActivity();
                            getStravaFlowApp(activity).saveAccessToken(authResponse.getAccessToken());
                            Intent intent = new Intent(activity, ActivitiesActivity.class);
                            activity.startActivity(intent);
                        }
                    }

                    @Override
                    public void failure(RetrofitError error) {

                    }
                });
    }

    private void handleAccessDenied() {
    }
}

@@ -2865,24 +2865,55 @@
     
-if (isAdded()) %7B
+saveAccessToken(authResponse.getAccessToken());
 %0A   
@@ -2937,35 +2937,24 @@
     
-    
+launch
 Activit
-y activity = get
+ies
 Acti
@@ -2981,24 +2981,27 @@
         
+%7D%0A%0A
         
 getStrav
@@ -2996,83 +2996,8 @@
     
-getStravaFlowApp(activity).saveAccessToken(authResponse.getAccessToken());%0A
     
@@ -2996,32 +2996,42 @@
                 
+@Override%0A
                 
@@ -3034,71 +3034,55 @@
     
-Intent intent = new Intent(activity, ActivitiesActivity.class);
+    public void failure(RetrofitError error) %7B%0A
 %0A   
@@ -3098,24 +3098,26 @@
         
+%7D%0A
         
 activity
@@ -3112,48 +3112,16 @@
     
-activity.startActivity(intent);%0A
         
     
@@ -3120,78 +3120,264 @@
     
+%7D);%0A
     
+%7D%0A%0A
     
-    
+private void handleAccessDenied() %7B%0A
     %7D%0A
+%0A
     
-                %7D%0A%0A                    @Override
+private void saveAccessToken(String accessToken) %7B%0A        Activity activity = getActivity();%0A        if (activity != null) %7B%0A            getStravaFlowApp(activity).saveAccessToken(accessToken);
 %0A   
@@ -3385,72 +3385,71 @@
     
- 
+%7D%0A
     
+%7D%0A%0A
     
-   public void failure(RetrofitError error
+private void launchActivitiesActivity(
 ) %7B%0A
-%0A
         
     
@@ -3448,88 +3448,204 @@
     
-            %7D%0A                %7D
+Activity activity = getActivity();%0A        if (activity != null) %7B%0A            Intent intent = new Intent(activity, ActivitiesActivity.class
 );%0A    
-%7D%0A%0A
+ 
     
-private void handleAccessDenied() %7B
+   activity.startActivity(intent);%0A        %7D
 %0A   

refactor AuthFragment