package com.example.patrice.rerservation01;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class mes_emprunts02 extends AppCompatActivity // ListActivity
{
    private ProgressDialog pDialog;
    // Creating JSON Parser object
    JSONParser jParser = new JSONParser();

    ArrayList<HashMap<String, String>> productsList;

    // url to get all products list
    //private static String url_all_products = "https://api.androidhive.info/android_connect/get_all_products.php";
    private static String url_jeu = "http://analog-design.net/WORKAREA/WEBSITE/Mysql/mes_emprunts.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_PRODUCTS = "products";
    private static final String TAG_ID = "id";
    private static final String TAG_JEU = "jeu";
    private static final String TAG_INFOS = "infos";
    private static final String TAG_DATE = "date";
    private static final String TAG_TIME = "time";

    JSONArray products = null;

    ListView listing;
    TextView infos;

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

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

        listing = (ListView) findViewById(R.id.list);
        infos = (TextView) findViewById(R.id.xxx);

        infos.setText("Patoche ... ?");

        new chargement_infos_DB().execute();

        /*
        ActionBar actionBar = getActionBar();
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);
        setTitle("Edit repeat");
        */
        //        ActionBar ab = getActionBar();
        //        ab.setTitle("This is new text for ActionBar title");

    }

    class chargement_infos_DB extends AsyncTask<String, String, String>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            pDialog = new ProgressDialog(mes_emprunts02.this);
            pDialog.setMessage("Chargement ...Patience.");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args)
        {
            List<NameValuePair> params = new ArrayList<NameValuePair>();

            JSONObject json = jParser.makeHttpRequest(url_jeu, "GET", params);

            Log.d("Affiche format JSON: ", json.toString());

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

                if (success == 1)
                {
                    // products found
                    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 name = c.getString(TAG_JEU);
                        String date = c.getString(TAG_DATE);
                        String time = c.getString(TAG_TIME);

                        //name = name + " " + date + " " + time +"  *toto" ;

                        // creating new HashMap
                        HashMap<String, String> map = new HashMap<String, String>();

                        map.put(TAG_ID, id);
                        map.put(TAG_JEU, name);

                        String infos = date + " " + time;

                        map.put(TAG_INFOS, infos);

                        // 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_emprunts02.this, productsList, R.layout.list_item, new String[]
                            { TAG_ID,  TAG_JEU, TAG_INFOS },    new int[] { R.id.pid, R.id.name, R.id.infos });

                    // updating listview
//                  listing.setListAdapter(adapter);

                   listing.setAdapter(adapter);

                    // android:id="@android:id/list" dans xml et on fixe  setListAdapter(adapter);

                   // listing.setAdapter(new CustomAdapter(this, productsList));
                }
            });
        }
    }



    class CustomAdapter extends BaseAdapter
    {
        Context context;
        ArrayList<HashMap<String, String>> productsList = new ArrayList<HashMap<String, String>>();

        public CustomAdapter(Runnable context, ArrayList productsList)
        {
           // this.context = context;
            this.productsList = productsList;
        }

        @Override
        public int getCount()
        {
            return productsList.size();
        }

        @Override
        public Object getItem(int position)
        {
            return productsList.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        private class Holder
        {
            TextView tv;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent)
        {
            convertView = null;

            if (convertView == null)
            {
                LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

                convertView = mInflater.inflate(R.layout.list_item, null);

                Holder holder = new Holder();

               holder.tv = (TextView) convertView.findViewById(R.id.name);
                holder.tv.setText("plouf");
                // viewHolder.profilePic = (ImageView) rowView.findViewById(R.id.imgProfile);
               // viewHolder.username = (TextView) rowView.findViewById(R.id.txtUsername);
               // viewHolder.country = (TextView) rowView.findViewById(R.id.txtCountry);


                Button btn_delete = (Button) convertView.findViewById(R.id.btn_delete);

                //Model m = productsList.get(position);
                //tvName.setText(m.get());

                btn_delete.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v)
                    {
                        //productsList.remove(position);
                        //notifyDataSetChanged();
                        Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
                    }
                });
            }
            return convertView;
        }
    }


}