package com.example.patrice.rerservation01;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ListView;

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

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

//https://github.com/ashishmishra007/Android-Asynchronous-Image-Loading-Example

public class listview_all_games extends Activity
{

    ListView list;
    Listview_Adapter adapter;

    JSONArray element = null;

    ArrayList<HashMap<String, String>> AppArrList = new ArrayList<HashMap<String, String>>();

    ArrayList<HashMap<String, String>> tempo = new ArrayList<HashMap<String, String>>();


    String[] imageUrls;

    //= new String [];



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

        imageUrls = new String[400];

        list=(ListView)findViewById(R.id.listView1);



        // tâche de fond
        String urlString = "http://analog-design.net/WORKAREA/WEBSITE/reservation/query04.php";
        try {
            tempo = new ProcessJSON().execute(urlString).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }


        adapter=new Listview_Adapter(this, imageUrls, tempo);
        list.setAdapter(adapter);
    }

    @Override
    public void onDestroy()
    {
        list.setAdapter(null);
        super.onDestroy();
    }

    private class ProcessJSON extends AsyncTask<String, Void, ArrayList>
    {

        @Override
        protected void onPreExecute()
        {
        }

        @Override
        protected ArrayList doInBackground(String... strings)
        {
            String stream = null;
            String urlString = strings[0];

            newHTTPDataHandler hh = new newHTTPDataHandler();
            stream = hh.GetHTTPData(urlString);

            try
            {
                JSONObject jObj = null;
                jObj = new JSONObject(stream);
                element = jObj.getJSONArray("result");

                HashMap<String, String> map;

                for (int i = 0; i < element.length(); i++)
                {
                    JSONObject x = element.getJSONObject(i);
                    map = new HashMap<String, String>();
                    map.put("id", x.getString("id"));
                    map.put("nom", x.getString("nom"));
                    map.put("image", x.getString("image"));
                    map.put("barcode", x.getString("barcode"));
                    AppArrList.add(map);

                    imageUrls[i] = x.getString("image");



                }

            }

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


            return AppArrList; // a voir

        }

        @Override
        protected void onPostExecute(final ArrayList data)
        {

        }

    }

    class newHTTPDataHandler
    {
        String stream = null;

        public newHTTPDataHandler()
        {
        }

        public String GetHTTPData(String urlString)
        {
            try
            {
                URL url = new URL(urlString);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                // Check the connection status
                if(urlConnection.getResponseCode() == 200)
                {
                    // if response code = 200 ok
                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                    // Read the BufferedInputStream
                    BufferedReader r = new BufferedReader(new InputStreamReader(in));
                    StringBuilder sb = new StringBuilder();
                    String line;
                    while ((line = r.readLine()) != null)
                    {
                        sb.append(line);
                    }
                    stream = sb.toString();
                    // End reading...............

                    // Disconnect the HttpURLConnection
                    urlConnection.disconnect();
                }
                else
                {
                    // Do something
                }
            }

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

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

            finally
            {

            }

            // Return the data from specified url
            return stream;
        }
    }


}