package com.example.patrice.boot_04;

import android.app.Notification;
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.support.v4.content.ContextCompat;
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;

import static android.content.Context.NOTIFICATION_SERVICE;

// https://learnandroidwithme.wordpress.com/2013/10/09/how-to-schedule-notification-via-alarmmanager-in-android/

//https://androiddeveloperhelp.wordpress.com/2014/12/26/daily-local-notification-android/

public class notification_display 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 ");
/*
        mManager = (NotificationManager) context.getApplicationContext().getSystemService(context.getApplicationContext().NOTIFICATION_SERVICE);
      //  Intent intent1 = new Intent(context.getApplicationContext(),Wishes.class);
        Notification notification = new Notification(R.drawable.ic_launcher,"Birthday wish from Ajeet", System.currentTimeMillis());
     //   intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
      //  PendingIntent pendingNotificationIntent = PendingIntent.getActivity( context.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
       // notification.setLatestEventInfo(context.getApplicationContext(), "Happy BirthDay Devika", "Click To see the birthday Wish", pendingNotificationIntent);

        mManager.notify(0, notification);
*/

        updateTask.execute("3");


/*
            int MID = 0;
            long when = System.currentTimeMillis();
            NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Intent notificationIntent = new Intent(context, cancel_notification.class);
            notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

            NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("pipo")
                    .setContentText("Events To be PErformed")
                    .setSound(alarmSound)
                    .setAutoCancel(true).setWhen(when)
                    .setContentIntent(pendingIntent);
            // .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notificationManager.notify(MID, mNotifyBuilder.build());

            //MID++;
*/

    }

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

    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;

                    //int notifId = 100;
                    // showNotification(ctx, "title", message, notifId);
                    //Toast.makeText(context, "Suppression d'une notification", Toast.LENGTH_SHORT).show();




                    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, cancel_notification.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.ic_launcher)
                    .setContentTitle("444wwwpipo")
                    .setContentText("Events To be PErformed")
                    .setSound(alarmSound)
                    .setAutoCancel(true)
                    //.setWhen(when)
                    .setContentIntent(pendingIntent);
            // .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000});
            notificationManager.notify(MID, mNotifyBuilder.build());

        }
    }

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

    private void showNotification(Context context, String title, String message, int notifId)
    {

        int num = (int) System.currentTimeMillis();

        NotificationManager notificationManagerCompat = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
                .setContentTitle(title)
                .setSmallIcon(R.drawable.ic_replay_30_black_36dp)
                .setContentText(message)
                .setColor(ContextCompat.getColor(context, android.R.color.black))
                //.setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setSound(alarmSound);
        //  notificationManagerCompat.notify(notifId, builder.build());

        notificationManagerCompat.notify(num, builder.build());
    }

}