package com.example.patrice.rerservation01;


import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class mes_emprunts extends ListActivity
{

    String pid;


    JSONParser jsonParser = new JSONParser();

    private static String url_emprunts_detials = "http://analog-design.net/WORKAREA/WEBSITE/Mysql/mes_emprunts.php";

    private ProgressDialog pDialog;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;

    JSONArray products = null;

    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_ID = "id";
    private static final String TAG_CLIENT = "client";
    private static final String TAG_JEU = "jeu";
    private static final String TAG_DATE = "date";
    private static final String TAG_TIME = "time";
    private static final String TAG_TS = "ts";
    private static final String TAG_SUCCESS = "success";

    JSONObject product;

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

        // Getting complete product details in background thread
        new GetProductDetails().execute();

        ListView lv = getListView();
    }

    /*****************************************************************/

    class GetProductDetails extends AsyncTask<String, String, String>
    {

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            pDialog = new ProgressDialog(mes_emprunts.this);
            pDialog.setMessage("Loading products. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }


        protected String doInBackground(String... args)
        {
            // Building Parameters
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            // getting JSON string from URL
            JSONObject json = jParser.makeHttpRequest(url_emprunts_detials, "GET", params);

            // Check your log cat for JSON reponse
            Log.d("All Products: ", json.toString());

            try {
                // Checking for SUCCESS TAG
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1)
                {
                    // products found
                    // Getting Array of Products
                    products = json.getJSONArray(TAG_PRODUCTS);

                    // looping through All Products
                    for (int i = 0; i < products.length(); i++)
                    {
                            JSONObject c = products.getJSONObject(i);

                            // Storing each json item in variable
                            String id = c.getString(TAG_ID);
                            String client = c.getString(TAG_CLIENT);
                            String jeu = c.getString(TAG_JEU);
                            String date = c.getString(TAG_DATE);
                            String time = c.getString(TAG_TIME);
                            String ts = c.getString(TAG_TS);

                            // creating new HashMap
                            HashMap<String, String> map = new HashMap<String, String>();  //?   prevoir 6 * string car 6 elements et non 2

                            // adding each child node to HashMap key => value
                            map.put(TAG_ID, "id");
                            //map.put(TAG_CLIENT, client);
                            map.put(TAG_JEU, "jeu");
                            //map.put(TAG_DATE, date);
                            //map.put(TAG_TIME, time);
                            //map.put(TAG_TS, ts);


                            // adding HashList to ArrayList
                            productsList.add(map);
                    }
                }
                else
                {
                    // no products found

                }
            }

            catch (JSONException e)
            {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url)
        {

            pDialog.dismiss();
            // updating UI from Background Thread
            runOnUiThread(new Runnable()
            {
                public void run()
                {

                    ListAdapter adapter = new SimpleAdapter(mes_emprunts.this, productsList, R.layout.list_item, new String[]
                            { TAG_ID,  TAG_JEU},    new int[] { R.id.pid, R.id.name });

                    // updating listview
                    setListAdapter(adapter);


                }
            });
        }
    }

    /*****************************************************************/


}