package com.example.pierrot.jsonvolley;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.text.method.ScrollingMovementMethod;
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.JSONArray;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.pierrot.jsonvolley.app.AppController;


import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONException;
import org.json.JSONObject;

public class affichageLigne extends AppCompatActivity {

    private static String TAG = MainActivity.class.getSimpleName();
    private TextView affichageligne;
    final String EXTRA_ID = "";
    private String txtReponse = "";

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

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

        String id = intent.getStringExtra(EXTRA_ID);


        String urlLigneFrais = "http://gsb-applifrais-hvcode.esy.es/web/api/lignes/" + id + "/frais/fiches.json";


        JsonArrayRequest req = new JsonArrayRequest(urlLigneFrais,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            txtReponse+="--- Ligne Frais Forfait ---"+"\n\n";
                            for (int i = 0; i < response.length(); i++) {

                                JSONObject person = (JSONObject) response
                                        .get(i);

                                String quantiterepas = person.getString("quantiterepasmidi");
                                String quantiterelaisetape = person.getString("quantiterelaisetape");
                                String quantitenuitee = person.getString("quantitenuitee");
                                String distance = person.getString("distance");

                                txtReponse += "Quantite repas midi: " + quantiterepas + "\n\n";
                                txtReponse += "Quantite relais etape: " + quantiterelaisetape + "\n\n";
                                txtReponse += "Quantite nuitee: " + quantitenuitee + "\n\n";
                                txtReponse += "distance: " + distance + "\n\n\n\n";

                            }

                            affichageligne.setText(txtReponse);
                            affichageligne.setMovementMethod(new ScrollingMovementMethod());

                        } 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();
            }
        });

        String urlLigneHors = "http://gsb-applifrais-hvcode.esy.es/web/api/lignes/" + id + "/hors/frais/fiches.json";

        JsonArrayRequest req2 = new JsonArrayRequest(urlLigneHors,
                new Response.Listener<JSONArray>() {
                    @Override
                    public void onResponse(JSONArray response) {
                        Log.d(TAG, response.toString());

                        try {
                            // Parsing json array response
                            // loop through each json object
                            txtReponse += "---- Ligne Hors Forfait ----" + "\n\n";
                            for (int i = 0; i < response.length(); i++) {

                                JSONObject ligneHorsFrais = (JSONObject) response
                                        .get(i);

                                String libelle = ligneHorsFrais.getString("libelle");
                                String montant = ligneHorsFrais.getString("montant");

                                txtReponse += "Libelle: " + libelle + "\n\n";
                                txtReponse += "Montant: " + montant + "\n\n";

                            }

                            affichageligne.setText(txtReponse);

                        } 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();
            }
        });

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(req);
        AppController.getInstance().addToRequestQueue(req2);

    }

}

