package com.drabarz.karola.raillearn.login;

import android.content.Intent;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

import com.drabarz.karola.raillearn.R;
import com.drabarz.karola.raillearn.model.Token;
import com.drabarz.karola.raillearn.service.RailLearnLogInUserApi;
import com.drabarz.karola.raillearn.service.ServiceFactory;
import com.drabarz.karola.raillearn.trip.list.TripsActivity;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class LoginActivity extends ActionBarActivity {

    private static final String TOKEN = "token";
    private CallbackManager callbackManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (isLoggedIn()) {
            TripsActivity.start(LoginActivity.this);
        } else {
            showLogInForm();
        }
    }

    private boolean isLoggedIn() {
        return PreferenceManager.getDefaultSharedPreferences(this).contains(TOKEN);
    }

    private void showLogInForm() {
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();

        setContentView(R.layout.login_activity);

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("email");

        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                RailLearnLogInUserApi retrofitService = ServiceFactory.createRetrofitService(RailLearnLogInUserApi.class, getString(R.string.service_endpoint));
                retrofitService.logInUser(loginResult.getAccessToken().getToken())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Action1<Token>() {
                                       @Override
                                       public void call(Token token) {
                                           onLogInSuccess(token.getToken());
                                       }
                                   }, new Action1<Throwable>() {
                                       @Override
                                       public void call(Throwable throwable) {
                                           Log.e("LoginActivity", throwable.getMessage(), throwable);
                                       }
                                   }
                        );
            }

            @Override
            public void onCancel() {
                // Do nothing
            }

            @Override
            public void onError(FacebookException e) {
                Log.e("LoginActivity", "Facebook login error: " + e, e);
            }
        });
    }

    private void onLogInSuccess(String token) {
        PreferenceManager.getDefaultSharedPreferences(this).edit().putString(TOKEN, token).commit();
        TripsActivity.start(LoginActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
}

@@ -1708,17 +1708,16 @@
 mail%22);%0A
-%0A
         
@@ -1883,25 +1883,24 @@
 sult) %7B%0A
-%0A
         
         
@@ -1883,32 +1883,854 @@
 sult) %7B%0A        
+        onFacebookLogInSuccess(loginResult.getAccessToken().getToken());%0A            %7D%0A%0A            @Override%0A            public void onCancel() %7B%0A                // Do nothing%0A            %7D%0A%0A            @Override%0A            public void onError(FacebookException e) %7B%0A                Log.e(%22LoginActivity%22, %22Facebook login error: %22 + e, e);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void onLogInSuccess(String token) %7B%0A        PreferenceManager.getDefaultSharedPreferences(this).edit().putString(TOKEN, token).commit();%0A        TripsActivity.start(LoginActivity.this);%0A    %7D%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        callbackManager.onActivityResult(requestCode, resultCode, data);%0A    %7D%0A%0A    private void onFacebookLogInSuccess(String facebookToken) %7B%0A
         RailLear
@@ -2874,24 +2874,16 @@
         
-      
-  
 retrofit
@@ -2904,57 +2904,23 @@
 ser(
-loginResult.getAccessToken().getToken())%0A        
+facebookToken)%0A
     
@@ -2957,32 +2957,24 @@
 ulers.io())%0A
-        
             
@@ -3032,32 +3032,24 @@
             
-        
 .subscribe(n
@@ -3097,32 +3097,24 @@
             
-     
-   
 @Override%0A  
@@ -3107,32 +3107,24 @@
   @Override%0A
-        
             
@@ -3209,24 +3209,16 @@
         
-  
-      
 onLogInS
@@ -3274,34 +3274,18 @@
         
-       
- 
 %7D%0A
-        
         
@@ -3360,32 +3360,24 @@
             
-    
-    
 @Override%0A  
@@ -3370,32 +3370,24 @@
   @Override%0A
-        
             
@@ -3476,32 +3476,24 @@
             
-        
 Log.e(%22Login
@@ -3570,34 +3570,18 @@
         
-      
-  %7D%0A        
+%7D%0A
         
@@ -3621,694 +3621,8 @@
     
-        );%0A            %7D%0A%0A            @Override%0A            public void onCancel() %7B%0A                // Do nothing%0A            %7D%0A%0A            @Override%0A            public void onError(FacebookException e) %7B%0A                Log.e(%22LoginActivity%22, %22Facebook login error: %22 + e, e);%0A            %7D%0A        %7D);%0A    %7D%0A%0A    private void onLogInSuccess(String token) %7B%0A        PreferenceManager.getDefaultSharedPreferences(this).edit().putString(TOKEN, token).commit();%0A        TripsActivity.start(LoginActivity.this);%0A    %7D%0A%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        callbackManager.onActivityResult(requestCode, resultCode, data
 );%0A 

Create onFacebookLogInSuccess method.