package net.wholesome.wholesomestart;

import android.app.NotificationManager;
import android.content.Context;
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 int NOTIFICATION_ID = 1;

    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.makeNotification(context, responseData.getJSONArray("children"));
                } catch (JSONException e) {
                    GeneralHelpers.Log("JSON parsing the /r/wholesomememes response failed: " + e.getMessage());
                    e.printStackTrace();
                }
            }
        });
    }

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

        try {
            GeneralHelpers.Log("Chosen post: " + post.getString("title"));
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle("Here is today's post!")
                            .setContentText(post.getString("title"));

            NotificationManager notificationManager = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(NOTIFICATION_ID, notificationBuilder.build());
        } catch (JSONException e) {
            GeneralHelpers.Log("Failed to get title of post: " + e.getMessage());
            e.printStackTrace();
        }
    }

    @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;
    }
}

@@ -91,23 +91,112 @@
 oid.
-content.Context
+app.PendingIntent;%0Aimport android.content.Context;%0Aimport android.content.Intent;%0Aimport android.net.Uri
 ;%0Aim
@@ -766,31 +766,147 @@
 tic 
-int NOTIFICATION_ID = 1
+String REDDIT_BASE_URL = %22https://www.reddit.com%22;%0A    private static int NOTIFICATION_ID = 1;%0A    private static int PENDING_INTENT_ID = 0
 ;%0A%0A 
@@ -1578,20 +1578,26 @@
 Creator.
-make
+prepareFor
 Notifica
@@ -1927,12 +1927,18 @@
 oid 
-make
+prepareFor
 Noti
@@ -2078,75 +2078,800 @@
     
-GeneralHelpers.Log(%22Chosen post: %22 + post.getString(%22title%22));%0A    
+String postTitle = post.getString(%22title%22);%0A            String url = post.getString(%22permalink%22);%0A            String fullUrl = REDDIT_BASE_URL + url;%0A%0A            GeneralHelpers.Log(%22Chosen post: %22 + postTitle);%0A            NotificationCreator.makeNotification(context, postTitle, fullUrl);%0A        %7D catch (JSONException e) %7B%0A            GeneralHelpers.Log(%22Failed to get title of post: %22 + e.getMessage());%0A            e.printStackTrace();%0A        %7D%0A    %7D%0A%0A    private static void makeNotification(Context context, String title, String url) %7B%0A        //Set intent for when notification is clicked%0A        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));%0A        PendingIntent pendingIntent = PendingIntent.getActivity(context, PENDING_INTENT_ID,%0A                intent, 0);%0A%0A
     
@@ -2923,20 +2923,16 @@
 ilder =%0A
-    
         
@@ -2979,20 +2979,16 @@
 ontext)%0A
-    
         
@@ -3055,36 +3055,32 @@
                 
-    
 .setContentTitle
@@ -3121,36 +3121,32 @@
                 
-    
 .setContentText(
@@ -3149,41 +3149,129 @@
 ext(
-post.getString(%22title%22));%0A%0A
+title)%0A                        .setAutoCancel(true)%0A                        .setContentIntent(pendingIntent);%0A  
       
+%0A  
     
@@ -3360,20 +3360,16 @@
         
-    
 .getSyst
@@ -3409,20 +3409,16 @@
 RVICE);%0A
-    
         
@@ -3495,169 +3495,8 @@
 ));%0A
-        %7D catch (JSONException e) %7B%0A            GeneralHelpers.Log(%22Failed to get title of post: %22 + e.getMessage());%0A            e.printStackTrace();%0A        %7D%0A
     

Make clicking on notification open the post page