package com.example.patrice.rerservation01;


import android.app.ProgressDialog;
import android.content.Context;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import org.apache.http.NameValuePair;
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_emprunts03 extends AppCompatActivity
{

    private ProgressDialog pDialog;

    JSONParser jParser = new JSONParser();

    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;

    private ArrayList<Jeu_data> DB_ArrayList;

    ListView listing;
    private MyAppAdapter myAppAdapter;

    private ArrayList<ProfileModel> profileModelArrayList;

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

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

//        profileModelArrayList=new ArrayList<>();
//        profileModelArrayList.add(new ProfileModel("Nirav","India",R.drawable.games));
//        profileModelArrayList.add(new ProfileModel("Surabhi","India",R.drawable.games));
//        profileModelArrayList.add(new ProfileModel("Martins","Nigeria",R.drawable.games));

        new chargement_infos_DB().execute();

    }

    class chargement_infos_DB extends AsyncTask<String, String, String>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            pDialog = new ProgressDialog(mes_emprunts03.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);

                      //  Jeu_data obj = new Jeu_data();
                        profileModelArrayList=new ArrayList<>();
                        profileModelArrayList.add(new ProfileModel("Nirav","India",R.drawable.games));
                        profileModelArrayList.add(new ProfileModel("Surabhi","India",R.drawable.games));
                        profileModelArrayList.add(new ProfileModel("Martins","Nigeria",R.drawable.games));



                        //DB_ArrayList.add(new Jeu_data(id, name, date, time ) );

                        //obj.setId(id);
                        //obj.setJeu(name);
                        //obj.setDate(date);
                        //obj.setTime(time);

                       // DB_ArrayList.add(obj);


                    }
                }

                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()
                {
                    myAppAdapter = new MyAppAdapter(DB_ArrayList , mes_emprunts03.this);
                    listing.setAdapter(myAppAdapter);

                }
            });
        }
    }

    class MyAppAdapter extends BaseAdapter {

        public class ViewHolder
        {
            TextView aqw;

        }

        public ArrayList<Jeu_data> blabla;

        public Context context;


        private MyAppAdapter(ArrayList<Jeu_data> apps, Context context)
        {
            this.blabla = apps;
            this.context = context;

        }

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

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

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

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

            View rowView = convertView;
            ViewHolder viewHolder;

            if (rowView == null)
            {
                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.list_item, parent,false);

                // configure view holder

                viewHolder = new ViewHolder();

                viewHolder.aqw = (TextView) rowView.findViewById(R.id.name);

                rowView.setTag(viewHolder);

            }

            else
            {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.aqw.setText("Bingo");

            return rowView;


        }


    }

}


class Jeu_data
{

   String id;
   String jeu;
   String date;
   String time;

    public Jeu_data (String id, String jeu, String date, String time)
    {
        this.id = id;
        this.jeu = jeu;
        this.date = date;
        this.time = time;
    }


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

    public String getId()
    {
        return id;
    }

    public void setId(String id)
    {
        this.id = id;
    }

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

    public String getJeu()
    {
        return jeu;
    }

    public void setJeu(String jeu)
    {
        this.jeu = jeu;
    }

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

    public String getDate()
    {
        return date;
    }

    public void setDate(String date)
    {
        this.date = date;
    }

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

    public String getTime()
    {
        return time;
    }

    public void setTime(String time)
    {
        this.time = time;
    }
}

class ProfileModel {

    private String username;

    private String country;

    private int profilePic;

    public ProfileModel(String username, String country, int profilePic)
    {
        this.username = username;
        this.country = country;
        this.profilePic = profilePic;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getProfilePic() {
        return profilePic;
    }

    public void setProfilePic(int profilePic) {
        this.profilePic = profilePic;
    }
}