package com.example.patrice.rerservation01;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
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.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

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

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

// https://github.com/niravkalola/ListViewDemo/tree/master/app/src/main/java/com/nkdroid/listview/demo

public class mes_emprunts04 extends AppCompatActivity
{
    private ListView lv;
    private MyAppAdapter myAppAdapter;

    //private ArrayList<ProfileModel02> profileModelArrayList;

    private ArrayList<Jeu_Listing> DB_Response;     // profileModelArrayList;

    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_DATE = "date";
    private static final String TAG_TIME = "time";

    JSONArray products = null;

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

        new chargement_infos_DB().execute();

    }

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

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

                    DB_Response = new ArrayList<>();

                    // 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);

                        DB_Response.add(new Jeu_Listing(id, name ,date, time ));
                        //profileModelArrayList.add(new ProfileModel02("Surabhi","India",R.drawable.games));
                        //profileModelArrayList.add(new ProfileModel02("Martins","Nigeria",R.drawable.games));
                    }
                }

                else
                {
                    // pas de jeu dans l'histoirque
                }
            }

            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()
                {

                    lv = (ListView)findViewById(R.id.lv_emprunts);

                    myAppAdapter = new MyAppAdapter(DB_Response, mes_emprunts04.this);

                    lv.setAdapter(myAppAdapter);

                }
            });
        }
    }

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

    public class MyAppAdapter extends BaseAdapter
    {

        public class ViewHolder
        {
            TextView username,country;
            ImageView profilePic;
        }

        //public ArrayList<ProfileModel02> profileList;
        public ArrayList<Jeu_Listing> elements_liste; //profileList;

        public Context context;

        //private MyAppAdapter(ArrayList<ProfileModel02> apps, Context context)
        private MyAppAdapter(ArrayList<Jeu_Listing> apps, Context context)
        {
            this.elements_liste = apps;
            this.context = context;
        }

        @Override
        public int getCount()
        {
            return elements_liste.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.item_profile01, parent,false);

                // configure view holder
                viewHolder = new ViewHolder();

                viewHolder.profilePic = (ImageView) rowView.findViewById(R.id.imgProfile);
                viewHolder.username = (TextView) rowView.findViewById(R.id.txtUsername);
                viewHolder.country = (TextView) rowView.findViewById(R.id.txtCountry);
                rowView.setTag(viewHolder);
            }

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

            //viewHolder.username.setText(profileList.get(position).getUsername()+"");
            //viewHolder.country.setText(profileList.get(position).getCountry()+"");

            viewHolder.username.setText(elements_liste.get(position).getId() + "" );
            viewHolder.country.setText(elements_liste.get(position).getJeu() + "" );


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

            btn_delete.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    String titre_jeu = DB_Response.get(position).getJeu();

                    String msg = "Restitution du jeu " + "\n" + titre_jeu;

                    AlertDialog.Builder builder = new AlertDialog.Builder(mes_emprunts04.this);
//                    builder.setTitle("Restitution du jeu emprunter !");
                    builder.setTitle(msg);

                    builder.setMessage("Déposer le jeu ?");
                    builder.setCancelable(false);

                    builder.setPositiveButton("Oui", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Toast.makeText(getApplicationContext(), "Jeu rendu, merci :) ", Toast.LENGTH_LONG).show();


                            DB_Response.remove(position);
                            notifyDataSetChanged();
                        }
                    });

                    builder.setNegativeButton("Non", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which)
                        {
                            Toast.makeText(getApplicationContext(), "Abandon", Toast.LENGTH_SHORT).show();
                        }
                    });

                    builder.show();

                }
            });

            return rowView;

        }

    }
}

/*
class ProfileModel02
{

    private String username;
    private String country;
    private int profilePic;

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

    /*public ProfileModel02(String username, String country, int profilePic)
    {
        this.username = username;
        this.country = country;
        this.profilePic = profilePic;
    }*/
/*
    public ProfileModel02 (String id, String jeu, String date, String time)
    {
        this.id = id;
        this.jeu = jeu;
        this.date = date;
        this.time = time;
    }

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

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

    /*************************/
/*
    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 Jeu_Listing
{

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

    public Jeu_Listing (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;
    }
}
