package com.example.patrice.reservation02;


import android.app.AlertDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

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 supprimer_jeu extends AppCompatActivity
{

    ListView lv;
    ArrayAdapter<String> adapter;
    EditText inputSearch;
    Button btn_supprimer;

    JSONParser jsonParser = new JSONParser();

    JSONParser jParser = new JSONParser();

    private static String url_requete_liste_jeux_db = "http://analog-design.net/WORKAREA/WEBSITE/reservation/request_all_games.php";
    private static String url_requete_infos_jeu = "http://analog-design.net/WORKAREA/WEBSITE/reservation/requete_infos_jeu.php";
    private static String url_admin_update_jeu_db = "http://analog-design.net/WORKAREA/WEBSITE/reservation/admin_update_jeu_db.php";
    private static String url_admin_supprime_jeu_db = "http://analog-design.net/WORKAREA/WEBSITE/reservation/admin_supprime_jeu_db.php";

    ArrayList<HashMap<String, String>> jeux_liste;

    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_NAME = "nom";

    JSONArray jeux = null;


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

        getSupportActionBar().setTitle(" Supprimer un jeu");
        getSupportActionBar().setLogo(R.drawable.remove);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

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

        lv = (ListView) findViewById(R.id.list_view);
        inputSearch = (EditText) findViewById(R.id.inputSearch);


        /* chargement */
        new charge_liste_jeux_db().execute();
/*
        btn_supprimer.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {

                Toast.makeText(supprimer_jeu.this, "Supprimer le jeu...", Toast.LENGTH_LONG).show();

               // supprimer_dialog(listview_index, inputSearch.getText().toString() );


            }
        });
*/

        /* chargement */
        new charge_liste_jeux_db().execute();

    }

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

    // pour le retour - inclus dans l'action bar ( < )
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


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

    class charge_liste_jeux_db extends AsyncTask<String, String, String[]>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        protected String[] doInBackground(String... args)
        {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            JSONObject json = jParser.makeHttpRequest(url_requete_liste_jeux_db, "GET", params);

            Log.d("Tous les jeux: ", json.toString());

            String elements [] = null;

            try
            {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1)
                {
                    jeux = json.getJSONArray("liste_jeux_db");

                    for (int i = 0; i < jeux.length(); i++)
                    {
                        JSONObject c = jeux.getJSONObject(i);

                        String id = c.getString(TAG_ID);
                        String name = c.getString(TAG_NAME);

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

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

                        // adding HashList to ArrayList
                        jeux_liste.add(map);

                    }
                }
                else
                {
                    // vide , ce n'est pas prévu
                }
            }

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


            return null;
        }

        protected void onPostExecute(String tab [])
        {
            List<String> liste = new ArrayList<String>();

            for (int i=0;i<jeux_liste.size();i++)
            {
                HashMap<String, String> hashmap= jeux_liste.get(i);
                String nom = hashmap.get("nom");
                liste.add(nom);
            }

            // convertir arraylist en array
            String[] computers = liste.toArray(new String[liste.size()]);

           // adapter = new ArrayAdapter<String>(supprimer_jeu.this, R.layout.search_list_item_layout, R.id.product_name, computers);
           // lv.setAdapter(adapter);

        }
    }

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

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



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

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

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
        }

        protected String doInBackground(String... args)
        {

            String id = args[0];

            int success;
            String result = null;

            try
            {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("id", id));

                JSONObject json = jsonParser.makeHttpRequest(url_admin_supprime_jeu_db, "POST", params);

                Log.d("Delete jeu", json.toString());

                success = json.getInt("sucess");
                if (success == 1)
                {
                    //Intent i = getIntent();
                    //setResult(100, i);
                    //finish();
                    result = "OK";
                }
            }

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

            return result;
        }

        protected void onPostExecute(String result)
        {


        }

    }

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

}