package com.example.pierrot.jsonvolley;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.pierrot.jsonvolley.app.AppController;

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

/**
 * Created by Pierrot on 27/04/2016.
 */
public class loginVisiteur extends AppCompatActivity {

    private static String TAG = MainActivity.class.getSimpleName();
    private TextView information;
    final String EXTRA_ID = "";
    private String txtReponse = "";
    private Button recherche;
    private EditText mois,annee;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_visiteur);

        Intent intent = getIntent();
        information = (TextView) findViewById(R.id.information);
        recherche = (Button) findViewById(R.id.recherche);
        mois = (EditText) findViewById(R.id.mois);
        annee = (EditText) findViewById(R.id.annee);

            String id = intent.getStringExtra(EXTRA_ID);
            String moisText = mois.getText().toString();
            String anneeText = annee.getText().toString();

        recherche.setOnClickListener(new View.OnClickListener() {

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


            }

    private void connexionRequest() {

        Intent intent = getIntent();
        information = (TextView) findViewById(R.id.information);

        mois = (EditText) findViewById(R.id.mois);
        annee = (EditText) findViewById(R.id.annee);

        String id = intent.getStringExtra(EXTRA_ID);
        String moisText = mois.getText().toString();
        String anneeText = annee.getText().toString();
        String urlFiche = "http://gsb-applifrais-hvcode.esy.es/web/api/fiches/" + id + "/users/" + moisText + "/mois/" + anneeText + "/annee.json";


        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
                urlFiche, 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(loginVisiteur.this, affichageLigne.class);
                    intent.putExtra(EXTRA_ID, id);
                    startActivity(intent);

                } catch (JSONException e) {
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }
            }
        }, 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
            }
        });

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

}

