package com.example.patrice.rerservation01;

import android.os.AsyncTask;
import android.widget.Toast;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * Created by Patrice on 20/03/2018.
 */

public class debug
{

    private String retour = null;

    JSONParser jsonParser = new JSONParser();
    private static String url_update_jeu =  "http://analog-design.net/WORKAREA/WEBSITE/reservation/update_request.php";
    private static final String TAG_SUCCESS = "success";

    void debug()
    {

        try
        {

            String game_name = "A l'abordage ! - Der schwarze pirat";
            String filename = "7622210713803.jpeg";
            String image_store_path = "http://analog-design.net/WORKAREA/WEBSITE/reservation/Capture/" + filename ;
            String barcode = "7622210713803";
            retour = new xxxupdate_jeu().execute(game_name , image_store_path, barcode ).get();
        }

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

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

        if (retour.equals("OK"))
        {
           String erreur = "none";
        }
        else
        {
           String erreur = "FAIL";
        }

    }

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

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

        protected String doInBackground(String... args)
        {

            String nom = args[0];
            String image = args[1];
            String barcode = args[2];

            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("nom", nom));
            params.add(new BasicNameValuePair("image", image));
            params.add(new BasicNameValuePair("barcode", barcode));

            // sending modified data through http request
            // Notice that update product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_update_jeu, "POST", params);

            String reponse = null;

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

                if (success == 1)
                {
                    //Toast.makeText(gestion_DB02.this, "Ajout réussi. ", Toast.LENGTH_LONG).show();
                    reponse = "OK";
                }

                else
                {
                    // fail
                    // Toast.makeText(gestion_DB02.this, "Erreur pendant l'enregistrement !.", Toast.LENGTH_LONG).show();
                    reponse = "FAIL";
                }
            }

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

            return reponse; //null;//return null;
        }


        protected void onPostExecute(String file_url)
        {
        }
    }
}
