package net.wholesome.wholesomestart;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;

import net.wholesome.wholesomestart.helpers.GeneralHelpers;
import net.wholesome.wholesomestart.helpers.NetworkHelpers;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

class NotificationCreator {
    private static String WHOLESOME_TOP_DAILY_URL = "https://nm.reddit.com/r/wholesomememes/top.json?sort=top&t=day";
    private static String REDDIT_BASE_URL = "https://www.reddit.com";
    private static int NOTIFICATION_ID = 1;
    private static int PENDING_INTENT_ID = 0;

    public static void createNewNotification(final Context context) {
        NetworkHelpers.get(WHOLESOME_TOP_DAILY_URL, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                GeneralHelpers.Log("Failed to get /r/wholesomememes posts: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                try {
                    JSONObject jsonResponse = new JSONObject(response.body().string());
                    JSONObject responseData = jsonResponse.getJSONObject("data");
                    NotificationCreator.prepareForNotification(context, responseData.getJSONArray("children"));
                } catch (JSONException e) {
                    GeneralHelpers.Log("JSON parsing the /r/wholesomememes response failed: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }

    private static void prepareForNotification(Context context, JSONArray posts) {
        JSONObject post = NotificationCreator.choosePost(posts);

        try {
            String postTitle = post.getString("title");
            String url = post.getString("permalink");
            String fullUrl = REDDIT_BASE_URL + url;

            GeneralHelpers.Log("Chosen post: " + postTitle);
            NotificationCreator.makeNotification(context, postTitle, fullUrl);
        } catch (JSONException e) {
            GeneralHelpers.Log("Failed to get title of post: " + e.getMessage());
            e.printStackTrace();
        }
    }

    private static void makeNotification(Context context, String title, String url) {
        //Set intent for when notification is clicked
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_ID,
                intent, 0);

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Here is today's post!")
                        .setContentText(title)
                        .setAutoCancel(true)
                        .setContentIntent(pendingIntent);
        
        NotificationManager notificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
    }

    @Nullable
    private static JSONObject choosePost(JSONArray posts) {
        try {
            return posts.getJSONObject(0).getJSONObject("data");
        } catch (JSONException e) {
            GeneralHelpers.Log("Getting single post from posts array failed: " + e.getMessage());
            e.printStackTrace();
        }

        return null;
    }
}

@@ -161,32 +161,103 @@
 content.Intent;%0A
+import android.graphics.Bitmap;%0Aimport android.graphics.BitmapFactory;%0A
 import android.n
@@ -264,16 +264,16 @@
 et.Uri;%0A
-
 import a
@@ -594,16 +594,44 @@
 ception;
+%0Aimport java.io.InputStream;
 %0A%0Aimport
@@ -2322,16 +2322,73 @@
 L + url;
+%0A            String imgUrl = post.getString(%22thumbnail%22);
 %0A%0A      
@@ -2474,32 +2474,28 @@
 Creator.
-makeNotification
+getThumbnail
 (context
@@ -2514,16 +2514,24 @@
  fullUrl
+, imgUrl
 );%0A     
@@ -2685,32 +2685,774 @@
        %7D%0A    %7D%0A%0A
+    private static void getThumbnail(final Context context, final String title, final String postUrl, String imgUrl) %7B%0A        NetworkHelpers.get(imgUrl, new Callback() %7B%0A            @Override%0A            public void onFailure(Call call, IOException e) %7B%0A                GeneralHelpers.Log(%22Failed to get thumbnail for notification: %22 + e.getMessage());%0A            %7D%0A%0A            @Override%0A            public void onResponse(Call call, Response response) throws IOException %7B%0A                InputStream imageStream = response.body().byteStream();%0A                Bitmap bitmap = BitmapFactory.decodeStream(imageStream);%0A                NotificationCreator.makeNotification(context, title, postUrl, bitmap);%0A            %7D%0A        %7D);%0A    %7D%0A%0A
     private stat
@@ -3517,16 +3517,30 @@
 ring url
+, Bitmap image
 ) %7B%0A    
@@ -3955,16 +3955,61 @@
 uncher)%0A
+                        .setLargeIcon(image)%0A
         
@@ -4216,24 +4216,16 @@
 ntent);%0A
-        
 %0A       

Make post thumbnail be used for notification large icon