package in.iamkelv.balances.wizards.setupwizard;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

import com.dd.processbutton.iml.ActionProcessButton;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.codepond.wizardroid.WizardStep;
import org.json.JSONException;
import org.json.JSONObject;

import in.iamkelv.balances.R;

public class SetupWizardStepLogin extends WizardStep {

    // Member variables
    private String mUsername = "";
    private String mPassword = "";
    private String mBaseUrl = "https://balances.iamkelv.in/";

    // Methods

    public SetupWizardStepLogin() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.setup_wizard_step_login, container, false);

        final EditText txtUsername = (EditText) v.findViewById(R.id.txtLoginUsername);
        final EditText txtPassword = (EditText) v.findViewById(R.id.txtLoginPassword);
        final ActionProcessButton btnSignIn = (ActionProcessButton) v.findViewById(R.id.btnSignIn);

        btnSignIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Change view properties
                btnSignIn.setEnabled(false);
                btnSignIn.setMode(ActionProcessButton.Mode.ENDLESS);
                btnSignIn.setProgress(1);

                // Assign variables
                mUsername = txtUsername.getText().toString();
                mPassword = txtPassword.getText().toString();

                // Create parameters
                RequestParams params = new RequestParams();
                params.put("username", mUsername);
                params.put("password", mPassword);
                // Create headers
                Header[] header = {
                        new BasicHeader("Content-Type", "application/x-www-form-urlencoded")
                };
                // Create client
                AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
                client.post(null, mBaseUrl + "auth", header, params, "application/x-www-form-urlencoded", new JsonHttpResponseHandler() {
                    @Override
                    public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                            // Change view properties
                            btnSignIn.setProgress(0);
                            btnSignIn.setEnabled(true);
                            new AlertDialog.Builder(getActivity())
                                    .setMessage(response.toString())
                                    .setCancelable(false)
                                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int id) {
                                        }
                                    }).create().show();
                    }
//
                    @Override
                    public void onFailure(int statusCode, Header[] headers, Throwable e, JSONObject errorResponse) {
                        // Change view properties
                        btnSignIn.setProgress(0);
                        btnSignIn.setEnabled(true);
                        try {
                            new AlertDialog.Builder(getActivity())
                                    .setMessage("Error - " + errorResponse.getString("message"))
                                    .setCancelable(false)
                                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int id) {
                                        }
                                    }).create().show();
                        } catch (JSONException e1) {
                            new AlertDialog.Builder(getActivity())
                                    .setMessage("Error - " + "An unknown error has occurred.")
                                    .setCancelable(false)
                                    .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                                        @Override
                                        public void onClick(DialogInterface dialog, int id) {
                                        }
                                    }).create().show();
                        }
                    }
                });
            }
        });

        return v;
    }

}
@@ -2723,36 +2723,32 @@
                 
-    
 // Change view p
@@ -2773,36 +2773,32 @@
                 
-    
 btnSignIn.setPro
@@ -2807,602 +2807,11 @@
 ess(
-0);%0A                            btnSignIn.setEnabled(true);%0A                            new AlertDialog.Builder(getActivity())%0A                                    .setMessage(response.toString())%0A                                    .setCancelable(false)%0A                                    .setPositiveButton(%22ok%22, new DialogInterface.OnClickListener() %7B%0A                                        @Override%0A                                        public void onClick(DialogInterface dialog, int id) %7B%0A                                        %7D%0A                                    %7D).create().show(
+100
 );%0A 

Add button response to successful authentication