package com.example.patrice.appli_01;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
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.List;

public class traitement extends BroadcastReceiver
{

    String load_nom = null;          // 0
    String load_barcode = null;      // 1
    String load_description = null;  // 2
    String load_image = null;        // 3

    boolean flag_checkin = false;

    JSONParser jsonParser = new JSONParser();


    private static String url_requete_infos_jeu = "http://analog-design.net/WORKAREA/WEBSITE/reservation/requete_infos_jeu.php";

    private static final String TAG_SUCCESS = "success";


    requete updateTask = new requete();

    Context ctx = null;

    private NotificationManager mManager;

    public void onReceive(Context context, Intent intent)
    {

        ctx = context;

        Toast.makeText(context, "Boot 4", Toast.LENGTH_LONG).show();

        Log.v("*********", "trace 4 ");

        updateTask.execute("3");
    }

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

    class requete extends AsyncTask<String, String, String>
    {
        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();

        }

        protected String doInBackground(String... args)
        {
            String id = args[0];

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

            JSONObject json = jsonParser.makeHttpRequest(url_requete_infos_jeu, "GET", params);
            Log.d("Affiche format JSON: ", json.toString());

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

                if (success == 1)
                {
                    JSONArray productObj = json.getJSONArray("jeu");
                    // le premier element, car il y en a un seul !
                    JSONObject product = productObj.getJSONObject(0);

                    load_nom = product.getString("nom");
                    load_description = "blabla..."; //product.getString("description");
                    load_barcode = product.getString("barcode");
                    load_image = product.getString("image");

                    String message = load_nom + "&" + load_description + "&" + load_barcode + "&" + load_image;

                    flag_checkin = true;

                    Log.d(" HTTP ******* ", message);

                }

                else
                {
                    // erreur
                }
            }

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

            return("toto");
        }

        protected void onPostExecute(String s)
        {
            int MID = 0;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(ctx, pipo.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(ctx)
                    .setSmallIcon(R.drawable.android)
                    .setContentTitle("2 Mai 2018")
                    .setContentText("blabla")
                    .setSound(alarmSound)
                    .setAutoCancel(true)
                    //.setWhen(when)
                    .setContentIntent(pendingIntent);
            // .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notificationManager.notify(MID, mNotifyBuilder.build());

        }
    }



}