package com.example.pierrot.jsonvolley;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import com.example.pierrot.jsonvolley.app.AppController;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ProgressDialog;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
import android.widget.EditText;
import android.content.Intent;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;

public class MainActivity extends AppCompatActivity {

    private static String TAG = MainActivity.class.getSimpleName();
    private Button connexion;
    private EditText password,username;
    private String urlJsonObj = "";
    private String usernameText = "";
    private String passwordText = "";
    final String EXTRA_ID = "";
    final String EXTRA_USERNAME = "";
    final String EXTRA_NOM = "";
    final String EXTRA_PRENOM = "";
    final String EXTRA_EMAIL = "";


    // Progress dialog
    private ProgressDialog pDialog;


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

        connexion = (Button) findViewById(R.id.connexion);
        password = (EditText) findViewById(R.id.password);
        username = (EditText) findViewById(R.id.username);

        pDialog = new ProgressDialog(this);
        pDialog.setMessage("Connexion en cours");
        pDialog.setCancelable(false);

        connexion.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // making json object request
                connexionRequest();
            }
        });

    }

    /**
     * Method to make json object request where json response starts wtih {
     * */
    private void connexionRequest() {

        showpDialog();
        usernameText = username.getText().toString();
        passwordText = password.getText().toString();

        urlJsonObj = "http://gsb-applifrais-hvcode.esy.es/web/api/users/"+ usernameText +"/passwords/"+ passwordText +".json";

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
                urlJsonObj, null, new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                Log.d(TAG, response.toString());

                try {

                    final String id = response.getString("id");

                    Intent intent = new Intent(MainActivity.this, loginVisiteur.class);
                    intent.putExtra(EXTRA_ID, id);
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

                hidepDialog();
            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplicationContext(),
                        error.getMessage(), Toast.LENGTH_SHORT).show();
                // hide the progress dialog
                hidepDialog();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(jsonObjReq);
    }

    private void showpDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    }

    private void hidepDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    }
}