package org.forestguardian.View;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import okhttp3.Headers;
import retrofit2.adapter.rxjava2.Result;

import org.forestguardian.DataAccess.Local.SessionData;
import org.forestguardian.DataAccess.Local.User;
import org.forestguardian.DataAccess.WebServer.ForestGuardianService;
import org.forestguardian.Helpers.UserValidations;
import org.forestguardian.R;

import static junit.framework.Assert.assertEquals;

/**
 * Created by emma on 08/04/17.
 */

public class SignUpActivity  extends AppCompatActivity {

    @BindView(R.id.signup_btn) Button mSignUp;
    @BindView(R.id.signup_user) EditText mUsername;
    @BindView(R.id.signup_email) EditText mEmail;
    @BindView(R.id.signup_pass) EditText mPassword;
    @BindView(R.id.signup_pass_confirmation) EditText mPasswordConfirmation;

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_up);
        ButterKnife.bind(this);

        mSignUp.setOnClickListener(pView -> {

            // Get data from form.
            String email = mEmail.getText().toString();
            String username = mUsername.getText().toString();
            String pass = mPassword.getText().toString();
            String confirmation = mPasswordConfirmation.getText().toString();

            if (TextUtils.isEmpty(email)  || TextUtils.isEmpty(username) || TextUtils.isEmpty(pass)) {
                String error = "Please fill all fields.";
                Log.d(getLocalClassName(),error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                return;
            }

            if (!UserValidations.isEmailValid(email)) {
                String error = "Email is invalid.";
                Log.d(getLocalClassName(),error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                return;
            }

            // Check is password is present.
            if (!UserValidations.isPasswordValid(pass)) {
                String error = "Password should have at least 8 characters.";
                Log.d(getLocalClassName(),error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                return;
            }

            // Check if password and confirmation are equals.
            if (!pass.equals(confirmation)) {
                String error = "Password and confirmation should be equal.";
                Log.d(getLocalClassName(),error);
                Toast.makeText(this, error, Toast.LENGTH_LONG).show();
                return;
            }

            // Create Model
            User user = new User();
            user.setEmail(email);
            user.setUsername(username);
            user.setPassword(pass);
            user.setPasswordConfirmation(confirmation);

            // Send Login Request
            Observable<Result<SessionData>> sessionService = ForestGuardianService.global().service().signUp(user);
            sessionService.subscribeOn(Schedulers.newThread())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(pSessionDataResult -> {

                        if ( pSessionDataResult.isError() ){
                            /* TODO: Handle authentication error case. Review also pSessionDataResult.response().isSuccessful() case
                            * Check for error messages are ready for user viewing.
                            * */
                            Log.e( getLocalClassName(), pSessionDataResult.error().getMessage() );
                            Toast.makeText(this, "Problems with online service..." , Toast.LENGTH_LONG ).show();
                            return;
                        }

                        if ( !pSessionDataResult.response().isSuccessful() ){
                            Log.e( getLocalClassName(), "Problem processing request." );
                            Toast.makeText(this, "Problem processing request.", Toast.LENGTH_LONG ).show();
                            return;
                        }

                        Headers authHeaders = pSessionDataResult.response().headers();
                        String accessToken = authHeaders.get("Access-Token");

                        User authenticatedUser = pSessionDataResult.response().body().getUser();
                        authenticatedUser.setToken(accessToken);

                        Log.e("Current User", authenticatedUser.getEmail());
                        assertEquals(authenticatedUser.getEmail(), user.getEmail());

                        //Uncomment addApiAuthorizationHeader() when ApiAuthorization feature is enabled from backend.
                        //ForestGuardianService.global().addApiAuthorizationHeader();

                        //Add token to authenticate future requests.
                        ForestGuardianService.global().addAuthenticationHeaders(authenticatedUser.getEmail(), accessToken);

                        Toast.makeText(this, "Welcome!", Toast.LENGTH_SHORT).show();

                        Intent intent = new Intent(getApplicationContext(),MapActivity.class);
                        startActivity(intent);
                        finish();
                    });

        });
    }
}

@@ -1755,24 +1755,67 @@
 oString();%0A%0A
+            /* Check Local validations */%0A%0A
             
@@ -2395,53 +2395,8 @@
  %7D%0A%0A
-            // Check is password is present.%0A
     
@@ -2691,70 +2691,8 @@
  %7D%0A%0A
-            // Check if password and confirmation are equals.%0A
     
@@ -3529,24 +3529,95 @@
 esult -%3E %7B%0A%0A
+                        /* Check web service response validations. */%0A%0A
             
@@ -3739,183 +3739,8 @@
 ase.
- Review also pSessionDataResult.response().isSuccessful() case%0A                            * Check for error messages are ready for user viewing.%0A                            *
  */%0A
@@ -4088,24 +4088,111 @@
 essful() )%7B%0A
+                            /* Check for error messages are ready for user viewing. */%0A
             
@@ -4431,32 +4431,108 @@
              %7D%0A%0A
+                        // Save authentication headers for future requests.%0A
                 
@@ -4680,17 +4680,16 @@
 oken%22);%0A
-%0A
         
@@ -4834,33 +4834,32 @@
 n(accessToken);%0A
-%0A
                 
@@ -4870,96 +4870,62 @@
     
-Log.e(%22Current User%22, authenticatedUser.getEmail());%0A                        assertEqual
+ForestGuardianService.global().addAuthenticationHeader
 s(au
@@ -4956,23 +4956,19 @@
 (), 
-user.getEmail()
+accessToken
 );%0A%0A
@@ -4993,16 +4993,17 @@
       //
+ 
 Uncommen
@@ -5113,16 +5113,17 @@
       //
+ 
 ForestGu
@@ -5203,133 +5203,37 @@
     
-//Add token to authenticate future requests.%0A                        ForestGuardianService.global().addAuthenticationHeaders(
+Log.e(%22Authenticated User:%22, 
 auth
@@ -5252,37 +5252,24 @@
 r.getEmail()
-, accessToken
 );%0A%0A        
@@ -5342,24 +5342,69 @@
 T).show();%0A%0A
+                        // Load MapActivity.%0A
             

Add SignUpActivity comments.