package rs.elfak.mosis.marko.discounthunt;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;

import org.json.JSONException;
import org.json.JSONObject;

import rs.elfak.mosis.marko.discounthunt.api.endpoints.UserEndpoint;
import rs.elfak.mosis.marko.discounthunt.api.endpoints.UserSessionEndpoint;

/**
 * A login screen that offers login via username/password.
 */
public class SignupActivity extends SessionActivity {

    // UI references.
    private EditText mFirstNameView;
    private EditText mLastNameView;
    private EditText mPhoneView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        initViews();

        mFirstNameView = (EditText) findViewById(R.id.signup_first_name);
        mLastNameView = (EditText) findViewById(R.id.signup_last_name);
        mPhoneView = (EditText) findViewById(R.id.signup_phone);

        Button signupButton = (Button) findViewById(R.id.signup);
        signupButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                attemptSignup();
            }
        });
    }

    /**
     * Attempts to sign in or register the account specified by the login form.
     * If there are form errors (invalid username, missing fields, etc.), the
     * errors are presented and no actual login attempt is made.
     */
    private void attemptSignup() {

        // Reset errors.
        mUsernameView.setError(null);
        mPasswordView.setError(null);

        // Store values at the time of the login attempt.
        String username = mUsernameView.getText().toString();
        String password = mPasswordView.getText().toString();
        String firstName = mFirstNameView.getText().toString();
        String lastName = mLastNameView.getText().toString();
        String phone = mPhoneView.getText().toString();

        boolean cancel = false;
        View focusView = null;

        // Check for a valid password, if the user entered one.
        if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
            mPasswordView.setError(getString(R.string.error_invalid_password));
            focusView = mPasswordView;
            cancel = true;
        }

        // Check for a valid username address.
        if (TextUtils.isEmpty(username)) {
            mUsernameView.setError(getString(R.string.error_field_required));
            focusView = mUsernameView;
            cancel = true;
        } else if (!isUsernameValid(username)) {
            mUsernameView.setError(getString(R.string.error_invalid_username));
            focusView = mUsernameView;
            cancel = true;
        }

        if (cancel) {
            // There was an error; don't attempt login and focus the first
            // form field with an error.
            focusView.requestFocus();
        } else {
            // Show a progress spinner, and kick off a background task to
            // perform the user login attempt.
            showProgress(true);
            try {
                JSONObject userJsonObject = new JSONObject();
                userJsonObject.put("username", username);
                userJsonObject.put("password", password);
                userJsonObject.put("first_name", firstName);
                userJsonObject.put("last_name", lastName);
                userJsonObject.put("phone", phone);
                createUser(userJsonObject);
            }catch (JSONException ex) {
                showError();
            }
        }
    }

    private void createUser(final JSONObject userJsonObject) {
        UserEndpoint userEndpoint = new UserEndpoint();
        userEndpoint.post(userJsonObject,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        createSession(userJsonObject);
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        showProgress(false);
                        showError();
                    }
                }
        );
    }
}


@@ -159,16 +159,59 @@
 getApi;%0A
+import android.bluetooth.BluetoothAdapter;%0A
 import a
@@ -676,16 +676,43 @@
 Error;%0A%0A
+import org.json.JSONArray;%0A
 import o
@@ -4078,24 +4078,111 @@
 e%22, phone);%0A
+                userJsonObject.put(%22user_devices_attributes%22, userDevicesJsonArray());%0A
             
@@ -4230,19 +4230,16 @@
    %7D
+ 
 catch (
-JSON
 Exce
@@ -4291,27 +4291,662 @@
         
-%7D%0A        %7D
+    finish();%0A            %7D%0A        %7D%0A    %7D%0A%0A    private String getBlueToothMAC() throws Exception %7B%0A        BluetoothAdapter mAdapter = BluetoothAdapter.getDefaultAdapter();%0A        if (mAdapter != null) %7B%0A            return mAdapter.getAddress();%0A        %7D else %7B%0A            throw new Exception(%22BlueTooth is not supported!%22);%0A        %7D%0A    %7D%0A%0A    private JSONArray userDevicesJsonArray() throws Exception %7B%0A        JSONArray jsonArray = new JSONArray();%0A        JSONObject deviceJsonObject = new JSONObject();%0A        deviceJsonObject.put(%22hardware_uuid%22, getBlueToothMAC());%0A        jsonArray.put(deviceJsonObject);%0A        return jsonArray;
 %0A    %7D%0A%0A

Save user device on signup